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

33 lines
1003 B
Go
Raw Normal View History

package serverinterceptors
2020-07-26 17:09:05 +08:00
import (
"context"
"github.com/zeromicro/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(svr any, stream grpc.ServerStream, info *grpc.StreamServerInfo,
2020-07-26 17:09:05 +08:00
handler grpc.StreamHandler) error {
if err := authenticator.Authenticate(stream.Context()); err != nil {
return err
}
return handler(svr, stream)
2020-07-26 17:09:05 +08:00
}
}
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 any, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (any, error) {
2020-07-26 17:09:05 +08:00
if err := authenticator.Authenticate(ctx); err != nil {
return nil, err
}
return handler(ctx, req)
}
}