login_auth.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, 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. }
  57. var token string
  58. if user.Password == password{
  59. token = l.getToken(ctx, user.User)
  60. auth := &redis_model.Auth{
  61. Phone: user.Phone,
  62. ID: user.ID,
  63. User: user.User,
  64. Username: user.Username,
  65. RealName: user.RealName,
  66. Role: user.Role,
  67. Email: user.Email,
  68. Token: token,
  69. }
  70. if err := l.setSession(ctx, user.User, auth); err != nil {
  71. fmt.Printf("setSession error\n")
  72. return "", "",err
  73. }
  74. }
  75. return token,user.Username,nil
  76. }
  77. func (l *loginAuth) setSession(ctx context.Context, user string, auth *redis_model.Auth) error {
  78. if authJson, err := json.Marshal(auth); err == nil {
  79. err = redis.Set(ctx, l.getRedisKey(user), string(authJson), l.sessionTTL)
  80. if err == nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. //func (l *loginAuth) getSessionCode(ctx context.Context, phone string) (*string, error) {
  87. // value, err := redis.Get(ctx, l.getRedisKey(phone))
  88. // if err != nil {
  89. // if err == consts.RedisNil {
  90. // return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  91. // }
  92. // return nil, err
  93. // }
  94. // return &value, nil
  95. //}
  96. func (l *loginAuth) getSessionAuth(ctx context.Context, user string) (*redis_model.Auth, error) {
  97. value, err := redis.Get(ctx, l.getRedisKey(user))
  98. if err != nil {
  99. if err == consts.RedisNil {
  100. return nil, fmt.Errorf("not found in redis,user:%+v", user)
  101. }
  102. return nil, err
  103. }
  104. auth := new(redis_model.Auth)
  105. if err = json.Unmarshal([]byte(value), auth); err != nil {
  106. return nil, err
  107. }
  108. return auth, nil
  109. }
  110. func (l *loginAuth) getToken(ctx context.Context, user string) string {
  111. timeSeed := strconv.FormatInt(time.Now().Unix(), 10)
  112. token := user + "." + timeSeed + "." + util.MD5(user, timeSeed, consts.AuthSalt)
  113. return token
  114. }
  115. func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) {
  116. parts := strings.Split(token, ".")
  117. if len(parts) == 3 {
  118. user := parts[0]
  119. timeSeed := parts[1]
  120. if parts[2] == util.MD5(user, timeSeed, consts.AuthSalt) {
  121. return user, nil
  122. }
  123. }
  124. return "", errors.New("token invalid")
  125. }
  126. func (l *loginAuth) encryptPassword(password string) string {
  127. return util.MD5(password)
  128. }
  129. func (l *loginAuth) getRedisKey(key string) string {
  130. return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
  131. }