Skip to content

Commit ed8500d

Browse files
committed
grpc: standardize auth interceptor
1 parent c7215a2 commit ed8500d

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

net/grpc/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func MakeJWTInterceptors(jwksURL string, tls *tls.Config, claims ClaimsFunc, whi
4040
return MakeAuthInterceptors(ValidateJWT(jwksURL, tls, claims), whiteListedMethods...)
4141
}
4242

43-
func (f AuthInterceptors) Unary() grpc.ServerOption {
43+
func (f AuthInterceptors) Unary() grpc.UnaryServerInterceptor {
4444
return UnaryServerInterceptor(f.authFunc)
4545
}
46-
func (f AuthInterceptors) Stream() grpc.ServerOption {
46+
func (f AuthInterceptors) Stream() grpc.StreamServerInterceptor {
4747
return StreamServerInterceptor(f.authFunc)
4848
}
4949

net/grpc/interceptor.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@ import (
99

1010
type Interceptor = func(ctx context.Context, method string) (context.Context, error)
1111

12-
func UnaryServerInterceptor(intercept Interceptor) grpc.ServerOption {
13-
return grpc.UnaryInterceptor(
14-
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
15-
newCtx, err := intercept(ctx, info.FullMethod)
16-
if err != nil {
17-
return nil, err
18-
}
19-
return handler(newCtx, req)
20-
})
12+
func UnaryServerInterceptorOption(intercept Interceptor) grpc.ServerOption {
13+
return grpc.UnaryInterceptor(UnaryServerInterceptor(intercept))
2114
}
2215

23-
func StreamServerInterceptor(intercept Interceptor) grpc.ServerOption {
24-
return grpc.StreamInterceptor(
25-
func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
26-
newCtx, err := intercept(stream.Context(), info.FullMethod)
27-
if err != nil {
28-
return err
29-
}
30-
wrapped := grpc_middleware.WrapServerStream(stream)
31-
wrapped.WrappedContext = newCtx
32-
return handler(srv, wrapped)
33-
})
16+
func StreamServerInterceptorOption(intercept Interceptor) grpc.ServerOption {
17+
return grpc.StreamInterceptor(StreamServerInterceptor(intercept))
18+
}
19+
20+
// StreamServerInterceptor returns a new unary server interceptors that performs per-request auth.
21+
func StreamServerInterceptor(intercept Interceptor) grpc.StreamServerInterceptor {
22+
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
23+
newCtx, err := intercept(stream.Context(), info.FullMethod)
24+
if err != nil {
25+
return err
26+
}
27+
wrapped := grpc_middleware.WrapServerStream(stream)
28+
wrapped.WrappedContext = newCtx
29+
return handler(srv, wrapped)
30+
}
31+
}
32+
33+
// UnaryServerInterceptor returns a new unary server interceptors that performs per-request auth.
34+
func UnaryServerInterceptor(intercept Interceptor) grpc.UnaryServerInterceptor {
35+
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
36+
newCtx, err := intercept(ctx, info.FullMethod)
37+
if err != nil {
38+
return nil, err
39+
}
40+
return handler(newCtx, req)
41+
}
3442
}

net/grpc/interceptor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestUnaryInterceptor(t *testing.T) {
1212
m := &MockInterceptor{}
13-
svr := StubGrpcServer(UnaryServerInterceptor(m.Intercept))
13+
svr := StubGrpcServer(UnaryServerInterceptorOption(m.Intercept))
1414
defer svr.Close()
1515
go svr.Serve()
1616

@@ -21,7 +21,7 @@ func TestUnaryInterceptor(t *testing.T) {
2121

2222
func TestStreamInterceptor(t *testing.T) {
2323
m := &MockInterceptor{}
24-
svr := StubGrpcServer(StreamServerInterceptor(m.Intercept))
24+
svr := StubGrpcServer(StreamServerInterceptorOption(m.Intercept))
2525
defer svr.Close()
2626
go svr.Serve()
2727

0 commit comments

Comments
 (0)