@Test public void shouldLoadUserByUsername_userMapping() { Map<String, String> attributes = new HashMap<>(); attributes.put("sub", "id"); attributes.put("preferred_username", "username"); mapper.setMappers(attributes); stubFor(any(urlPathEqualTo("/api/authentication")) .withRequestBody(matching(".*")) .willReturn(okJson("{\"id\" : \"123456789\", \"username\" : \"johndoe\"}"))); TestObserver<User> testObserver = authenticationProvider.loadUserByUsername(new Authentication() { @Override public Object getCredentials() { return "johndoe"; } @Override public Object getPrincipal() { return "johndoepassword"; } @Override public AuthenticationContext getContext() { return new SimpleAuthenticationContext(new DummyRequest()); } }).test(); testObserver.awaitTerminalEvent(); testObserver.assertComplete(); testObserver.assertNoErrors(); testObserver.assertValue(u -> "123456789".equals(u.getId())); testObserver.assertValue(u -> "johndoe".equals(u.getUsername())); }
@Override public Maybe<User> loadUserByUsername(Authentication authentication) { try { TemplateEngine templateEngine = authentication.getContext().getTemplateEngine(); templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, authentication.getPrincipal()); templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, authentication.getCredentials()); final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource(); final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class); final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString()); final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders(); final String authenticationBody = resourceConfiguration.getHttpBody(); final HttpRequest<Buffer> httpRequest = client.requestAbs(authenticationHttpMethod, authenticationURI); final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions(); if (authenticationHttpHeaders != null) { authenticationHttpHeaders.forEach(header -> { String extValue = templateEngine.getValue(header.getValue(), String.class); httpRequest.putHeader(header.getName(), extValue); }); } Single<HttpResponse<Buffer>> responseHandler; if (authenticationBody != null && !authenticationBody.isEmpty()) { String bodyRequest = templateEngine.getValue(authenticationBody, String.class); if (!httpRequest.headers().contains(HttpHeaders.CONTENT_TYPE)) { httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } else { String contentTypeHeader = httpRequest.headers().get(HttpHeaders.CONTENT_TYPE); switch (contentTypeHeader) { case(MediaType.APPLICATION_JSON): responseHandler = httpRequest.rxSendJsonObject(new JsonObject(bodyRequest)); break; case(MediaType.APPLICATION_FORM_URLENCODED): Map<String, String> queryParameters = format(bodyRequest); MultiMap multiMap = MultiMap.caseInsensitiveMultiMap(); multiMap.setAll(queryParameters); responseHandler = httpRequest.rxSendForm(multiMap); break; default: httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } } } else { responseHandler = httpRequest.rxSend(); } return responseHandler .toMaybe() .map(httpResponse -> { String responseBody = httpResponse.bodyAsString(); templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody)); Exception lastException = null; if (errorConditions != null) { Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator(); while (iter.hasNext() && lastException == null) { HttpResponseErrorCondition errorCondition = iter.next(); if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) { Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException()); if (errorCondition.getMessage() != null) { String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class); Constructor<?> constructor = clazz.getConstructor(String.class); lastException = clazz.cast(constructor.newInstance(new Object[]{errorMessage})); } else { lastException = clazz.newInstance(); } } } } if (lastException != null) { throw lastException; } if (responseBody == null) { throw new InternalAuthenticationServiceException("Unable to find user information"); } return createUser( new JsonObject(responseBody).getMap()); }) .onErrorResumeNext(ex -> { if (ex instanceof AuthenticationException) { return Maybe.error(ex); } LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex)); }); } catch (Exception ex) { LOGGER.error("An error has occurred while authenticating the user {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex)); } }
HttpAuthenticationProvider implements AuthenticationProvider { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { try { TemplateEngine templateEngine = authentication.getContext().getTemplateEngine(); templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, authentication.getPrincipal()); templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, authentication.getCredentials()); final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource(); final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class); final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString()); final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders(); final String authenticationBody = resourceConfiguration.getHttpBody(); final HttpRequest<Buffer> httpRequest = client.requestAbs(authenticationHttpMethod, authenticationURI); final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions(); if (authenticationHttpHeaders != null) { authenticationHttpHeaders.forEach(header -> { String extValue = templateEngine.getValue(header.getValue(), String.class); httpRequest.putHeader(header.getName(), extValue); }); } Single<HttpResponse<Buffer>> responseHandler; if (authenticationBody != null && !authenticationBody.isEmpty()) { String bodyRequest = templateEngine.getValue(authenticationBody, String.class); if (!httpRequest.headers().contains(HttpHeaders.CONTENT_TYPE)) { httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } else { String contentTypeHeader = httpRequest.headers().get(HttpHeaders.CONTENT_TYPE); switch (contentTypeHeader) { case(MediaType.APPLICATION_JSON): responseHandler = httpRequest.rxSendJsonObject(new JsonObject(bodyRequest)); break; case(MediaType.APPLICATION_FORM_URLENCODED): Map<String, String> queryParameters = format(bodyRequest); MultiMap multiMap = MultiMap.caseInsensitiveMultiMap(); multiMap.setAll(queryParameters); responseHandler = httpRequest.rxSendForm(multiMap); break; default: httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } } } else { responseHandler = httpRequest.rxSend(); } return responseHandler .toMaybe() .map(httpResponse -> { String responseBody = httpResponse.bodyAsString(); templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody)); Exception lastException = null; if (errorConditions != null) { Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator(); while (iter.hasNext() && lastException == null) { HttpResponseErrorCondition errorCondition = iter.next(); if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) { Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException()); if (errorCondition.getMessage() != null) { String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class); Constructor<?> constructor = clazz.getConstructor(String.class); lastException = clazz.cast(constructor.newInstance(new Object[]{errorMessage})); } else { lastException = clazz.newInstance(); } } } } if (lastException != null) { throw lastException; } if (responseBody == null) { throw new InternalAuthenticationServiceException("Unable to find user information"); } return createUser( new JsonObject(responseBody).getMap()); }) .onErrorResumeNext(ex -> { if (ex instanceof AuthenticationException) { return Maybe.error(ex); } LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex)); }); } catch (Exception ex) { LOGGER.error("An error has occurred while authenticating the user {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex)); } } }
HttpAuthenticationProvider implements AuthenticationProvider { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { try { TemplateEngine templateEngine = authentication.getContext().getTemplateEngine(); templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, authentication.getPrincipal()); templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, authentication.getCredentials()); final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource(); final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class); final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString()); final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders(); final String authenticationBody = resourceConfiguration.getHttpBody(); final HttpRequest<Buffer> httpRequest = client.requestAbs(authenticationHttpMethod, authenticationURI); final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions(); if (authenticationHttpHeaders != null) { authenticationHttpHeaders.forEach(header -> { String extValue = templateEngine.getValue(header.getValue(), String.class); httpRequest.putHeader(header.getName(), extValue); }); } Single<HttpResponse<Buffer>> responseHandler; if (authenticationBody != null && !authenticationBody.isEmpty()) { String bodyRequest = templateEngine.getValue(authenticationBody, String.class); if (!httpRequest.headers().contains(HttpHeaders.CONTENT_TYPE)) { httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } else { String contentTypeHeader = httpRequest.headers().get(HttpHeaders.CONTENT_TYPE); switch (contentTypeHeader) { case(MediaType.APPLICATION_JSON): responseHandler = httpRequest.rxSendJsonObject(new JsonObject(bodyRequest)); break; case(MediaType.APPLICATION_FORM_URLENCODED): Map<String, String> queryParameters = format(bodyRequest); MultiMap multiMap = MultiMap.caseInsensitiveMultiMap(); multiMap.setAll(queryParameters); responseHandler = httpRequest.rxSendForm(multiMap); break; default: httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } } } else { responseHandler = httpRequest.rxSend(); } return responseHandler .toMaybe() .map(httpResponse -> { String responseBody = httpResponse.bodyAsString(); templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody)); Exception lastException = null; if (errorConditions != null) { Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator(); while (iter.hasNext() && lastException == null) { HttpResponseErrorCondition errorCondition = iter.next(); if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) { Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException()); if (errorCondition.getMessage() != null) { String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class); Constructor<?> constructor = clazz.getConstructor(String.class); lastException = clazz.cast(constructor.newInstance(new Object[]{errorMessage})); } else { lastException = clazz.newInstance(); } } } } if (lastException != null) { throw lastException; } if (responseBody == null) { throw new InternalAuthenticationServiceException("Unable to find user information"); } return createUser( new JsonObject(responseBody).getMap()); }) .onErrorResumeNext(ex -> { if (ex instanceof AuthenticationException) { return Maybe.error(ex); } LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex)); }); } catch (Exception ex) { LOGGER.error("An error has occurred while authenticating the user {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex)); } }  }
HttpAuthenticationProvider implements AuthenticationProvider { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { try { TemplateEngine templateEngine = authentication.getContext().getTemplateEngine(); templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, authentication.getPrincipal()); templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, authentication.getCredentials()); final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource(); final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class); final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString()); final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders(); final String authenticationBody = resourceConfiguration.getHttpBody(); final HttpRequest<Buffer> httpRequest = client.requestAbs(authenticationHttpMethod, authenticationURI); final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions(); if (authenticationHttpHeaders != null) { authenticationHttpHeaders.forEach(header -> { String extValue = templateEngine.getValue(header.getValue(), String.class); httpRequest.putHeader(header.getName(), extValue); }); } Single<HttpResponse<Buffer>> responseHandler; if (authenticationBody != null && !authenticationBody.isEmpty()) { String bodyRequest = templateEngine.getValue(authenticationBody, String.class); if (!httpRequest.headers().contains(HttpHeaders.CONTENT_TYPE)) { httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } else { String contentTypeHeader = httpRequest.headers().get(HttpHeaders.CONTENT_TYPE); switch (contentTypeHeader) { case(MediaType.APPLICATION_JSON): responseHandler = httpRequest.rxSendJsonObject(new JsonObject(bodyRequest)); break; case(MediaType.APPLICATION_FORM_URLENCODED): Map<String, String> queryParameters = format(bodyRequest); MultiMap multiMap = MultiMap.caseInsensitiveMultiMap(); multiMap.setAll(queryParameters); responseHandler = httpRequest.rxSendForm(multiMap); break; default: httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } } } else { responseHandler = httpRequest.rxSend(); } return responseHandler .toMaybe() .map(httpResponse -> { String responseBody = httpResponse.bodyAsString(); templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody)); Exception lastException = null; if (errorConditions != null) { Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator(); while (iter.hasNext() && lastException == null) { HttpResponseErrorCondition errorCondition = iter.next(); if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) { Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException()); if (errorCondition.getMessage() != null) { String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class); Constructor<?> constructor = clazz.getConstructor(String.class); lastException = clazz.cast(constructor.newInstance(new Object[]{errorMessage})); } else { lastException = clazz.newInstance(); } } } } if (lastException != null) { throw lastException; } if (responseBody == null) { throw new InternalAuthenticationServiceException("Unable to find user information"); } return createUser( new JsonObject(responseBody).getMap()); }) .onErrorResumeNext(ex -> { if (ex instanceof AuthenticationException) { return Maybe.error(ex); } LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex)); }); } catch (Exception ex) { LOGGER.error("An error has occurred while authenticating the user {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex)); } }  @Override Maybe<User> loadUserByUsername(Authentication authentication); @Override Maybe<User> loadUserByUsername(String username); }
HttpAuthenticationProvider implements AuthenticationProvider { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { try { TemplateEngine templateEngine = authentication.getContext().getTemplateEngine(); templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, authentication.getPrincipal()); templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, authentication.getCredentials()); final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource(); final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class); final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString()); final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders(); final String authenticationBody = resourceConfiguration.getHttpBody(); final HttpRequest<Buffer> httpRequest = client.requestAbs(authenticationHttpMethod, authenticationURI); final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions(); if (authenticationHttpHeaders != null) { authenticationHttpHeaders.forEach(header -> { String extValue = templateEngine.getValue(header.getValue(), String.class); httpRequest.putHeader(header.getName(), extValue); }); } Single<HttpResponse<Buffer>> responseHandler; if (authenticationBody != null && !authenticationBody.isEmpty()) { String bodyRequest = templateEngine.getValue(authenticationBody, String.class); if (!httpRequest.headers().contains(HttpHeaders.CONTENT_TYPE)) { httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } else { String contentTypeHeader = httpRequest.headers().get(HttpHeaders.CONTENT_TYPE); switch (contentTypeHeader) { case(MediaType.APPLICATION_JSON): responseHandler = httpRequest.rxSendJsonObject(new JsonObject(bodyRequest)); break; case(MediaType.APPLICATION_FORM_URLENCODED): Map<String, String> queryParameters = format(bodyRequest); MultiMap multiMap = MultiMap.caseInsensitiveMultiMap(); multiMap.setAll(queryParameters); responseHandler = httpRequest.rxSendForm(multiMap); break; default: httpRequest.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(bodyRequest.length())); responseHandler = httpRequest.rxSendBuffer(Buffer.buffer(bodyRequest)); } } } else { responseHandler = httpRequest.rxSend(); } return responseHandler .toMaybe() .map(httpResponse -> { String responseBody = httpResponse.bodyAsString(); templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody)); Exception lastException = null; if (errorConditions != null) { Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator(); while (iter.hasNext() && lastException == null) { HttpResponseErrorCondition errorCondition = iter.next(); if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) { Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException()); if (errorCondition.getMessage() != null) { String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class); Constructor<?> constructor = clazz.getConstructor(String.class); lastException = clazz.cast(constructor.newInstance(new Object[]{errorMessage})); } else { lastException = clazz.newInstance(); } } } } if (lastException != null) { throw lastException; } if (responseBody == null) { throw new InternalAuthenticationServiceException("Unable to find user information"); } return createUser( new JsonObject(responseBody).getMap()); }) .onErrorResumeNext(ex -> { if (ex instanceof AuthenticationException) { return Maybe.error(ex); } LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex)); }); } catch (Exception ex) { LOGGER.error("An error has occurred while authenticating the user {}", ex); return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex)); } }  @Override Maybe<User> loadUserByUsername(Authentication authentication); @Override Maybe<User> loadUserByUsername(String username);  }