login_auth.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.getSessionAuth(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) AuthCode(ctx context.Context, phone string, code string) (string, error) {
  45. user, err := db.GetUserByPhone(ctx, phone)
  46. fmt.Println("login_auth", user, err)
  47. if err != nil {
  48. return "", err
  49. } else if user == nil {
  50. // 账号不存在,则注册账号
  51. _, err = Enterprise.CreateEnterprise(ctx, phone)
  52. if err != nil {
  53. return "账号创建失败", err
  54. }
  55. user, err = db.GetUserByPhone(ctx, phone)
  56. fmt.Println("login_auth", user, err)
  57. if err != nil {
  58. return "", err
  59. }
  60. } else if string(user.Role) != consts.BRole {
  61. // 账号权限有误
  62. logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
  63. return "权限错误,请登录企业账号", errors.New("auth fail")
  64. }
  65. vcode, err := l.getSessionCode(ctx, phone)
  66. if err != nil {
  67. return "", err
  68. }
  69. fmt.Printf("缓存的验证码 vcode: %v,实际填入的 code:%v", vcode, code)
  70. if vcode != code {
  71. // 验证码错误
  72. logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
  73. return "验证码有误", errors.New("auth fail")
  74. }
  75. token := l.getToken(ctx, phone)
  76. enterprise, err := db.GetEnterpriseByUID(ctx, user.ID)
  77. if err != nil {
  78. return "", err
  79. }
  80. auth := &redis_model.Auth{
  81. Phone: phone,
  82. ID: user.ID,
  83. User: user.User,
  84. Username: user.Username,
  85. RealName: user.RealName,
  86. Role: user.Role,
  87. Email: user.Email,
  88. Token: token,
  89. EnterpriseID: enterprise.EnterpriseID,
  90. }
  91. if err := l.setSession(ctx, phone, auth); err != nil {
  92. fmt.Printf("setSession error\n")
  93. return "", err
  94. }
  95. return token, nil
  96. }
  97. // func (l *loginAuth) AuthPassword(ctx context.Context, phone string, password string) (string, error) {
  98. // // 验证是否存在
  99. // user, err := db.GetUserByPhone(ctx, phone)
  100. // if err != nil {
  101. // return "", err
  102. // }
  103. // // 验证正确性
  104. // if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) {
  105. // // 登录失败
  106. // logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone)
  107. // return "", errors.New("auth fail")
  108. // }
  109. // token := l.getToken(ctx, phone)
  110. // auth := &redis_model.Auth{
  111. // Phone: phone,
  112. // ID: user.ID,
  113. // User: user.User,
  114. // Username: user.Username,
  115. // RealName: user.RealName,
  116. // Role: user.Role,
  117. // Email: user.Email,
  118. // Token: token,
  119. // }
  120. // if err := l.setSession(ctx, phone, auth); err != nil {
  121. // return "", err
  122. // }
  123. // return token, nil
  124. // }
  125. func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_model.Auth) error {
  126. if authJson, err := json.Marshal(auth); err == nil {
  127. err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL)
  128. if err == nil {
  129. return err
  130. }
  131. }
  132. return nil
  133. }
  134. func (l *loginAuth) getSessionCode(ctx context.Context, phone string) (string, error) {
  135. value, err := redis.Get(ctx, l.getRedisKey(phone))
  136. if err != nil {
  137. if err == consts.RedisNil {
  138. return "", fmt.Errorf("not found in redis,phone:%+v", phone)
  139. }
  140. return "", err
  141. }
  142. return value, nil
  143. }
  144. func (l *loginAuth) getSessionAuth(ctx context.Context, phone string) (*redis_model.Auth, error) {
  145. value, err := redis.Get(ctx, l.getRedisKey(phone))
  146. if err != nil {
  147. if err == consts.RedisNil {
  148. return nil, fmt.Errorf("not found in redis,phone:%+v", phone)
  149. }
  150. return nil, err
  151. }
  152. auth := new(redis_model.Auth)
  153. if err = json.Unmarshal([]byte(value), auth); err != nil {
  154. return nil, err
  155. }
  156. return auth, nil
  157. }
  158. func (l *loginAuth) getToken(ctx context.Context, phone string) string {
  159. timeSeed := strconv.FormatInt(time.Now().Unix(), 10)
  160. token := phone + "." + timeSeed + "." + util.MD5(phone, timeSeed, consts.AuthSalt)
  161. return token
  162. }
  163. func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) {
  164. parts := strings.Split(token, ".")
  165. if len(parts) == 3 {
  166. phone := parts[0]
  167. timeSeed := parts[1]
  168. if parts[2] == util.MD5(phone, timeSeed, consts.AuthSalt) {
  169. return phone, nil
  170. }
  171. }
  172. return "", errors.New("token invalid")
  173. }
  174. func (l *loginAuth) encryptPassword(password string) string {
  175. return util.MD5(password)
  176. }
  177. func (l *loginAuth) getRedisKey(key string) string {
  178. return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
  179. }