Search in sources :

Example 1 with Response

use of com.okta.commons.http.Response in project okta-commons-java by okta.

the class HttpClientRequestExecutor method toSdkResponse.

protected Response toSdkResponse(HttpResponse httpResponse) throws IOException {
    int httpStatus = httpResponse.getStatusLine().getStatusCode();
    HttpHeaders headers = getHeaders(httpResponse);
    MediaType mediaType = headers.getContentType();
    HttpEntity entity = getHttpEntity(httpResponse);
    InputStream body = entity != null ? entity.getContent() : null;
    long contentLength;
    // ensure that the content has been fully acquired before closing the http stream
    if (body != null) {
        byte[] bytes = toBytes(entity);
        contentLength = entity.getContentLength();
        if (bytes != null) {
            body = new ByteArrayInputStream(bytes);
        } else {
            body = null;
        }
    } else {
        // force 0 content length when there is no body
        contentLength = 0;
    }
    Response response = new DefaultResponse(httpStatus, mediaType, body, contentLength);
    response.getHeaders().putAll(headers);
    return response;
}
Also used : DefaultResponse(com.okta.commons.http.DefaultResponse) Response(com.okta.commons.http.Response) HttpResponse(org.apache.http.HttpResponse) DefaultResponse(com.okta.commons.http.DefaultResponse) HttpHeaders(com.okta.commons.http.HttpHeaders) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaType(com.okta.commons.http.MediaType)

Example 2 with Response

use of com.okta.commons.http.Response in project okta-commons-java by okta.

the class OkHttpRequestExecutor method toSdkResponse.

private Response toSdkResponse(okhttp3.Response okResponse) throws IOException {
    int httpStatus = okResponse.code();
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(okResponse.headers().toMultimap());
    MediaType mediaType = headers.getContentType();
    ResponseBody body = okResponse.body();
    InputStream bodyInputStream = null;
    long contentLength;
    // ensure that the content has been fully acquired before closing the http stream
    if (body != null) {
        contentLength = body.contentLength();
        bodyInputStream = new ByteArrayInputStream(body.bytes());
    } else {
        // force 0 content length when there is no body
        contentLength = 0;
    }
    Response response = new DefaultResponse(httpStatus, mediaType, bodyInputStream, contentLength);
    response.getHeaders().putAll(headers);
    return response;
}
Also used : DefaultResponse(com.okta.commons.http.DefaultResponse) Response(com.okta.commons.http.Response) DefaultResponse(com.okta.commons.http.DefaultResponse) HttpHeaders(com.okta.commons.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaType(com.okta.commons.http.MediaType) ResponseBody(okhttp3.ResponseBody)

Example 3 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method answerChallenge.

@Override
public IDXResponse answerChallenge(AnswerChallengeRequest answerChallengeRequest, String href) throws ProcessingException {
    IDXResponse idxResponse;
    try {
        Request request = new DefaultRequest(HttpMethod.POST, href, null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(answerChallengeRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 4 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method token.

@Override
public TokenResponse token(String url, String grantType, String interactionCode, IDXClientContext idxClientContext) throws ProcessingException {
    TokenResponse tokenResponse;
    StringBuilder urlParameters = new StringBuilder();
    urlParameters.append("grant_type=").append(grantType);
    urlParameters.append("&client_id=").append(clientConfiguration.getClientId());
    if (Strings.hasText(clientConfiguration.getClientSecret())) {
        urlParameters.append("&client_secret=").append(clientConfiguration.getClientSecret());
    }
    urlParameters.append("&interaction_code=").append(interactionCode);
    urlParameters.append("&code_verifier=").append(idxClientContext.getCodeVerifier());
    try {
        Request request = new DefaultRequest(HttpMethod.POST, url, null, getHttpHeaders(true), new ByteArrayInputStream(urlParameters.toString().getBytes(StandardCharsets.UTF_8)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        tokenResponse = objectMapper.convertValue(responseJsonNode, TokenResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return tokenResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IOException(java.io.IOException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 5 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method interact.

@Override
public IDXClientContext interact(String token, EmailTokenType tokenType) throws ProcessingException {
    InteractResponse interactResponse;
    String codeVerifier, codeChallenge, state;
    try {
        codeVerifier = PkceUtil.generateCodeVerifier();
        codeChallenge = PkceUtil.generateCodeChallenge(codeVerifier);
        state = UUID.randomUUID().toString();
        StringBuilder urlParameters = new StringBuilder().append("client_id=").append(clientConfiguration.getClientId()).append("&client_secret=").append(clientConfiguration.getClientSecret()).append("&scope=").append(clientConfiguration.getScopes().stream().map(Object::toString).collect(Collectors.joining(" "))).append("&code_challenge=").append(codeChallenge).append("&code_challenge_method=").append(PkceUtil.CODE_CHALLENGE_METHOD).append("&redirect_uri=").append(clientConfiguration.getRedirectUri()).append("&state=").append(state);
        if (Strings.hasText(token) && !Strings.isEmpty(tokenType)) {
            if (tokenType == EmailTokenType.ACTIVATION_TOKEN) {
                urlParameters.append("&activation_token=").append(token);
            } else if (tokenType == EmailTokenType.RECOVERY_TOKEN) {
                urlParameters.append("&recovery_token=").append(token);
            }
        }
        HttpHeaders httpHeaders = getHttpHeaders(true);
        if (clientConfiguration.getDeviceContext() != null) {
            httpHeaders.setAll(clientConfiguration.getDeviceContext().getAll());
        }
        Request request = new DefaultRequest(HttpMethod.POST, normalizedIssuerUri(clientConfiguration.getIssuer(), "/v1/interact"), null, httpHeaders, new ByteArrayInputStream(urlParameters.toString().getBytes(StandardCharsets.UTF_8)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        interactResponse = objectMapper.convertValue(responseJsonNode, InteractResponse.class);
        Assert.notNull(interactResponse, "interact response cannot be null");
        Assert.notNull(interactResponse.getInteractionHandle(), "interactionHandle cannot be null");
    } catch (IOException | IllegalArgumentException | HttpException | NoSuchAlgorithmException e) {
        throw new ProcessingException(e);
    }
    return new IDXClientContext(codeVerifier, codeChallenge, interactResponse.getInteractionHandle(), state);
}
Also used : HttpHeaders(com.okta.commons.http.HttpHeaders) DefaultRequest(com.okta.commons.http.DefaultRequest) IDXClientContext(com.okta.idx.sdk.api.model.IDXClientContext) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) HttpException(com.okta.commons.http.HttpException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Aggregations

Response (com.okta.commons.http.Response)19 DefaultRequest (com.okta.commons.http.DefaultRequest)15 Request (com.okta.commons.http.Request)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)13 AnswerChallengeRequest (com.okta.idx.sdk.api.request.AnswerChallengeRequest)13 ChallengeRequest (com.okta.idx.sdk.api.request.ChallengeRequest)13 EnrollRequest (com.okta.idx.sdk.api.request.EnrollRequest)13 EnrollUserProfileUpdateRequest (com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest)13 IdentifyRequest (com.okta.idx.sdk.api.request.IdentifyRequest)13 PollRequest (com.okta.idx.sdk.api.request.PollRequest)13 RecoverRequest (com.okta.idx.sdk.api.request.RecoverRequest)13 SkipAuthenticatorEnrollmentRequest (com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest)13 ErrorResponse (com.okta.idx.sdk.api.response.ErrorResponse)13 IDXResponse (com.okta.idx.sdk.api.response.IDXResponse)13 TokenResponse (com.okta.idx.sdk.api.response.TokenResponse)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 HttpException (com.okta.commons.http.HttpException)12 CancelRequest (com.okta.idx.sdk.api.request.CancelRequest)12 IntrospectRequest (com.okta.idx.sdk.api.request.IntrospectRequest)12