@Test public void shouldAuthenticateClient() throws Exception { Client client = mock(Client.class); when(client.getClientId()).thenReturn("my-client-id"); when(client.getClientSecret()).thenReturn("my-client-secret"); HttpServerRequest httpServerRequest = mock(HttpServerRequest.class); VertxHttpHeaders vertxHttpHeaders = new VertxHttpHeaders(); vertxHttpHeaders.add(HttpHeaders.AUTHORIZATION, "Basic bXktY2xpZW50LWlkOm15LWNsaWVudC1zZWNyZXQ="); when(httpServerRequest.headers()).thenReturn(MultiMap.newInstance(vertxHttpHeaders)); RoutingContext context = mock(RoutingContext.class); when(context.request()).thenReturn(httpServerRequest); CountDownLatch latch = new CountDownLatch(1); authProvider.handle(client, context, clientAsyncResult -> { latch.countDown(); Assert.assertNotNull(clientAsyncResult); Assert.assertNotNull(clientAsyncResult.result()); }); assertTrue(latch.await(10, TimeUnit.SECONDS)); }
@Override public void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler) { final String authorization = getBasicAuthorization(context.request()); if (authorization == null) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", authenticationHeader()))); return; } try { String decoded = new String(Base64.getDecoder().decode(authorization)); int colonIdx = decoded.indexOf(":"); if (colonIdx == -1) { throw new IllegalArgumentException(); } String clientId = decoded.substring(0, colonIdx); String clientSecret = decoded.substring(colonIdx + 1); if (!client.getClientId().equals(clientId) || !client.getClientSecret().equals(clientSecret)) { handler.handle(Future.failedFuture(new InvalidClientException(ClientAuthHandler.GENERIC_ERROR_MESSAGE, authenticationHeader()))); return; } handler.handle(Future.succeededFuture(client)); } catch (RuntimeException e) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", e, authenticationHeader()))); return; } }
ClientBasicAuthProvider implements ClientAuthProvider { @Override public void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler) { final String authorization = getBasicAuthorization(context.request()); if (authorization == null) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", authenticationHeader()))); return; } try { String decoded = new String(Base64.getDecoder().decode(authorization)); int colonIdx = decoded.indexOf(":"); if (colonIdx == -1) { throw new IllegalArgumentException(); } String clientId = decoded.substring(0, colonIdx); String clientSecret = decoded.substring(colonIdx + 1); if (!client.getClientId().equals(clientId) || !client.getClientSecret().equals(clientSecret)) { handler.handle(Future.failedFuture(new InvalidClientException(ClientAuthHandler.GENERIC_ERROR_MESSAGE, authenticationHeader()))); return; } handler.handle(Future.succeededFuture(client)); } catch (RuntimeException e) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", e, authenticationHeader()))); return; } } }
ClientBasicAuthProvider implements ClientAuthProvider { @Override public void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler) { final String authorization = getBasicAuthorization(context.request()); if (authorization == null) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", authenticationHeader()))); return; } try { String decoded = new String(Base64.getDecoder().decode(authorization)); int colonIdx = decoded.indexOf(":"); if (colonIdx == -1) { throw new IllegalArgumentException(); } String clientId = decoded.substring(0, colonIdx); String clientSecret = decoded.substring(colonIdx + 1); if (!client.getClientId().equals(clientId) || !client.getClientSecret().equals(clientSecret)) { handler.handle(Future.failedFuture(new InvalidClientException(ClientAuthHandler.GENERIC_ERROR_MESSAGE, authenticationHeader()))); return; } handler.handle(Future.succeededFuture(client)); } catch (RuntimeException e) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", e, authenticationHeader()))); return; } }  }
ClientBasicAuthProvider implements ClientAuthProvider { @Override public void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler) { final String authorization = getBasicAuthorization(context.request()); if (authorization == null) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", authenticationHeader()))); return; } try { String decoded = new String(Base64.getDecoder().decode(authorization)); int colonIdx = decoded.indexOf(":"); if (colonIdx == -1) { throw new IllegalArgumentException(); } String clientId = decoded.substring(0, colonIdx); String clientSecret = decoded.substring(colonIdx + 1); if (!client.getClientId().equals(clientId) || !client.getClientSecret().equals(clientSecret)) { handler.handle(Future.failedFuture(new InvalidClientException(ClientAuthHandler.GENERIC_ERROR_MESSAGE, authenticationHeader()))); return; } handler.handle(Future.succeededFuture(client)); } catch (RuntimeException e) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", e, authenticationHeader()))); return; } }  @Override boolean canHandle(Client client, RoutingContext context); @Override void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler); }
ClientBasicAuthProvider implements ClientAuthProvider { @Override public void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler) { final String authorization = getBasicAuthorization(context.request()); if (authorization == null) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", authenticationHeader()))); return; } try { String decoded = new String(Base64.getDecoder().decode(authorization)); int colonIdx = decoded.indexOf(":"); if (colonIdx == -1) { throw new IllegalArgumentException(); } String clientId = decoded.substring(0, colonIdx); String clientSecret = decoded.substring(colonIdx + 1); if (!client.getClientId().equals(clientId) || !client.getClientSecret().equals(clientSecret)) { handler.handle(Future.failedFuture(new InvalidClientException(ClientAuthHandler.GENERIC_ERROR_MESSAGE, authenticationHeader()))); return; } handler.handle(Future.succeededFuture(client)); } catch (RuntimeException e) { handler.handle(Future.failedFuture(new InvalidClientException("Invalid client: missing or unsupported authentication method", e, authenticationHeader()))); return; } }  @Override boolean canHandle(Client client, RoutingContext context); @Override void handle(Client client, RoutingContext context, Handler<AsyncResult<Client>> handler);  }