Skip to content

Commit 184cc24

Browse files
authored
Merge pull request #28 from napptive/feature/PG-1070_stream_zone_interceptor
PG-1070 Add streaming zone interceptor
2 parents 376fd97 + 8d3bdb9 commit 184cc24

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

pkg/interceptors/jwt_interceptor_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2020 Napptive
2+
* Copyright 2023 Napptive
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package interceptors
1718

1819
import (
@@ -21,11 +22,10 @@ import (
2122
"net"
2223
"time"
2324

24-
"github.com/napptive/njwt/pkg/helper"
25-
2625
grpc_ping_go "github.com/napptive/grpc-ping-go"
2726
"github.com/napptive/nerrors/pkg/nerrors"
2827
"github.com/napptive/njwt/pkg/config"
28+
"github.com/napptive/njwt/pkg/helper"
2929
"github.com/napptive/njwt/pkg/njwt"
3030
"github.com/napptive/njwt/pkg/utils"
3131
"github.com/onsi/ginkgo"

pkg/interceptors/zones.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package interceptors
1919
import (
2020
"context"
2121
"fmt"
22-
"github.com/golang-jwt/jwt"
23-
"github.com/rs/zerolog/log"
2422

23+
"github.com/golang-jwt/jwt"
2524
"github.com/napptive/nerrors/pkg/nerrors"
2625
"github.com/napptive/njwt/pkg/config"
2726
"github.com/napptive/njwt/pkg/njwt"
27+
"github.com/rs/zerolog/log"
2828
"google.golang.org/grpc"
2929
"google.golang.org/grpc/metadata"
3030
)
@@ -114,3 +114,34 @@ func authorizeZoneAwareJWTToken(ctx context.Context, config config.JWTConfig, se
114114

115115
return claim, nil
116116
}
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

Comments
 (0)