authinterceptor.go 816 B

123456789101112131415161718192021222324252627282930
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "github.com/tal-tech/go-zero/zrpc/internal/auth"
  5. "google.golang.org/grpc"
  6. )
  7. func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor {
  8. return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
  9. handler grpc.StreamHandler) error {
  10. if err := authenticator.Authenticate(stream.Context()); err != nil {
  11. return err
  12. }
  13. return handler(srv, stream)
  14. }
  15. }
  16. func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor {
  17. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  18. handler grpc.UnaryHandler) (interface{}, error) {
  19. if err := authenticator.Authenticate(ctx); err != nil {
  20. return nil, err
  21. }
  22. return handler(ctx, req)
  23. }
  24. }