### Question:
TokenTransformationFilter extends GenericHeapObject implements Filter { @Override public Promise<Response, NeverThrowsException> filter(final Context context, final Request request, final Handler next) { final String resolvedIdToken = idToken.eval(bindings(context, request)); if (resolvedIdToken == null) { logger.error(format("OpenID Connect id_token expression (%s) has evaluated to null", idToken)); return newResponsePromise(newInternalServerError()); } return handler.handle(context, transformationRequest(resolvedIdToken)) .thenAsync(processIssuedToken(context, request, next)); } TokenTransformationFilter(final Handler handler,
                                     final URI endpoint,
                                     final Expression<String> idToken,
                                     final Expression<String> target); @Override Promise<Response, NeverThrowsException> filter(final Context context,
                                                          final Request request,
                                                          final Handler next);  }### Answer:
@Test(dataProvider = "uriBuildingBlocks") public void shouldCreateTranslationUrlWithRealm(String baseUri, String realm, String instance, String expected) throws Exception { when(transformationHandler.handle(any(Context.class), any(Request.class))) .thenReturn(newResponsePromise(SSO_TOKEN_RESPONSE)); JsonValue config = json(object(field("openamUri", baseUri), field("realm", realm), field("username", "guillaume"), field("password", "s3cr3t"), field("idToken", "${attributes.id_token}"), field("target", "${attributes.saml}"), field("instance", instance), field("amHandler", "#mock-handler"))); Filter filter = (Filter) new TokenTransformationFilter.Heaplet().create(Name.of("this"), config, heap); filter.filter(new SessionContext(context, mock(Session.class)), new Request(), next); verify(transformationHandler, times(2)).handle(any(Context.class), captor.capture()); Request transformationRequest = captor.getAllValues().get(1); assertThat(transformationRequest.getUri().asURI()) .isEqualTo(new URI(expected)); }
@Test public void shouldTransformIdTokenToSamlAssertions() throws Exception { when(transformationHandler.handle(eq(context), any(Request.class))) .thenReturn(newResponsePromise(ISSUED_TOKEN_RESPONSE)); TokenTransformationFilter filter = new TokenTransformationFilter(transformationHandler, new URI("http: Expression.valueOf("${attributes.id_token}", String.class), Expression.valueOf("${attributes.saml_token}", String.class)); Request request = new Request(); filter.filter(context, request, next); verify(transformationHandler).handle(eq(context), captor.capture()); Request transformationRequest = captor.getValue(); JsonValue transformation = new JsonValue(transformationRequest.getEntity().getJson()); assertThat(transformation.get(new JsonPointer("input_token_state/oidc_id_token")).asString()) .isEqualTo(ID_TOKEN_JWT); assertThat(attributesContext.getAttributes()).contains(entry("saml_token", SAML_ASSERTIONS)); verify(next).handle(context, request); }
@Test public void shouldFailWhenNoTransformedTokenIsIssued() throws Exception { when(transformationHandler.handle(eq(context), any(Request.class))) .thenReturn(newResponsePromise(ERROR_RESPONSE)); TokenTransformationFilter filter = new TokenTransformationFilter(transformationHandler, new URI("http: Expression.valueOf("${attributes.id_token}", String.class), Expression.valueOf("${attributes.saml_token}", String.class)); Request request = new Request(); Response response = filter.filter(context, request, next).get(); assertThat(response.getStatus()).isEqualTo(Status.BAD_GATEWAY); verifyZeroInteractions(next); }