auth_interceptor.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package auth_jwt
  2. import (
  3. "context"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/os/glog"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/metadata"
  9. "google.golang.org/grpc/status"
  10. "sort"
  11. )
  12. // AuthInterceptor is a server interceptor for authentication and authorization
  13. type AuthInterceptor struct {
  14. jwtManager *JWTManager
  15. accessibleRoles map[string][]string
  16. }
  17. // NewAuthInterceptor returns a new auth interceptor
  18. func NewAuthInterceptor(jwtManager *JWTManager, accessibleRoles map[string][]string) *AuthInterceptor {
  19. return &AuthInterceptor{jwtManager, accessibleRoles}
  20. }
  21. // Unary returns a server interceptor function to authenticate and authorize unary RPC
  22. func (interceptor *AuthInterceptor) Unary() grpc.UnaryServerInterceptor {
  23. return func(
  24. ctx context.Context,
  25. req interface{},
  26. info *grpc.UnaryServerInfo,
  27. handler grpc.UnaryHandler,
  28. ) (interface{}, error) {
  29. enableJWT := g.Config().GetBool("system.enableJWT")
  30. if !enableJWT {
  31. return handler(ctx, req)
  32. }
  33. //glog.Info("--> unary interceptor: ", info.FullMethod)
  34. noAuthenticationList := g.Config().GetStrings("system.noAuthenticationList")
  35. if IsContains(info.FullMethod, noAuthenticationList) {
  36. return handler(ctx, req)
  37. }
  38. err := interceptor.authorize(ctx, info.FullMethod)
  39. if err != nil {
  40. glog.Error(err)
  41. return nil, err
  42. }
  43. return handler(ctx, req)
  44. }
  45. }
  46. // Stream returns a server interceptor function to authenticate and authorize stream RPC
  47. func (interceptor *AuthInterceptor) Stream() grpc.StreamServerInterceptor {
  48. return func(
  49. srv interface{},
  50. stream grpc.ServerStream,
  51. info *grpc.StreamServerInfo,
  52. handler grpc.StreamHandler,
  53. ) error {
  54. glog.Info("--> stream interceptor: ", info.FullMethod)
  55. enableJWT := g.Config().GetBool("system.enableJWT")
  56. if !enableJWT {
  57. return handler(srv, stream)
  58. }
  59. err := interceptor.authorize(stream.Context(), info.FullMethod)
  60. if err != nil {
  61. glog.Error(err)
  62. return err
  63. }
  64. return handler(srv, stream)
  65. }
  66. }
  67. func (interceptor *AuthInterceptor) authorize(ctx context.Context, method string) error {
  68. accessibleRoles, ok := interceptor.accessibleRoles[method]
  69. md, ok := metadata.FromIncomingContext(ctx)
  70. if !ok {
  71. return status.Errorf(codes.Unauthenticated, "没有提供metadata")
  72. }
  73. values := md["authorization"]
  74. if len(values) == 0 {
  75. return status.Errorf(codes.Unauthenticated, "不是授权的token")
  76. }
  77. accessToken := values[0]
  78. claims, err := interceptor.jwtManager.Verify(accessToken)
  79. if err != nil {
  80. return status.Errorf(codes.Unauthenticated, err.Error())
  81. }
  82. //TODO:角色处理
  83. glog.Info("claims====", claims)
  84. glog.Info("accessibleRoles====", accessibleRoles)
  85. //for _, roles := range accessibleRoles {
  86. // glog.Info(roles)
  87. // //if roles == claims.Roles {
  88. // return nil
  89. // //}
  90. //}
  91. return nil
  92. //return status.Error(codes.PermissionDenied, "没有访问的权限")
  93. }
  94. // IsContains 查找值val是否在数组array中存在
  95. func IsContains(target string, str_array []string) bool {
  96. sort.Strings(str_array)
  97. index := sort.SearchStrings(str_array, target)
  98. if index < len(str_array) && str_array[index] == target {
  99. return true
  100. }
  101. return false
  102. }