login_auth.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "youngee_b_api/consts"
  11. "youngee_b_api/db"
  12. "youngee_b_api/model/redis_model"
  13. "youngee_b_api/model/system_model"
  14. "youngee_b_api/redis"
  15. "youngee_b_api/util"
  16. "github.com/sirupsen/logrus"
  17. )
  18. var LoginAuth *loginAuth
  19. func LoginAuthInit(config *system_model.Session) {
  20. auth := new(loginAuth)
  21. auth.sessionTTL = time.Duration(config.TTL) * time.Minute
  22. LoginAuth = auth
  23. }
  24. type loginAuth struct {
  25. sessionTTL time.Duration
  26. }
  27. func (l *loginAuth) AuthToken(ctx context.Context, token string) (*redis_model.Auth, error) {
  28. phone, err := l.parseToken(ctx, token)
  29. if err != nil {
  30. logrus.Debug("token格式错误:%+v", token)
  31. return nil, err
  32. }
  33. auth, err := l.getSession(ctx, phone)
  34. if err != nil {
  35. logrus.Debug("获取session redis错误: token:%+v,err:%+v", token, err)
  36. return nil, err
  37. }
  38. if auth.Token != token {
  39. logrus.Debug("获取session time过期错误: token:%+v", token)
  40. return nil, errors.New("auth failed")
  41. }
  42. return auth, nil
  43. }
  44. func (l *loginAuth) AuthMSG(ctx context.Context, phone string, vcode string) (string, error) {
  45. // 验证是否存在
  46. user, err := db.GetUserByPhone(ctx, phone)
  47. if err != nil {
  48. return "", err
  49. }
  50. code, err := l.getSessionCode(ctx, phone)
  51. if err != nil {
  52. return "", err
  53. }
  54. if user == nil || string(user.Role) != consts.BRole || *code != vcode { // 登录失败
  55. logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
  56. return "", errors.New("auth fail")
  57. }
  58. enterpriseID, err := db.GetEnterpriseByUID(ctx, user.ID)
  59. if err != nil {
  60. return "", err
  61. }
  62. token := l.getToken(ctx, phone)
  63. auth := &redis_model.Auth{
  64. Phone: phone,
  65. ID: user.ID,
  66. EnterpriseID: *enterpriseID,
  67. User: user.User,
  68. Username: user.Username,
  69. RealName: user.RealName,
  70. Role: user.Role,
  71. Email: user.Email,
  72. Token: token,
  73. }
  74. if err := l.setSession(ctx, phone, auth); err != nil {
  75. fmt.Printf("setSession error\n")
  76. return "", err
  77. }
  78. return token, nil
  79. }
  80. // func (l *loginAuth) AuthPassword(ctx context.Context, phone string, password string) (string, error) {
  81. // // 验证是否存在
  82. // user, err := db.GetUserByPhone(ctx, phone)
  83. // if err != nil {
  84. // return "", err
  85. // }
  86. // // 验证正确性
  87. // if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) {
  88. // // 登录失败
  89. // logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
  90. // return "", errors.New("auth fail")
  91. // }
  92. // token := l.getToken(ctx, phone)
  93. // auth := &redis_model.Auth{
  94. // Phone: phone,
  95. // ID: user.ID,
  96. // User: user.User,
  97. // Username: user.Username,
  98. // RealName: user.RealName,
  99. // Role: user.Role,
  100. // Email: user.Email,
  101. // Token: token,
  102. // }
  103. // if err := l.setSession(ctx, phone, auth); err != nil {
  104. // return "", err
  105. // }
  106. // return token, nil
  107. // }
  108. func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_model.Auth) error {
  109. if authJson, err := json.Marshal(auth); err == nil {
  110. err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL)
  111. if err == nil {
  112. return err
  113. }
  114. }
  115. return nil
  116. }
  117. func (l *loginAuth) getSessionCode(ctx context.Context, phone string) (*string, error) {
  118. value, err := redis.Get(ctx, l.getRedisKey(phone))
  119. if err != nil {
  120. if err == consts.RedisNil {
  121. return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  122. }
  123. return nil, err
  124. }
  125. return &value, nil
  126. }
  127. func (l *loginAuth) getSession(ctx context.Context, phone string) (*redis_model.Auth, error) {
  128. value, err := redis.Get(ctx, l.getRedisKey(phone))
  129. if err != nil {
  130. if err == consts.RedisNil {
  131. return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  132. }
  133. return nil, err
  134. }
  135. auth := new(redis_model.Auth)
  136. if err = json.Unmarshal([]byte(value), auth); err != nil {
  137. return nil, err
  138. }
  139. return auth, nil
  140. }
  141. func (l *loginAuth) getToken(ctx context.Context, phone string) string {
  142. timeSeed := strconv.FormatInt(time.Now().Unix(), 10)
  143. token := phone + "." + timeSeed + "." + util.MD5(phone, timeSeed, consts.AuthSalt)
  144. return token
  145. }
  146. func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) {
  147. parts := strings.Split(token, ".")
  148. if len(parts) == 3 {
  149. phone := parts[0]
  150. timeSeed := parts[1]
  151. if parts[2] == util.MD5(phone, timeSeed, consts.AuthSalt) {
  152. return phone, nil
  153. }
  154. }
  155. return "", errors.New("token invalid")
  156. }
  157. func (l *loginAuth) encryptPassword(password string) string {
  158. return util.MD5(password)
  159. }
  160. func (l *loginAuth) getRedisKey(key string) string {
  161. return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
  162. }