@@ -19,12 +19,12 @@ package interceptors
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "github.com/golang-jwt/jwt"
23
- "github.com/rs/zerolog/log"
24
22
23
+ "github.com/golang-jwt/jwt"
25
24
"github.com/napptive/nerrors/pkg/nerrors"
26
25
"github.com/napptive/njwt/pkg/config"
27
26
"github.com/napptive/njwt/pkg/njwt"
27
+ "github.com/rs/zerolog/log"
28
28
"google.golang.org/grpc"
29
29
"google.golang.org/grpc/metadata"
30
30
)
@@ -114,3 +114,34 @@ func authorizeZoneAwareJWTToken(ctx context.Context, config config.JWTConfig, se
114
114
115
115
return claim , nil
116
116
}
117
+
118
+ // WithZoneAwareJWTStreamInterceptor creates a gRPC stream interceptor that verifies if the JWT received is
119
+ // // valid attending to the zone that issued it.
120
+ func WithZoneAwareJWTStreamInterceptor (config config.JWTConfig , secretProvider SecretProvider ) grpc.ServerOption {
121
+ return grpc .StreamInterceptor (ZoneAwareJWTStreamInterceptor (config , secretProvider ))
122
+ }
123
+
124
+ // ZoneAwareJWTStreamInterceptor verifies the JWT token and adds the claim information in the context
125
+ func ZoneAwareJWTStreamInterceptor (config config.JWTConfig , secretProvider SecretProvider ) grpc.StreamServerInterceptor {
126
+ return func (srv interface {},
127
+ stream grpc.ServerStream ,
128
+ info * grpc.StreamServerInfo ,
129
+ handler grpc.StreamHandler ) error {
130
+
131
+ ctx := stream .Context ()
132
+ authClaim , err := authorizeZoneAwareJWTToken (ctx , config , secretProvider )
133
+ if err != nil {
134
+ return nerrors .FromError (err ).ToGRPC ()
135
+ }
136
+
137
+ // add the claim information to the context metadata
138
+ newCtx , err := AddClaimToContext (authClaim , ctx )
139
+ if err != nil {
140
+ return err
141
+ }
142
+ // uses this new context in the stream wrapper
143
+ w := newStreamContextWrapper (stream )
144
+ w .SetContext (newCtx )
145
+ return handler (srv , w )
146
+ }
147
+ }
0 commit comments