Search in sources :

Example 1 with AcquisitionResponse

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");
}
Also used : AcquisitionResponse(io.syndesis.server.credential.AcquisitionResponse) CredentialFlowState(io.syndesis.server.credential.CredentialFlowState) ClientSideState(io.syndesis.server.endpoint.v1.state.ClientSideState) State(io.syndesis.server.credential.AcquisitionResponse.State) OAuth2CredentialFlowState(io.syndesis.server.credential.OAuth2CredentialFlowState) CredentialFlowState(io.syndesis.server.credential.CredentialFlowState) OAuth2CredentialFlowState(io.syndesis.server.credential.OAuth2CredentialFlowState) OAuth2CredentialFlowState(io.syndesis.server.credential.OAuth2CredentialFlowState) URI(java.net.URI) Test(org.junit.Test)

Example 2 with AcquisitionResponse

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();
}
Also used : AcquisitionResponse(io.syndesis.server.credential.AcquisitionResponse) AcquisitionFlow(io.syndesis.server.credential.AcquisitionFlow) CredentialFlowState(io.syndesis.server.credential.CredentialFlowState) NewCookie(javax.ws.rs.core.NewCookie) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

AcquisitionResponse (io.syndesis.server.credential.AcquisitionResponse)2 CredentialFlowState (io.syndesis.server.credential.CredentialFlowState)2 AcquisitionFlow (io.syndesis.server.credential.AcquisitionFlow)1 State (io.syndesis.server.credential.AcquisitionResponse.State)1 OAuth2CredentialFlowState (io.syndesis.server.credential.OAuth2CredentialFlowState)1 ClientSideState (io.syndesis.server.endpoint.v1.state.ClientSideState)1 URI (java.net.URI)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 NewCookie (javax.ws.rs.core.NewCookie)1 Test (org.junit.Test)1