login_auth.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/sirupsen/logrus"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "youngee_m_api/consts"
  12. "youngee_m_api/db"
  13. "youngee_m_api/model/redis_model"
  14. "youngee_m_api/model/system_model"
  15. "youngee_m_api/redis"
  16. "youngee_m_api/util"
  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. user, err := l.parseToken(ctx, token)
  29. if err != nil {
  30. logrus.Debug("token格式错误:%+v", token)
  31. return nil, err
  32. }
  33. auth, err := l.getSessionAuth(ctx, user)
  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) AuthCode(ctx context.Context, User string, password string) (string, string, string, error) {
  45. user, err := db.GetUser(ctx, User)
  46. if err != nil {
  47. return "", "", "", err
  48. } else if user == nil {
  49. // 账号不存在
  50. logrus.Debugf("[AuthCode] auth fail,User:%+v", User)
  51. return "账号不存在", "", "", errors.New("auth fail")
  52. } else if string(user.Role) != consts.BRole && string(user.Role) != consts.BRole2 {
  53. // 账号权限有误
  54. logrus.Debugf("[AuthCode] auth fail,User:%+v", User)
  55. return "权限错误,请登录管理账号", "", "", errors.New("auth fail")
  56. } else if string(user.UserState) != "1" {
  57. // 账号已经被禁用
  58. logrus.Debugf("[AuthCode] auth fail,User:%+v", User)
  59. return "账号已经被禁用", "", "", errors.New("auth fail")
  60. }
  61. var token string
  62. if user.Password == password {
  63. token = l.getToken(ctx, user.User)
  64. auth := &redis_model.Auth{
  65. Phone: user.Phone,
  66. ID: user.ID,
  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, user.User, auth); err != nil {
  75. fmt.Printf("setSession error\n")
  76. return "", "", "", err
  77. }
  78. }
  79. return token, user.Username, user.Role, nil
  80. }
  81. func (l *loginAuth) setSession(ctx context.Context, user string, auth *redis_model.Auth) error {
  82. if authJson, err := json.Marshal(auth); err == nil {
  83. err = redis.Set(ctx, l.getRedisKey(user), string(authJson), l.sessionTTL)
  84. if err == nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. //func (l *loginAuth) getSessionCode(ctx context.Context, phone string) (*string, error) {
  91. // value, err := redis.Get(ctx, l.getRedisKey(phone))
  92. // if err != nil {
  93. // if err == consts.RedisNil {
  94. // return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  95. // }
  96. // return nil, err
  97. // }
  98. // return &value, nil
  99. //}
  100. func (l *loginAuth) getSessionAuth(ctx context.Context, user string) (*redis_model.Auth, error) {
  101. value, err := redis.Get(ctx, l.getRedisKey(user))
  102. if err != nil {
  103. if err == consts.RedisNil {
  104. return nil, fmt.Errorf("not found in redis,user:%+v", user)
  105. }
  106. return nil, err
  107. }
  108. auth := new(redis_model.Auth)
  109. if err = json.Unmarshal([]byte(value), auth); err != nil {
  110. return nil, err
  111. }
  112. return auth, nil
  113. }
  114. func (l *loginAuth) getToken(ctx context.Context, user string) string {
  115. timeSeed := strconv.FormatInt(time.Now().Unix(), 10)
  116. token := user + "." + timeSeed + "." + util.MD5(user, timeSeed, consts.AuthSalt)
  117. return token
  118. }
  119. func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) {
  120. parts := strings.Split(token, ".")
  121. if len(parts) == 3 {
  122. user := parts[0]
  123. timeSeed := parts[1]
  124. if parts[2] == util.MD5(user, timeSeed, consts.AuthSalt) {
  125. return user, nil
  126. }
  127. }
  128. return "", errors.New("token invalid")
  129. }
  130. func (l *loginAuth) encryptPassword(password string) string {
  131. return util.MD5(password)
  132. }
  133. func (l *loginAuth) getRedisKey(key string) string {
  134. return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
  135. }