mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
ae87114282
* chore: change interface{} to any * chore: update goctl version to 1.5.0 * chore: update goctl deps
33 lines
1003 B
Go
33 lines
1003 B
Go
package serverinterceptors
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zeromicro/go-zero/zrpc/internal/auth"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// StreamAuthorizeInterceptor returns a func that uses given authenticator in processing stream requests.
|
|
func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor {
|
|
return func(svr any, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
|
handler grpc.StreamHandler) error {
|
|
if err := authenticator.Authenticate(stream.Context()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return handler(svr, stream)
|
|
}
|
|
}
|
|
|
|
// UnaryAuthorizeInterceptor returns a func that uses given authenticator in processing unary requests.
|
|
func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (any, error) {
|
|
if err := authenticator.Authenticate(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return handler(ctx, req)
|
|
}
|
|
}
|