|
7 | 7 | import io.github.majusko.grpc.jwt.data.AuthContextData; |
8 | 8 | import io.github.majusko.grpc.jwt.data.GrpcHeader; |
9 | 9 | import io.github.majusko.grpc.jwt.data.GrpcJwtContext; |
10 | | -import io.github.majusko.grpc.jwt.interceptor.*; |
| 10 | +import io.github.majusko.grpc.jwt.interceptor.AllowedCollector; |
| 11 | +import io.github.majusko.grpc.jwt.interceptor.AuthClientInterceptor; |
| 12 | +import io.github.majusko.grpc.jwt.interceptor.AuthServerInterceptor; |
11 | 13 | import io.github.majusko.grpc.jwt.interceptor.proto.Example; |
12 | 14 | import io.github.majusko.grpc.jwt.interceptor.proto.ExampleServiceGrpc; |
13 | 15 | import io.github.majusko.grpc.jwt.service.GrpcRole; |
@@ -317,7 +319,7 @@ public void testEmptyUserIdInToken() throws IOException { |
317 | 319 | } |
318 | 320 |
|
319 | 321 | @Test |
320 | | - public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException, |
| 322 | + public void testExpiredInternalToken() throws IOException, NoSuchFieldException, IllegalAccessException, |
321 | 323 | NoSuchMethodException, InvocationTargetException, InterruptedException { |
322 | 324 |
|
323 | 325 | final GrpcJwtProperties customProperties = new GrpcJwtProperties(); |
@@ -351,6 +353,39 @@ public void testExpiredToken() throws IOException, NoSuchFieldException, Illegal |
351 | 353 | refreshMethod.invoke(jwtService); |
352 | 354 | } |
353 | 355 |
|
| 356 | + @Test |
| 357 | + public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException { |
| 358 | + |
| 359 | + final GrpcJwtProperties customProperties = new GrpcJwtProperties(); |
| 360 | + final Field field = customProperties.getClass().getDeclaredField("expirationSec"); |
| 361 | + field.setAccessible(true); |
| 362 | + field.set(customProperties, -10L); |
| 363 | + |
| 364 | + final JwtService customJwtService = new JwtService(environment, customProperties); |
| 365 | + final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN))); |
| 366 | + |
| 367 | + final ManagedChannel channel = initTestServer(new ExampleService()); |
| 368 | + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); |
| 369 | + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); |
| 370 | + |
| 371 | + final Metadata header = new Metadata(); |
| 372 | + header.put(GrpcHeader.AUTHORIZATION, token); |
| 373 | + |
| 374 | + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); |
| 375 | + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() |
| 376 | + .setUserId("other-user-id").build(); |
| 377 | + |
| 378 | + Status status = Status.OK; |
| 379 | + |
| 380 | + try { |
| 381 | + final Empty ignore = injectedStub.getExample(request); |
| 382 | + } catch (StatusRuntimeException e) { |
| 383 | + status = e.getStatus(); |
| 384 | + } |
| 385 | + |
| 386 | + Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); |
| 387 | + } |
| 388 | + |
354 | 389 | @Test |
355 | 390 | public void testEmptyOwnerFieldInAnnotationSoRolesAreValidated() throws IOException { |
356 | 391 | final String token = jwtService |
|
0 commit comments