Search in sources :

Example 21 with HttpResponse

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;
    }
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) Uri(android.net.Uri) URL(java.net.URL)

Example 22 with HttpResponse

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;
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Example 23 with HttpResponse

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;
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Example 24 with HttpResponse

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;
    }
}
Also used : MicrosoftStsAuthorizationResult(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResult) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) Gson(com.google.gson.Gson) TreeMap(java.util.TreeMap) MicrosoftStsAuthorizationResult(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResult) MicrosoftStsAuthorizationErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationErrorResponse) MicrosoftStsAuthorizationResponse(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResponse) TypeToken(com.google.gson.reflect.TypeToken)

Example 25 with HttpResponse

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());
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.identity.common.internal.net.HttpResponse)29 IOException (java.io.IOException)10 HttpURLConnection (java.net.HttpURLConnection)10 InOrder (org.mockito.InOrder)10 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Test (org.junit.Test)5 MicrosoftTokenErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse)4 URL (java.net.URL)3 MicrosoftTokenResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenResponse)2 TokenResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenResponse)2 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)2 MockTokenResponse (com.microsoft.identity.internal.testutils.mocks.MockTokenResponse)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 MicrosoftTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest)1