use of io.syndesis.server.credential.AcquisitionResponse in project syndesis by syndesisio.
the class CredentialITCase method shouldInitiateCredentialFlow.
@Test
public void shouldInitiateCredentialFlow() throws UnsupportedEncodingException {
final ResponseEntity<AcquisitionResponse> acquisitionResponse = post("/api/v1/connectors/test-provider/credentials", Collections.singletonMap("returnUrl", "/ui#state"), AcquisitionResponse.class, tokenRule.validToken(), HttpStatus.ACCEPTED);
assertThat(acquisitionResponse.hasBody()).as("Should present a acquisition response in the HTTP body").isTrue();
final AcquisitionResponse response = acquisitionResponse.getBody();
assertThat(response.getType()).isEqualTo(Type.OAUTH2);
final String redirectUrl = response.getRedirectUrl();
assertThat(redirectUrl).as("Should redirect to Salesforce and containthe correct callback URL").startsWith("https://test/oauth2/authorize?client_id=testClientId&response_type=code&redirect_uri=").contains(encode("/api/v1/credentials/callback", "ASCII"));
final MultiValueMap<String, String> params = UriComponentsBuilder.fromHttpUrl(redirectUrl).build().getQueryParams();
final String state = params.getFirst("state");
assertThat(state).as("state parameter should be set").isNotEmpty();
final State responseStateInstruction = response.state();
assertThat(responseStateInstruction).as("acquisition response should contain the state instruction").isNotNull();
assertThat(responseStateInstruction.persist()).isEqualByComparingTo(State.Persist.COOKIE);
assertThat(responseStateInstruction.spec()).isNotEmpty();
final CredentialFlowState credentialFlowState = clientSideState.restoreFrom(Cookie.valueOf(responseStateInstruction.spec()), CredentialFlowState.class);
final CredentialFlowState expected = new OAuth2CredentialFlowState.Builder().key("test-state").providerId("test-provider").build();
assertThat(credentialFlowState).as("The flow state should be as expected").isEqualToIgnoringGivenFields(expected, "returnUrl");
final URI returnUrl = credentialFlowState.getReturnUrl();
assertThat(returnUrl).isNotNull();
assertThat(returnUrl.isAbsolute()).isTrue();
assertThat(returnUrl.getPath()).isEqualTo("/ui");
assertThat(returnUrl.getFragment()).isEqualTo("state");
}
use of io.syndesis.server.credential.AcquisitionResponse in project syndesis by syndesisio.
the class ConnectorCredentialHandler method create.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(@NotNull @Valid final AcquisitionRequest request, @Context final HttpServletRequest httpRequest) {
final AcquisitionFlow acquisitionFlow = credentials.acquire(connectorId, apiBase(httpRequest), absoluteTo(httpRequest, request.getReturnUrl()));
final CredentialFlowState flowState = acquisitionFlow.state().get();
final NewCookie cookie = state.persist(flowState.persistenceKey(), "/", flowState);
final AcquisitionResponse acquisitionResponse = AcquisitionResponse.Builder.from(acquisitionFlow).state(State.Builder.cookie(cookie.toString())).build();
return Response.accepted().entity(acquisitionResponse).build();
}
Aggregations