ApiKeyAuthenticationHandler implements AuthenticationHandler, InitializingBean { @Override public boolean canHandle(AuthenticationContext context) { final String apiKey = readApiKey(context.request()); if (apiKey == null) { return false; } if (apiKeyRepository != null) { if (context.get(APIKEY_CONTEXT_ATTRIBUTE) == null) { try { final Optional<ApiKey> optApiKey = apiKeyRepository.findById(apiKey); if (optApiKey.isPresent()) { context.request().metrics().setSecurityType(API_KEY); context.request().metrics().setSecurityToken(apiKey); } context.set(APIKEY_CONTEXT_ATTRIBUTE, optApiKey); } catch (TechnicalException e) { } } } return true; }  @Override void afterPropertiesSet(); @Override boolean canHandle(AuthenticationContext context); @Override String name(); @Override int order(); @Override List<AuthenticationPolicy> handle(ExecutionContext executionContext);  }
@Test public void shouldNotHandleRequest() { when(authenticationContext.request()).thenReturn(request); when(request.headers()).thenReturn(new HttpHeaders()); MultiValueMap<String, String> parameters = mock(MultiValueMap.class); when(request.parameters()).thenReturn(parameters); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertFalse(handle); }
@Test public void shouldHandleRequestUsingHeaders() throws TechnicalException { when(authenticationContext.request()).thenReturn(request); when(request.metrics()).thenReturn(metrics); HttpHeaders headers = new HttpHeaders(); headers.set("X-Gravitee-Api-Key", "xxxxx-xxxx-xxxxx"); when(request.headers()).thenReturn(headers); when(apiKeyRepository.findById("xxxxx-xxxx-xxxxx")).thenReturn(of(new ApiKey())); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); verify(metrics).setSecurityType(SecurityType.API_KEY); verify(metrics).setSecurityToken("xxxxx-xxxx-xxxxx"); }
@Test public void shouldHandleRequestUsingQueryParameters() throws TechnicalException { when(authenticationContext.request()).thenReturn(request); when(request.metrics()).thenReturn(metrics); MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); parameters.put("api-key", Collections.singletonList("xxxxx-xxxx-xxxxx")); when(request.parameters()).thenReturn(parameters); when(apiKeyRepository.findById("xxxxx-xxxx-xxxxx")).thenReturn(of(new ApiKey())); HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); verify(metrics).setSecurityType(SecurityType.API_KEY); verify(metrics).setSecurityToken("xxxxx-xxxx-xxxxx"); }
@Test public void shouldHandleRequestUsingQueryParameters_emptyApiKey() { when(authenticationContext.request()).thenReturn(request); MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); parameters.put("api-key", Collections.singletonList("")); when(request.parameters()).thenReturn(parameters); HttpHeaders headers = new HttpHeaders(); when(request.headers()).thenReturn(headers); boolean handle = authenticationHandler.canHandle(authenticationContext); Assert.assertTrue(handle); }