github.com/lingyao2333/mo-zero@v1.4.1/zrpc/internal/serverinterceptors/authinterceptor.go (about) 1 package serverinterceptors 2 3 import ( 4 "context" 5 6 "github.com/lingyao2333/mo-zero/zrpc/internal/auth" 7 "google.golang.org/grpc" 8 ) 9 10 // StreamAuthorizeInterceptor returns a func that uses given authenticator in processing stream requests. 11 func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor { 12 return func(svr interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, 13 handler grpc.StreamHandler) error { 14 if err := authenticator.Authenticate(stream.Context()); err != nil { 15 return err 16 } 17 18 return handler(svr, stream) 19 } 20 } 21 22 // UnaryAuthorizeInterceptor returns a func that uses given authenticator in processing unary requests. 23 func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor { 24 return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, 25 handler grpc.UnaryHandler) (interface{}, error) { 26 if err := authenticator.Authenticate(ctx); err != nil { 27 return nil, err 28 } 29 30 return handler(ctx, req) 31 } 32 }