You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/serverinterceptors/authinterceptor.go

33 lines
1003 B
Go

package serverinterceptors
4 years ago
import (
"context"
"github.com/zeromicro/go-zero/zrpc/internal/auth"
4 years ago
"google.golang.org/grpc"
)
// StreamAuthorizeInterceptor returns a func that uses given authenticator in processing stream requests.
4 years ago
func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor {
return func(svr any, stream grpc.ServerStream, info *grpc.StreamServerInfo,
4 years ago
handler grpc.StreamHandler) error {
if err := authenticator.Authenticate(stream.Context()); err != nil {
return err
}
return handler(svr, stream)
4 years ago
}
}
// UnaryAuthorizeInterceptor returns a func that uses given authenticator in processing unary requests.
4 years ago
func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (any, error) {
4 years ago
if err := authenticator.Authenticate(ctx); err != nil {
return nil, err
}
return handler(ctx, req)
}
}