auth.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package middleware
  2. import (
  3. "baize_scaffold/gateway/gw/config"
  4. "fmt"
  5. "net/http"
  6. "github.com/pkg/errors"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  8. "git.sxidc.com/go-framework/baize/framework/gateway"
  9. "git.sxidc.com/service-supports/fslog"
  10. "github.com/dgrijalva/jwt-go/request"
  11. )
  12. func Authentication() gateway.Handler {
  13. return func(requestBuilder *gateway.RequestBuilder) {
  14. c := requestBuilder.ApiContext()
  15. respFunc := response.SendMapResponse
  16. token, err := request.AuthorizationHeaderExtractor.ExtractToken(c.Request)
  17. if err != nil {
  18. fslog.Error(err)
  19. respFunc(c, http.StatusUnauthorized, map[string]any{
  20. "accessToken": "",
  21. }, errors.New("token错误"))
  22. c.Abort()
  23. return
  24. }
  25. requestBuilder.Post(&gateway.PostRequest{
  26. Url: config.GetGatewayConfig().ServicesConfig.UMBaseUrl + "/utm/api/v1/checkToken",
  27. Body: map[string]any{
  28. "token": token,
  29. },
  30. }).ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  31. userID, ok := c.GetResultMapValue("userId").(string)
  32. if !ok {
  33. fslog.Error(err)
  34. respFunc(c.ApiContext(), http.StatusUnauthorized, map[string]any{
  35. "accessToken": "",
  36. }, errors.New("用户ID获取错误"))
  37. c.ApiContext().Abort()
  38. }
  39. }).Request()
  40. currentTenant, err := ngumInner.GetUserCurrentTenant(umCheckResult.UserID)
  41. if err != nil {
  42. fslog.Error(err)
  43. respFunc(c, http.StatusUnauthorized, map[string]any{
  44. "code": ErrGetCurOrg.Code(),
  45. "accessToken": "",
  46. }, ErrGetCurOrg)
  47. c.Abort()
  48. return
  49. }
  50. tangentSimpleUserInfo := new(mbum_resp.TangentUserProfileInfo)
  51. tenantStatus := new(ngtm_resp.TenantStatus)
  52. if currentTenant.ID != "" {
  53. tenantStatus, err = ngtmInner.GetTenantStatus(currentTenant.ID)
  54. if err != nil {
  55. fslog.Error(err)
  56. respFunc(c, http.StatusUnauthorized, map[string]any{
  57. "code": ErrGetCurOrg.Code(),
  58. "accessToken": "",
  59. }, ErrGetCurOrgUserInfo)
  60. c.Abort()
  61. return
  62. }
  63. tangentSimpleUserInfo, err = ngumInner.GetTenantUserProfileSimpleByID(currentTenant.ID, umCheckResult.UserID)
  64. if err != nil {
  65. fslog.Error(err)
  66. respFunc(c, http.StatusUnauthorized, map[string]any{
  67. "code": ErrGetCurOrg.Code(),
  68. "accessToken": "",
  69. }, ErrGetCurOrgUserInfo)
  70. c.Abort()
  71. return
  72. }
  73. }
  74. // 2.使用新的uas 直接根据用户id和访问资源信息判断是否有权限。
  75. authInfo, err := uasV1.Authentication(&uasV1Req.AuthenticationJsonBody{
  76. TenantID: currentTenant.ID,
  77. UserID: umCheckResult.UserID,
  78. Namespace: global.Namespace,
  79. Resource: c.FullPath(),
  80. Action: c.Request.Method,
  81. IsExpireStatus: tenantStatus.IsExpireStatus,
  82. })
  83. if err != nil {
  84. fslog.Error(err)
  85. respFunc(c, http.StatusUnauthorized, map[string]any{
  86. "code": ErrAuth.Code(),
  87. "accessToken": "",
  88. }, ErrAuth)
  89. c.Abort()
  90. return
  91. }
  92. if authInfo != nil && !authInfo.Pass {
  93. fslog.Error(fmt.Errorf("path:%s,method:%s", c.Request.URL.Path, c.Request.Method))
  94. respFunc(c, http.StatusUnauthorized, map[string]any{
  95. "code": ErrNoPermission.Code(),
  96. "accessToken": "",
  97. }, ErrNoPermission)
  98. c.Abort()
  99. return
  100. }
  101. // 组合角色
  102. roles := make([]RoleInfo, 0)
  103. for _, sysRole := range authInfo.SysRoles {
  104. roles = append(roles, RoleInfo{
  105. ID: sysRole.ID,
  106. Name: sysRole.Name,
  107. })
  108. }
  109. for _, tangentRole := range authInfo.TenantRoles {
  110. roles = append(roles, RoleInfo{
  111. ID: tangentRole.ID,
  112. Name: tangentRole.Name,
  113. })
  114. }
  115. c.SetUserInfo(&UserInfoWithRoles{
  116. ID: umCheckResult.UserID,
  117. UserName: umCheckResult.UserName,
  118. Updated: umCheckResult.Updated,
  119. ProfileName: tangentSimpleUserInfo.Name,
  120. OrgInfo: currentTenant,
  121. RoleInfos: roles,
  122. TangentUserProfile: tangentSimpleUserInfo,
  123. })
  124. c.SetTenantInfo(currentTenant)
  125. c.Next()
  126. }
  127. }