go-zero/zrpc/internal/serverinterceptors/authinterceptor.go

33 lines
1.0 KiB
Go
Raw Normal View History

package serverinterceptors
2020-07-26 17:09:05 +08:00
import (
"context"
2020-09-18 11:41:52 +08:00
"github.com/tal-tech/go-zero/zrpc/internal/auth"
2020-07-26 17:09:05 +08:00
"google.golang.org/grpc"
)
2021-03-01 23:52:44 +08:00
// StreamAuthorizeInterceptor returns a func that uses given authenticator in processing stream requests.
2020-07-26 17:09:05 +08:00
func StreamAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {
if err := authenticator.Authenticate(stream.Context()); err != nil {
return err
}
return handler(srv, stream)
}
}
2021-03-01 23:52:44 +08:00
// UnaryAuthorizeInterceptor returns a func that uses given authenticator in processing unary requests.
2020-07-26 17:09:05 +08:00
func UnaryAuthorizeInterceptor(authenticator *auth.Authenticator) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
if err := authenticator.Authenticate(ctx); err != nil {
return nil, err
}
return handler(ctx, req)
}
}