11package io .github .majusko .grpc .jwt ;
22
3+ import com .google .common .collect .Sets ;
34import com .google .protobuf .Empty ;
45import io .github .majusko .grpc .jwt .annotation .Allow ;
56import io .github .majusko .grpc .jwt .annotation .Exposed ;
2223import org .lognet .springboot .grpc .GRpcService ;
2324import org .springframework .beans .factory .annotation .Autowired ;
2425import org .springframework .boot .test .context .SpringBootTest ;
26+ import org .springframework .core .env .Environment ;
2527import org .springframework .test .context .ActiveProfiles ;
2628import org .springframework .test .context .junit4 .SpringRunner ;
2729
2830import java .io .IOException ;
31+ import java .lang .reflect .Field ;
2932
3033@ RunWith (SpringRunner .class )
3134@ SpringBootTest
3235@ ActiveProfiles ("test" )
3336public class GrpcJwtSpringBootStarterApplicationTest {
3437
38+ @ Autowired
39+ private Environment environment ;
40+
3541 @ Autowired
3642 private JwtService jwtService ;
3743
@@ -210,6 +216,155 @@ public void testExposeAnnotationWithMissingInterceptor() throws IOException {
210216 Assert .assertEquals (Status .PERMISSION_DENIED .getCode (), status .getCode ());
211217 }
212218
219+ @ Test
220+ public void testSuccessExposeToTestEnvAnnotation () throws IOException {
221+ final ManagedChannel channel = initTestServer (new ExampleService ());
222+ final Channel interceptedChannel = ClientInterceptors .intercept (channel , authClientInterceptor );
223+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (interceptedChannel );
224+
225+ final Empty response = stub .listExample (Example .GetExampleRequest .newBuilder ().build ());
226+
227+ Assert .assertNotNull (response );
228+ }
229+
230+ @ Test
231+ public void testNonExistingFieldInPayload () throws IOException {
232+ final ManagedChannel channel = initTestServer (new ExampleService ());
233+ final Channel interceptedChannel = ClientInterceptors .intercept (channel , authClientInterceptor );
234+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (interceptedChannel );
235+
236+ Status status = Status .OK ;
237+
238+ try {
239+ final Empty ignored = stub .saveExample (Empty .getDefaultInstance ());
240+ } catch (StatusRuntimeException e ) {
241+ status = e .getStatus ();
242+ }
243+
244+ Assert .assertEquals (Status .PERMISSION_DENIED .getCode (), status .getCode ());
245+ }
246+
247+ @ Test
248+ public void testDiffUserIdAndNonExistingRole () throws IOException {
249+ final ManagedChannel channel = initTestServer (new ExampleService ());
250+ final Channel interceptedChannel = ClientInterceptors .intercept (channel , authClientInterceptor );
251+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (interceptedChannel );
252+
253+ Status status = Status .OK ;
254+
255+ try {
256+ final Empty ignored = stub .deleteExample (Example .GetExampleRequest .getDefaultInstance ());
257+ } catch (StatusRuntimeException e ) {
258+ status = e .getStatus ();
259+ }
260+
261+ Assert .assertEquals (Status .PERMISSION_DENIED .getCode (), status .getCode ());
262+ }
263+
264+ @ Test
265+ public void testCustomTokenWithEmptyUserIdAndEmptyRoles () throws IOException {
266+ final String token = jwtService .generate (new JwtData ("random-user-id" , Sets .newHashSet ()));
267+
268+ final ManagedChannel channel = initTestServer (new ExampleService ());
269+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (channel );
270+
271+ final Metadata header = new Metadata ();
272+ header .put (GrpcHeader .AUTHORIZATION , token );
273+
274+ final ExampleServiceGrpc .ExampleServiceBlockingStub injectedStub = MetadataUtils .attachHeaders (stub , header );
275+ final Example .GetExampleRequest request = Example .GetExampleRequest .newBuilder ()
276+ .setUserId ("other-user-id" ).build ();
277+
278+ Status status = Status .OK ;
279+
280+ try {
281+ final Empty ignore = injectedStub .getExample (request );
282+ } catch (StatusRuntimeException e ) {
283+ status = e .getStatus ();
284+ }
285+
286+ Assert .assertEquals (Status .PERMISSION_DENIED .getCode (), status .getCode ());
287+ }
288+
289+ @ Test
290+ public void testEmptyUserIdInToken () throws IOException {
291+ final String token = jwtService .generate (new JwtData ("" , Sets .newHashSet (ExampleService .ADMIN )));
292+
293+ final ManagedChannel channel = initTestServer (new ExampleService ());
294+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (channel );
295+
296+ final Metadata header = new Metadata ();
297+ header .put (GrpcHeader .AUTHORIZATION , token );
298+
299+ final ExampleServiceGrpc .ExampleServiceBlockingStub injectedStub = MetadataUtils .attachHeaders (stub , header );
300+ final Example .GetExampleRequest request = Example .GetExampleRequest .newBuilder ()
301+ .setUserId ("other-user-id" ).build ();
302+
303+ Status status = Status .OK ;
304+
305+ try {
306+ final Empty ignore = injectedStub .getExample (request );
307+ } catch (StatusRuntimeException e ) {
308+ status = e .getStatus ();
309+ }
310+
311+ Assert .assertEquals (Status .PERMISSION_DENIED .getCode (), status .getCode ());
312+ }
313+
314+ @ Test
315+ public void testExpiredToken () throws IOException , NoSuchFieldException , IllegalAccessException {
316+
317+ final GrpcJwtProperties customProperties = new GrpcJwtProperties ();
318+ final Field field = customProperties .getClass ().getDeclaredField ("expirationSec" );
319+ field .setAccessible (true );
320+ field .set (customProperties , -10L );
321+
322+
323+ final JwtService customJwtService = new JwtService (environment , customProperties );
324+ final String token = customJwtService .generate (new JwtData ("lala" , Sets .newHashSet (ExampleService .ADMIN )));
325+
326+ final ManagedChannel channel = initTestServer (new ExampleService ());
327+ final Channel interceptedChannel = ClientInterceptors .intercept (channel , authClientInterceptor );
328+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (interceptedChannel );
329+
330+ final Metadata header = new Metadata ();
331+ header .put (GrpcHeader .AUTHORIZATION , token );
332+
333+ final ExampleServiceGrpc .ExampleServiceBlockingStub injectedStub = MetadataUtils .attachHeaders (stub , header );
334+ final Example .GetExampleRequest request = Example .GetExampleRequest .newBuilder ()
335+ .setUserId ("other-user-id" ).build ();
336+
337+ Status status = Status .OK ;
338+
339+ try {
340+ final Empty ignore = injectedStub .getExample (request );
341+ } catch (StatusRuntimeException e ) {
342+ status = e .getStatus ();
343+ }
344+
345+ Assert .assertEquals (Status .UNAUTHENTICATED .getCode (), status .getCode ());
346+ }
347+
348+ @ Test
349+ public void testEmptyOwnerFieldInAnnotationSoRolesAreValidated () throws IOException {
350+ final String token = jwtService
351+ .generate (new JwtData ("random-user-id" , Sets .newHashSet (ExampleService .ADMIN )));
352+
353+ final ManagedChannel channel = initTestServer (new ExampleService ());
354+ final ExampleServiceGrpc .ExampleServiceBlockingStub stub = ExampleServiceGrpc .newBlockingStub (channel );
355+
356+ final Metadata header = new Metadata ();
357+ header .put (GrpcHeader .AUTHORIZATION , token );
358+
359+ final ExampleServiceGrpc .ExampleServiceBlockingStub injectedStub = MetadataUtils .attachHeaders (stub , header );
360+ final Example .GetExampleRequest request = Example .GetExampleRequest .newBuilder ()
361+ .setUserId ("other-user-id" ).build ();
362+
363+ final Empty response = injectedStub .someAction (request );
364+
365+ Assert .assertNotNull (response );
366+ }
367+
213368 private ManagedChannel initTestServer (BindableService service ) throws IOException {
214369
215370 final String serverName = InProcessServerBuilder .generateName ();
@@ -230,7 +385,7 @@ private ManagedChannel initTestServer(BindableService service) throws IOExceptio
230385@ GRpcService
231386class ExampleService extends ExampleServiceGrpc .ExampleServiceImplBase {
232387
233- private static final String ADMIN = "admin" ;
388+ public static final String ADMIN = "admin" ;
234389
235390 @ Override
236391 @ Allow (ownerField = "userId" , roles = {GrpcRole .INTERNAL , ADMIN })
@@ -257,4 +412,28 @@ public void listExample(Example.GetExampleRequest request, StreamObserver<Empty>
257412 response .onNext (Empty .getDefaultInstance ());
258413 response .onCompleted ();
259414 }
415+
416+ @ Override
417+ @ Allow (ownerField = "nonExistingField" )
418+ public void saveExample (Empty request , StreamObserver <Empty > response ) {
419+
420+ response .onNext (Empty .getDefaultInstance ());
421+ response .onCompleted ();
422+ }
423+
424+ @ Override
425+ @ Allow (ownerField = "userId" )
426+ public void deleteExample (Example .GetExampleRequest request , StreamObserver <Empty > response ) {
427+
428+ response .onNext (Empty .getDefaultInstance ());
429+ response .onCompleted ();
430+ }
431+
432+ @ Override
433+ @ Allow (ownerField = "" , roles = {ADMIN })
434+ public void someAction (Example .GetExampleRequest request , StreamObserver <Empty > response ) {
435+
436+ response .onNext (Empty .getDefaultInstance ());
437+ response .onCompleted ();
438+ }
260439}
0 commit comments