authinterceptor.go 1023 B

1234567891011121314151617181920212223242526272829303132
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "git.i2edu.net/i2/go-zero/zrpc/internal/auth"
  5. "google.golang.org/grpc"
  6. )
  7. // StreamAuthorizeInterceptor returns a func that uses given authenticator in processing stream requests.
  8. func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor {
  9. return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
  10. handler grpc.StreamHandler) error {
  11. if err := authenticator.Authenticate(stream.Context()); err != nil {
  12. return err
  13. }
  14. return handler(srv, stream)
  15. }
  16. }
  17. // UnaryAuthorizeInterceptor returns a func that uses given authenticator in processing unary requests.
  18. func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor {
  19. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  20. handler grpc.UnaryHandler) (interface{}, error) {
  21. if err := authenticator.Authenticate(ctx); err != nil {
  22. return nil, err
  23. }
  24. return handler(ctx, req)
  25. }
  26. }