use of com.okta.commons.http.Request in project okta-commons-java by okta.
the class OkHttpRequestExecutor method executeRequest.
@Override
public Response executeRequest(Request request) throws HttpException {
// Sign the request
this.requestAuthenticator.authenticate(request);
HttpUrl.Builder urlBuilder = HttpUrl.get(request.getResourceUrl()).newBuilder();
// query params
request.getQueryString().forEach(urlBuilder::addQueryParameter);
okhttp3.Request.Builder okRequestBuilder = new okhttp3.Request.Builder().url(urlBuilder.build());
// headers
request.getHeaders().toSingleValueMap().forEach(okRequestBuilder::addHeader);
boolean isMultipartFormDataForFileUploading = false;
String xContentType = RequestUtils.fetchHeaderValueAndRemoveIfPresent(request, "x-contentType");
if (!Strings.isEmpty(xContentType)) {
isMultipartFormDataForFileUploading = xContentType.equals(MediaType.MULTIPART_FORM_DATA_VALUE);
}
HttpMethod method = request.getMethod();
switch(method) {
case DELETE:
okRequestBuilder.delete();
break;
case GET:
okRequestBuilder.get();
break;
case HEAD:
okRequestBuilder.head();
break;
case POST:
if (isMultipartFormDataForFileUploading) {
String fileLocation = RequestUtils.fetchHeaderValueAndRemoveIfPresent(request, "x-fileLocation");
String formDataPartName = RequestUtils.fetchHeaderValueAndRemoveIfPresent(request, "x-fileFormDataName");
File file = new File(fileLocation);
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart(formDataPartName, file.getName(), RequestBody.create(null, file)).build();
okRequestBuilder.post(requestBody);
} else {
okRequestBuilder.post(new InputStreamRequestBody(request.getBody(), request.getHeaders().getContentType()));
}
break;
case PUT:
// TODO support 100-continue ?
okRequestBuilder.put(new InputStreamRequestBody(request.getBody(), request.getHeaders().getContentType()));
break;
default:
throw new IllegalArgumentException("Unrecognized HttpMethod: " + method);
}
try {
okhttp3.Response okResponse = client.newCall(okRequestBuilder.build()).execute();
return toSdkResponse(okResponse);
} catch (SocketException | SocketTimeoutException e) {
throw new HttpException("Unable to execute HTTP request - retryable exception: " + e.getMessage(), e, true);
} catch (IOException e) {
throw new HttpException(e.getMessage(), e);
}
}
use of com.okta.commons.http.Request 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;
}
use of com.okta.commons.http.Request 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;
}
use of com.okta.commons.http.Request 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);
}
use of com.okta.commons.http.Request in project okta-idx-java by okta.
the class BaseIDXClient method cancel.
@Override
public IDXResponse cancel(String stateHandle) throws ProcessingException {
IDXResponse idxResponse;
CancelRequest cancelRequest = CancelRequestBuilder.builder().withStateHandle(stateHandle).build();
try {
Request request = new DefaultRequest(HttpMethod.POST, clientConfiguration.getBaseUrl() + "/idp/idx/cancel", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(cancelRequest)), -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;
}
Aggregations