login_auth.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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) AuthPassword(ctx context.Context, phone, password string) (string, error) {
  45. // 验证是否存在
  46. user, err := db.GetUserByPhone(ctx, password)
  47. if err != nil {
  48. return "", err
  49. }
  50. // 验证正确性
  51. if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) {
  52. // 登录失败
  53. logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
  54. return "", errors.New("auth fail")
  55. }
  56. token := l.getToken(ctx, phone)
  57. auth := &redis_model.Auth{
  58. Phone: phone,
  59. ID: user.ID,
  60. User: user.User,
  61. Username: user.Username,
  62. RealName: user.RealName,
  63. Role: user.Role,
  64. Email: user.Email,
  65. Token: token,
  66. }
  67. if err := l.setSession(ctx, phone, auth); err != nil {
  68. return "", err
  69. }
  70. return token, nil
  71. }
  72. func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_model.Auth) error {
  73. if authJson, err := json.Marshal(auth); err == nil {
  74. err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL)
  75. if err == nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }
  81. func (l *loginAuth) getSession(ctx context.Context, phone string) (*redis_model.Auth, error) {
  82. value, err := redis.Get(ctx, l.getRedisKey(phone))
  83. if err != nil {
  84. if err == consts.RedisNil {
  85. return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  86. }
  87. return nil, err
  88. }
  89. auth := new(redis_model.Auth)
  90. if err = json.Unmarshal([]byte(value), auth); err != nil {
  91. return nil, err
  92. }
  93. return auth, nil
  94. }
  95. func (l *loginAuth) getToken(ctx context.Context, phone string) string {
  96. timeSeed := strconv.FormatInt(time.Now().Unix(), 10)
  97. token := phone + "." + timeSeed + "." + util.MD5(phone, timeSeed, consts.AuthSalt)
  98. return token
  99. }
  100. func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) {
  101. parts := strings.Split(token, ".")
  102. if len(parts) == 3 {
  103. phone := parts[0]
  104. timeSeed := parts[1]
  105. if parts[2] == util.MD5(phone, timeSeed, consts.AuthSalt) {
  106. return phone, nil
  107. }
  108. }
  109. return "", errors.New("token invalid")
  110. }
  111. func (l *loginAuth) encryptPassword(password string) string {
  112. return util.MD5(password)
  113. }
  114. func (l *loginAuth) getRedisKey(key string) string {
  115. return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
  116. }