token.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/golang-jwt/jwt"
  5. "strconv"
  6. "time"
  7. "iot_manager_service/app/user/model"
  8. "iot_manager_service/config"
  9. "iot_manager_service/util"
  10. "net/http"
  11. "strings"
  12. )
  13. var Auth = new(auth)
  14. type auth struct{}
  15. func (c *auth) Token(ctx *gin.Context) {
  16. tenantId := ctx.Query("tenantId")
  17. userName := ctx.Query("username")
  18. password := ctx.Query("password")
  19. grantType := ctx.Query("grant_type")
  20. refreshToken := ctx.Query("refresh_token")
  21. checkLock()
  22. userType := ctx.GetHeader(model.USER_TYPE_HEADER_KEY)
  23. token := model.Token{
  24. TenantId: tenantId,
  25. UserName: userName,
  26. Password: password,
  27. GrantType: grantType,
  28. RefreshToken: refreshToken,
  29. UserType: userType,
  30. }
  31. userInfo, err := grant(token, ctx)
  32. if err != nil {
  33. ctx.JSON(http.StatusOK, err)
  34. return
  35. }
  36. if userInfo == nil || userInfo.User == nil {
  37. ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.USER_NOT_FOUND, nil))
  38. return
  39. }
  40. if len(userInfo.Roles) == 0 {
  41. ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.USER_HAS_NO_ROLE, nil))
  42. return
  43. }
  44. jwtToken, e := getAccessToken(*userInfo)
  45. if e != nil {
  46. ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, e.Error(), nil))
  47. }
  48. ctx.JSON(http.StatusOK, model.RspToken{
  49. TenantId: userInfo.TenantId,
  50. UserId: strconv.FormatInt(userInfo.ID, 64),
  51. DeptId: userInfo.DeptId,
  52. PostId: userInfo.PostId,
  53. RoleId: userInfo.RoleId,
  54. OauthId: userInfo.OauthId,
  55. Account: userInfo.Account,
  56. UserName: userInfo.Name,
  57. NickName: userInfo.RealName,
  58. RoleName: userInfo.Roles[0],
  59. Avatar: userInfo.Avatar,
  60. AccessToken: jwtToken,
  61. RefreshToken: getRefreshToken(*userInfo),
  62. TokenType: model.BEARER,
  63. ExpiresIn: 7200,
  64. License: "",
  65. })
  66. }
  67. //checkLock 校验用户登录失败次数
  68. func checkLock() {
  69. }
  70. func getAccessToken(info model.UserInfo) (string, error) {
  71. claims := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
  72. model.Iss: "issuser",
  73. model.Aud: "audience",
  74. model.TokenType: "access_token",
  75. model.ClientId: "saber",
  76. model.TenantId: info.TenantId,
  77. model.RoleId: info.RoleId,
  78. model.RoleName: info.Roles[0],
  79. model.UserId: info.ID,
  80. model.DeptId: info.DeptId,
  81. model.PostId: info.PostId,
  82. model.OauthID: info.OauthId,
  83. model.Account: info.Account,
  84. model.UserName: info.Account,
  85. model.NickName: info.RealName,
  86. model.Exp: time.Now().Add(2 * time.Hour).Unix(),
  87. model.Nbf: time.Now().Unix(),
  88. })
  89. return claims.SigningString()
  90. }
  91. func getRefreshToken(info model.UserInfo) string {
  92. claims := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
  93. model.Iss: "issuser",
  94. model.Aud: "audience",
  95. model.ClientId: "saber",
  96. model.TokenType: "refresh_token",
  97. model.UserId: info.ID,
  98. model.Exp: time.Now().Add(7 * 24 * time.Hour).Unix(),
  99. model.Nbf: time.Now().Unix(),
  100. })
  101. token, _ := claims.SigningString()
  102. return token
  103. }
  104. func grant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.Errors) {
  105. info := &model.UserInfo{}
  106. key := ctx.GetHeader(model.CAPTCHA_HEADER_KEY)
  107. code := ctx.GetHeader(model.CAPTCHA_HEADER_CODE)
  108. // 获取验证码
  109. redisCode := util.Redis.Get(model.CAPTCHA_KEY + key).String()
  110. // 判断验证码
  111. if config.Instance().Server.CodeEnable && (code == "" || !strings.EqualFold(redisCode, code)) {
  112. return nil, util.NormalResponse(http.StatusBadRequest, model.CAPTCHA_NOT_CORRECT, nil)
  113. }
  114. if token.UserName != "" && token.Password != "" {
  115. // 获取租户信息
  116. //Tenant tenant = tenantService.getByTenantId(tenantId);
  117. //if (TokenUtil.judgeTenant(tenant)) {
  118. // throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
  119. //}
  120. // 获取用户类型
  121. // 根据不同用户类型调用对应的接口返回数据,用户可自行拓展
  122. // info.User = userService.GetUser(token.tenantId, token.UserName, token.password)
  123. }
  124. return info, nil
  125. }