diff --git a/pom.xml b/pom.xml index c7fa115..7fd6376 100644 --- a/pom.xml +++ b/pom.xml @@ -2,6 +2,13 @@ 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.3.RELEASE + + + io.github.majusko grpc-jwt-spring-boot-starter 1.0.4 @@ -12,7 +19,7 @@ 1.8 1.8 1.8 - 1.24.0 + 1.31.1 1.6.2 0.6.1 @@ -34,42 +41,62 @@ io.jsonwebtoken jjwt-impl - 0.10.5 + 0.11.2 runtime io.jsonwebtoken jjwt-jackson - 0.10.5 + 0.11.2 runtime io.github.lognet grpc-spring-boot-starter - 3.4.3 + 3.5.7 + + + + org.projectlombok + lombok + 1.18.12 + provided + + org.springframework.boot spring-boot-starter-test - 1.5.13.RELEASE test + + + + junit + junit + + + org.junit.vintage + junit-vintage-engine + + + - io.grpc - grpc-testing - ${grpc.version} + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} test - org.projectlombok - lombok - 1.18.12 - provided + io.grpc + grpc-testing + ${grpc.version} + test @@ -116,14 +143,29 @@ org.springframework.boot spring-boot-maven-plugin 2.3.3.RELEASE + + + + repackage + + repackage + + + false + + + + maven-compiler-plugin + 3.8.1 - 1.8 - 1.8 + ${maven.compiler.source} + ${maven.compiler.target} + org.jacoco jacoco-maven-plugin @@ -155,6 +197,17 @@ + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + Apache License, Version 2.0 diff --git a/src/main/java/io/github/majusko/grpc/jwt/data/JwtContextData.java b/src/main/java/io/github/majusko/grpc/jwt/data/JwtContextData.java index 178617d..c6098ff 100644 --- a/src/main/java/io/github/majusko/grpc/jwt/data/JwtContextData.java +++ b/src/main/java/io/github/majusko/grpc/jwt/data/JwtContextData.java @@ -1,6 +1,6 @@ package io.github.majusko.grpc.jwt.data; -import io.jsonwebtoken.Claims; +import io.jsonwebtoken.*; import lombok.AllArgsConstructor; import java.util.Set; diff --git a/src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java b/src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java index 08709c8..f41bd63 100644 --- a/src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java +++ b/src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java @@ -4,9 +4,9 @@ import com.google.protobuf.Empty; import io.github.majusko.grpc.jwt.annotation.Allow; import io.github.majusko.grpc.jwt.annotation.Exposed; -import io.github.majusko.grpc.jwt.data.JwtContextData; import io.github.majusko.grpc.jwt.data.GrpcHeader; import io.github.majusko.grpc.jwt.data.GrpcJwtContext; +import io.github.majusko.grpc.jwt.data.JwtContextData; import io.github.majusko.grpc.jwt.interceptor.AllowedCollector; import io.github.majusko.grpc.jwt.interceptor.AuthClientInterceptor; import io.github.majusko.grpc.jwt.interceptor.AuthServerInterceptor; @@ -21,459 +21,461 @@ import io.grpc.stub.MetadataUtils; import io.grpc.stub.StreamObserver; import io.grpc.testing.GrpcCleanupRule; -import org.junit.Assert; import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.lognet.springboot.grpc.GRpcService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -@RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("test") public class GrpcJwtSpringBootStarterApplicationTest { - @Autowired - private Environment environment; - - @Autowired - private JwtService jwtService; + @Autowired + private Environment environment; - @Autowired - private AllowedCollector allowedCollector; + @Autowired + private JwtService jwtService; - @Autowired - private AuthServerInterceptor authServerInterceptor; + @Autowired + private AllowedCollector allowedCollector; - @Autowired - private AuthClientInterceptor authClientInterceptor; + @Autowired + private AuthServerInterceptor authServerInterceptor; - @Rule - public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); + @Autowired + private AuthClientInterceptor authClientInterceptor; - @Test - public void testSuccessInternalToken() throws IOException { + @Rule + public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); - final Empty response = stub.getExample(Example.GetExampleRequest.newBuilder().build()); + @Test + public void testSuccessInternalToken() throws IOException { - Assert.assertNotNull(response); - } + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); + final Empty response = stub.getExample(Example.GetExampleRequest.newBuilder().build()); - @Test - public void testSuccessCustomAdminToken() throws IOException { + Assertions.assertNotNull(response); + } - final String token = jwtService.generate(new JwtData("some-user-id", "admin")); + @Test + public void testSuccessCustomAdminToken() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final String token = jwtService.generate(new JwtData("some-user-id", "admin")); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Empty response = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - Assert.assertNotNull(response); - } + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Empty response = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); - @Test - public void testCustomTokenWithWrongRole() throws IOException { + Assertions.assertNotNull(response); + } - final String token = jwtService.generate(new JwtData("some-user-id", "non-existing-role")); + @Test + public void testCustomTokenWithWrongRole() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final String token = jwtService.generate(new JwtData("some-user-id", "non-existing-role")); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - Status status = Status.OK; + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - try { - final Empty ignored = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + Status status = Status.OK; - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + try { + final Empty ignored = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - @Test - public void testWrongToken() throws IOException { + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - String token = jwtService.generate(new JwtData("some-user-id", "non-existing-role")); + @Test + public void testWrongToken() throws IOException { - token += "crwvvef"; - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + String token = jwtService.generate(new JwtData("some-user-id", "non-existing-role")); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + token += "crwvvef"; + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - Status status = Status.OK; + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - try { - final Empty ignored = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + Status status = Status.OK; - Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); - } + try { + final Empty ignored = injectedStub.getExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } + Assertions.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); + } - @Test - public void testMissingAuth() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + @Test + public void testMissingAuth() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testMissingCredentials() throws IOException { + @Test + public void testMissingCredentials() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testCustomTokenWithWrongRoleButMatchingOwner() throws IOException { - final String ownerUserId = "matching-user-id"; - final String token = jwtService.generate(new JwtData(ownerUserId, "non-existing-role")); + @Test + public void testCustomTokenWithWrongRoleButMatchingOwner() throws IOException { + final String ownerUserId = "matching-user-id"; + final String token = jwtService.generate(new JwtData(ownerUserId, "non-existing-role")); - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId(ownerUserId).build(); - final Empty response = injectedStub.getExample(request); + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId(ownerUserId).build(); + final Empty response = injectedStub.getExample(request); - Assert.assertNotNull(response); - } + Assertions.assertNotNull(response); + } - @Test - public void testAllowAnnotationWithMissingInterceptor() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + @Test + public void testAllowAnnotationWithMissingInterceptor() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.getExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testExposeAnnotationWithMissingInterceptor() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + @Test + public void testExposeAnnotationWithMissingInterceptor() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.listExample(Example.GetExampleRequest.newBuilder().build()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.listExample(Example.GetExampleRequest.newBuilder().build()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testSuccessExposeToTestEnvAnnotation() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); + @Test + public void testSuccessExposeToTestEnvAnnotation() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); - final Empty response = stub.listExample(Example.GetExampleRequest.newBuilder().build()); + final Empty response = stub.listExample(Example.GetExampleRequest.newBuilder().build()); - Assert.assertNotNull(response); - } + Assertions.assertNotNull(response); + } - @Test - public void testNonExistingFieldInPayload() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); + @Test + public void testNonExistingFieldInPayload() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.saveExample(Empty.getDefaultInstance()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.saveExample(Empty.getDefaultInstance()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testDiffUserIdAndNonExistingRole() throws IOException { - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); + @Test + public void testDiffUserIdAndNonExistingRole() throws IOException { + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignored = stub.deleteExample(Example.GetExampleRequest.getDefaultInstance()); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignored = stub.deleteExample(Example.GetExampleRequest.getDefaultInstance()); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testCustomTokenWithEmptyUserIdAndEmptyRoles() throws IOException { - final String token = jwtService.generate(new JwtData("random-user-id", Sets.newHashSet())); + @Test + public void testCustomTokenWithEmptyUserIdAndEmptyRoles() throws IOException { + final String token = jwtService.generate(new JwtData("random-user-id", Sets.newHashSet())); - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId("other-user-id").build(); + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId("other-user-id").build(); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignore = injectedStub.getExample(request); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignore = injectedStub.getExample(request); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testEmptyUserIdInToken() throws IOException { - final String token = jwtService.generate(new JwtData("", Sets.newHashSet("some-other-role"))); - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final Metadata header = new Metadata(); + @Test + public void testEmptyUserIdInToken() throws IOException { + final String token = jwtService.generate(new JwtData("", Sets.newHashSet("some-other-role"))); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + header.put(GrpcHeader.AUTHORIZATION, token); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId("other-user-id").build(); + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId("other-user-id").build(); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignore = injectedStub.getExample(request); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignore = injectedStub.getExample(request); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode()); + } - @Test - public void testExpiredInternalToken() throws IOException, NoSuchFieldException, IllegalAccessException, - NoSuchMethodException, InvocationTargetException, InterruptedException { + @Test + public void testExpiredInternalToken() throws IOException, NoSuchFieldException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, InterruptedException { - final GrpcJwtProperties customProperties = new GrpcJwtProperties(); - final Field field = customProperties.getClass().getDeclaredField("expirationSec"); - field.setAccessible(true); - field.set(customProperties, 1L); + final GrpcJwtProperties customProperties = new GrpcJwtProperties(); + final Field field = customProperties.getClass().getDeclaredField("expirationSec"); + field.setAccessible(true); + field.set(customProperties, 1L); - final Field propertyField = jwtService.getClass().getDeclaredField("properties"); - propertyField.setAccessible(true); - final GrpcJwtProperties existingProperties = (GrpcJwtProperties) propertyField.get(jwtService); - propertyField.set(jwtService, customProperties); + final Field propertyField = jwtService.getClass().getDeclaredField("properties"); + propertyField.setAccessible(true); + final GrpcJwtProperties existingProperties = (GrpcJwtProperties) propertyField.get(jwtService); + propertyField.set(jwtService, customProperties); - final Method refreshMethod = jwtService.getClass().getDeclaredMethod("refreshInternalToken"); - refreshMethod.setAccessible(true); + final Method refreshMethod = jwtService.getClass().getDeclaredMethod("refreshInternalToken"); + refreshMethod.setAccessible(true); - refreshMethod.invoke(jwtService); + refreshMethod.invoke(jwtService); - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId("other-user-id").build(); + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId("other-user-id").build(); - Thread.sleep(2000); + Thread.sleep(2000); - final Empty response = stub.getExample(request); + final Empty response = stub.getExample(request); - Assert.assertNotNull(response); + Assertions.assertNotNull(response); - propertyField.set(jwtService, existingProperties); - refreshMethod.invoke(jwtService); - } + propertyField.set(jwtService, existingProperties); + refreshMethod.invoke(jwtService); + } - @Test - public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException { + @Test + public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException { - final GrpcJwtProperties customProperties = new GrpcJwtProperties(); - final Field field = customProperties.getClass().getDeclaredField("expirationSec"); - field.setAccessible(true); - field.set(customProperties, -10L); + final GrpcJwtProperties customProperties = new GrpcJwtProperties(); + final Field field = customProperties.getClass().getDeclaredField("expirationSec"); + field.setAccessible(true); + field.set(customProperties, -10L); - final JwtService customJwtService = new JwtService(environment, customProperties); - final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN))); + final JwtService customJwtService = new JwtService(environment, customProperties); + final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN))); - final ManagedChannel channel = initTestServer(new ExampleService()); - final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); + final ManagedChannel channel = initTestServer(new ExampleService()); + final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = + ExampleServiceGrpc.newBlockingStub(interceptedChannel); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId("other-user-id").build(); + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId("other-user-id").build(); - Status status = Status.OK; + Status status = Status.OK; - try { - final Empty ignore = injectedStub.getExample(request); - } catch (StatusRuntimeException e) { - status = e.getStatus(); - } + try { + final Empty ignore = injectedStub.getExample(request); + } catch (StatusRuntimeException e) { + status = e.getStatus(); + } - Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); - } + Assertions.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); + } - @Test - public void testMissingOwnerFieldInAnnotationSoRolesAreValidated() throws IOException { - final String token = jwtService - .generate(new JwtData("random-user-id", Sets.newHashSet(ExampleService.ADMIN))); + @Test + public void testMissingOwnerFieldInAnnotationSoRolesAreValidated() throws IOException { + final String token = jwtService + .generate(new JwtData("random-user-id", Sets.newHashSet(ExampleService.ADMIN))); - final ManagedChannel channel = initTestServer(new ExampleService()); - final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); + final ManagedChannel channel = initTestServer(new ExampleService()); + final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(channel); - final Metadata header = new Metadata(); - header.put(GrpcHeader.AUTHORIZATION, token); + final Metadata header = new Metadata(); + header.put(GrpcHeader.AUTHORIZATION, token); - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); - final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() - .setUserId("other-user-id").build(); + final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); + final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() + .setUserId("other-user-id").build(); - final Empty response = injectedStub.someAction(request); + final Empty response = injectedStub.someAction(request); - Assert.assertNotNull(response); - } + Assertions.assertNotNull(response); + } - private ManagedChannel initTestServer(BindableService service) throws IOException { + private ManagedChannel initTestServer(BindableService service) throws IOException { - final String serverName = InProcessServerBuilder.generateName(); - final Server server = InProcessServerBuilder - .forName(serverName).directExecutor() - .addService(service) - .intercept(authServerInterceptor) - .build().start(); + final String serverName = InProcessServerBuilder.generateName(); + final Server server = InProcessServerBuilder + .forName(serverName).directExecutor() + .addService(service) + .intercept(authServerInterceptor) + .build().start(); - allowedCollector.postProcessBeforeInitialization(service, "exampleService"); + allowedCollector.postProcessBeforeInitialization(service, "exampleService"); - grpcCleanup.register(server); + grpcCleanup.register(server); - return grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()); - } + return grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()); + } } @GRpcService class ExampleService extends ExampleServiceGrpc.ExampleServiceImplBase { - public static final String ADMIN = "admin"; + public static final String ADMIN = "admin"; - @Override - @Allow(ownerField = "userId", roles = {GrpcRole.INTERNAL, ADMIN}) - public void getExample(Example.GetExampleRequest request, StreamObserver response) { + @Override + @Allow(ownerField = "userId", roles = {GrpcRole.INTERNAL, ADMIN}) + public void getExample(Example.GetExampleRequest request, StreamObserver response) { - JwtContextData authContext = GrpcJwtContext.get().orElseThrow(RuntimeException::new); + JwtContextData authContext = GrpcJwtContext.get().orElseThrow(RuntimeException::new); - Assert.assertNotNull(authContext.getJwt()); - Assert.assertTrue(authContext.getJwtClaims().size() > 0); + Assertions.assertNotNull(authContext.getJwt()); + Assertions.assertTrue(authContext.getJwtClaims().size() > 0); - if(!request.getUserId().equals(authContext.getUserId())) { - Assert.assertTrue(authContext.getRoles().stream() - .anyMatch($ -> $.equals(GrpcRole.INTERNAL) || $.equals(ADMIN))); - } + if (!request.getUserId().equals(authContext.getUserId())) { + Assertions.assertTrue(authContext.getRoles().stream() + .anyMatch($ -> $.equals(GrpcRole.INTERNAL) || $.equals(ADMIN))); + } - response.onNext(Empty.getDefaultInstance()); - response.onCompleted(); - } + response.onNext(Empty.getDefaultInstance()); + response.onCompleted(); + } - @Override - @Exposed(environments = "test") - public void listExample(Example.GetExampleRequest request, StreamObserver response) { + @Override + @Exposed(environments = "test") + public void listExample(Example.GetExampleRequest request, StreamObserver response) { - response.onNext(Empty.getDefaultInstance()); - response.onCompleted(); - } + response.onNext(Empty.getDefaultInstance()); + response.onCompleted(); + } - @Override - @Allow(ownerField = "nonExistingField") - public void saveExample(Empty request, StreamObserver response) { + @Override + @Allow(ownerField = "nonExistingField") + public void saveExample(Empty request, StreamObserver response) { - response.onNext(Empty.getDefaultInstance()); - response.onCompleted(); - } + response.onNext(Empty.getDefaultInstance()); + response.onCompleted(); + } - @Override - @Allow(ownerField = "userId") - public void deleteExample(Example.GetExampleRequest request, StreamObserver response) { + @Override + @Allow(ownerField = "userId") + public void deleteExample(Example.GetExampleRequest request, StreamObserver response) { - response.onNext(Empty.getDefaultInstance()); - response.onCompleted(); - } + response.onNext(Empty.getDefaultInstance()); + response.onCompleted(); + } - @Override - @Allow(roles = {ADMIN}) - public void someAction(Example.GetExampleRequest request, StreamObserver response) { + @Override + @Allow(roles = {ADMIN}) + public void someAction(Example.GetExampleRequest request, StreamObserver response) { - response.onNext(Empty.getDefaultInstance()); - response.onCompleted(); - } + response.onNext(Empty.getDefaultInstance()); + response.onCompleted(); + } } \ No newline at end of file