OAuth2AuthenticationHandler implements AuthenticationHandler { @Override public boolean canHandle(AuthenticationContext context) { String token = readToken(context.request()); if (token == null || token.isEmpty()) { return false; } if (context.get(JWT_CONTEXT_ATTRIBUTE) == null) { context.set(JWT_CONTEXT_ATTRIBUTE, new LazyJwtToken(token)); } return true; }  @Override boolean canHandle(AuthenticationContext context); @Override String name(); @Override int order(); @Override List<AuthenticationPolicy> handle(ExecutionContext executionContext);  }
@Test public void shouldNotHandleRequest_noAuthorizationHeader() { when(request.headers()).thenReturn(new HttpHeaders()); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertFalse(handle); }
@Test public void shouldNotHandleRequest_invalidAuthorizationHeader() { HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); headers.add(HttpHeaders.AUTHORIZATION, ""); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertFalse(handle); }
@Test public void shouldNotHandleRequest_noBearerAuthorizationHeader() { HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); headers.add(HttpHeaders.AUTHORIZATION, "Basic xxx-xx-xxx-xx-xx"); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertFalse(handle); }
@Test public void shouldHandleRequest_validAuthorizationHeader() { HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); headers.add(HttpHeaders.AUTHORIZATION, OAuth2AuthenticationHandler.BEARER_AUTHORIZATION_TYPE + " xxx-xx-xxx-xx-xx"); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); }
@Test public void shouldHandleRequest_ignoreCaseAuthorizationHeader() { HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); headers.add(HttpHeaders.AUTHORIZATION, "BeaRer xxx-xx-xxx-xx-xx"); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); }
@Test public void shouldHandleRequest_validQueryParameter() { LinkedMultiValueMap parameters = new LinkedMultiValueMap(); when(request.parameters()).thenReturn(parameters); parameters.add("access_token", "xxx-xx-xxx-xx-xx"); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); }
@Test public void shouldNotHandleRequest_noBearerValue() { HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); headers.add(HttpHeaders.AUTHORIZATION, OAuth2AuthenticationHandler.BEARER_AUTHORIZATION_TYPE + " "); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertFalse(handle); }