use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.
the class AzureActiveDirectory method performCloudDiscovery.
public static synchronized void performCloudDiscovery() throws IOException {
final String methodName = ":performCloudDiscovery";
Uri instanceDiscoveryRequestUri = Uri.parse(getDefaultCloudUrl() + AAD_INSTANCE_DISCOVERY_ENDPOINT);
instanceDiscoveryRequestUri = instanceDiscoveryRequestUri.buildUpon().appendQueryParameter(API_VERSION, API_VERSION_VALUE).appendQueryParameter(AUTHORIZATION_ENDPOINT, AUTHORIZATION_ENDPOINT_VALUE).build();
final HttpResponse response = httpClient.get(new URL(instanceDiscoveryRequestUri.toString()), new HashMap<String, String>());
Logger.info(TAG + methodName, "Response received.");
if (response.getStatusCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
Logger.warn(TAG + methodName, "Error getting cloud information");
} else {
// Our request was successful. Flush the HTTP cache to disk. Should only happen once
// per app launch. Instance Discovery Metadata will be cached in-memory
// until the app is killed.
HttpCache.flush();
Logger.info(TAG + methodName, "Parsing response.");
AzureActiveDirectoryInstanceResponse instanceResponse = ObjectMapper.deserializeJsonStringToObject(response.getBody(), AzureActiveDirectoryInstanceResponse.class);
Logger.info(TAG + methodName, "Discovered [" + instanceResponse.getClouds().size() + "] clouds.");
for (final AzureActiveDirectoryCloud cloud : instanceResponse.getClouds()) {
// Mark the deserialized Clouds as validated
cloud.setIsValidated(true);
for (final String alias : cloud.getHostAliases()) {
sAadClouds.put(alias.toLowerCase(Locale.US), cloud);
}
}
sIsInitialized = true;
}
}
use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.
the class MicrosoftStsOAuth2Strategy method performTokenRequest.
@Override
protected HttpResponse performTokenRequest(final MicrosoftStsTokenRequest request) throws IOException, ClientException {
final String methodName = ":performTokenRequest";
final HttpResponse response = super.performTokenRequest(request);
if (response.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED && response.getHeaders() != null && response.getHeaders().containsKey(CHALLENGE_REQUEST_HEADER)) {
// Received the device certificate challenge request. It is sent in 401 header.
Logger.info(TAG + methodName, "Receiving device certificate challenge request. ");
return performPKeyAuthRequest(response, request);
}
return response;
}
use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.
the class OAuth2Strategy method requestToken.
/**
* @param request generic token request.
* @return GenericTokenResult
* @throws IOException thrown when failed or interrupted I/O operations occur.
*/
public GenericTokenResult requestToken(final GenericTokenRequest request) throws IOException, ClientException {
final String methodName = ":requestToken";
Logger.verbose(TAG + methodName, "Requesting token...");
validateTokenRequest(request);
final HttpResponse response = performTokenRequest(request);
final GenericTokenResult result = getTokenResultFromHttpResponse(response);
if (result.getTokenResponse() != null) {
result.getTokenResponse().setAuthority(mTokenEndpoint);
}
if (result.getSuccess()) {
validateTokenResponse(request, result);
}
return result;
}
use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.
the class OAuth2Strategy method getDeviceCode.
public AuthorizationResult getDeviceCode(@NonNull final MicrosoftStsAuthorizationRequest authorizationRequest) throws IOException {
final String methodName = ":getDeviceCode";
// Set up headers and request body
final String requestBody = ObjectMapper.serializeObjectToFormUrlEncoded(authorizationRequest);
final Map<String, String> headers = new TreeMap<>();
headers.put(CLIENT_REQUEST_ID, DiagnosticContext.getRequestContext().get(DiagnosticContext.CORRELATION_ID));
headers.putAll(EstsTelemetry.getInstance().getTelemetryHeaders());
headers.put(HttpConstants.HeaderField.CONTENT_TYPE, DEVICE_CODE_CONTENT_TYPE);
final HttpResponse response = httpClient.post(((MicrosoftStsOAuth2Configuration) mConfig).getDeviceAuthorizationEndpoint(), headers, requestBody.getBytes(ObjectMapper.ENCODING_SCHEME));
// Any code below 300 (HTTP_MULT_CHOICE) is considered a success
if (response.getStatusCode() < HttpsURLConnection.HTTP_MULT_CHOICE) {
// Get and parse response body
final HashMap<String, String> parsedResponseBody = new Gson().fromJson(response.getBody(), new TypeToken<HashMap<String, String>>() {
}.getType());
// Create response and result objects
// "code" can be left null since it's DCF
final MicrosoftStsAuthorizationResponse authorizationResponse = new MicrosoftStsAuthorizationResponse(null, authorizationRequest.getState(), parsedResponseBody);
// MicrosoftSTAuthorizationResultFactory not used since no Intent is being created
final AuthorizationResult authorizationResult = new MicrosoftStsAuthorizationResult(AuthorizationStatus.SUCCESS, authorizationResponse);
Logger.verbose(TAG + methodName, "Device Code Flow authorization successful...");
return authorizationResult;
} else // Request failed
{
// Get and parse response body
final HashMap<String, Object> parsedResponseBody = new Gson().fromJson(response.getBody(), new TypeToken<HashMap<String, Object>>() {
}.getType());
// Create response and result objects
final MicrosoftStsAuthorizationErrorResponse authorizationErrorResponse = new MicrosoftStsAuthorizationErrorResponse((String) parsedResponseBody.get(AuthorizationResultFactory.ERROR), (String) parsedResponseBody.get(AuthorizationResultFactory.ERROR_DESCRIPTION));
// MicrosoftSTAuthorizationResultFactory not used since no Intent is being created
final AuthorizationResult authorizationResult = new MicrosoftStsAuthorizationResult(AuthorizationStatus.FAIL, authorizationErrorResponse);
Logger.verbose(TAG + methodName, "Device Code Flow authorization failure...");
return authorizationResult;
}
}
use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.
the class HttpResponseTest method testHttpResponseWithNullResponseHeaders.
@Test
public void testHttpResponseWithNullResponseHeaders() {
final HttpResponse response = new HttpResponse(HttpURLConnection.HTTP_OK, RESPONSE_BODY, null);
Assert.assertTrue(response.getBody().equals(RESPONSE_BODY));
Assert.assertNull(response.getHeaders());
}
Aggregations