token.go 3.7 KB

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