Search in sources :

Example 11 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockServerResponse method getMockTokenFailureProtectionPolicyRequiredResponse.

public static HttpResponse getMockTokenFailureProtectionPolicyRequiredResponse() {
    final MicrosoftTokenErrorResponse tokenErrorResponse = new MicrosoftTokenErrorResponse();
    tokenErrorResponse.setError("unauthorized_client");
    tokenErrorResponse.setErrorDescription("AADSTS53005: Application needs to enforce Intune protection policies");
    tokenErrorResponse.setErrorCodes(new ArrayList<Long>(Arrays.asList(70000L)));
    tokenErrorResponse.setTimeStamp("2019-10-23 21:05:16Z");
    tokenErrorResponse.setTraceId("8497799a-e9f9-402f-a951-7060b5014600");
    tokenErrorResponse.setCorrelationId("390d7507-c607-4f05-bb8a-51a2a7a6282b");
    tokenErrorResponse.setErrorUri("https://login.microsoftonline.com/error?code=70000");
    tokenErrorResponse.setSubError("protection_policy_required");
    final String mockResponse = ObjectMapper.serializeObjectToJsonString(tokenErrorResponse);
    final HttpResponse response = new HttpResponse(400, mockResponse, new HashMap<String, List<String>>());
    return response;
}
Also used : MicrosoftTokenErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockServerResponse method getMockTokenFailureServiceUnavailable.

public static HttpResponse getMockTokenFailureServiceUnavailable() {
    final MicrosoftTokenErrorResponse tokenErrorResponse = new MicrosoftTokenErrorResponse();
    tokenErrorResponse.setError("service_unavailable");
    tokenErrorResponse.setErrorDescription("AADSTS70000: Service is unavailable");
    tokenErrorResponse.setErrorCodes(new ArrayList<Long>(Arrays.asList(70000L)));
    tokenErrorResponse.setTimeStamp("2019-10-23 21:05:16Z");
    tokenErrorResponse.setTraceId("8497799a-e9f9-402f-a951-7060b5014600");
    tokenErrorResponse.setCorrelationId("390d7507-c607-4f05-bb8a-51a2a7a6282b");
    tokenErrorResponse.setErrorUri("https://login.microsoftonline.com/error?code=70000");
    tokenErrorResponse.setSubError("bad_token");
    final String mockResponse = ObjectMapper.serializeObjectToJsonString(tokenErrorResponse);
    final HttpResponse response = new HttpResponse(503, mockResponse, new HashMap<String, List<String>>());
    return response;
}
Also used : MicrosoftTokenErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockServerResponse method getMockTokenSuccessResponse.

public static HttpResponse getMockTokenSuccessResponse(final String localAccountId, final String tenant, final String issuer, final String rawClientInfo, final String accessToken) {
    final MicrosoftTokenResponse mockTokenResponse = new MicrosoftTokenResponse();
    mockTokenResponse.setTokenType("Bearer");
    mockTokenResponse.setScope("User.Read");
    mockTokenResponse.setExpiresIn(defaultTokenExpiryInSec);
    mockTokenResponse.setExtExpiresIn(defaultTokenExpiryInSec);
    mockTokenResponse.setAccessToken(accessToken);
    mockTokenResponse.setRefreshToken("6b80f5b5-d53c-4c46-992d-66c5dcd4cfb1");
    mockTokenResponse.setIdToken(MockTokenCreator.createMockIdTokenWithObjectIdTenantIdAndIssuer(localAccountId, tenant, issuer));
    mockTokenResponse.setClientInfo(rawClientInfo);
    final String mockResponse = ObjectMapper.serializeObjectToJsonString(mockTokenResponse);
    return new HttpResponse(200, mockResponse, new HashMap<>());
}
Also used : MicrosoftTokenResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenResponse) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Example 14 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class OpenIdProviderConfigurationClient method loadOpenIdProviderConfiguration.

/**
 * Get OpenID provider configuration.
 *
 * @return OpenIdProviderConfiguration
 */
public synchronized OpenIdProviderConfiguration loadOpenIdProviderConfiguration() throws ServiceException {
    final String methodName = ":loadOpenIdProviderConfiguration";
    try {
        final URL configUrl = new URL(mIssuer + sWellKnownConfig);
        // Check first for a cached copy...
        final OpenIdProviderConfiguration cacheResult = sConfigCache.get(configUrl);
        // If we found a result, return it...
        if (null != cacheResult) {
            Logger.info(TAG + methodName, "Using cached metadata result.");
            return cacheResult;
        }
        Logger.verbose(TAG + methodName, "Config URL is valid.");
        Logger.verbosePII(TAG + methodName, "Using request URL: " + configUrl);
        final HttpResponse providerConfigResponse = httpClient.get(configUrl, new HashMap<String, String>());
        final int statusCode = providerConfigResponse.getStatusCode();
        if (HttpURLConnection.HTTP_OK != statusCode || TextUtils.isEmpty(providerConfigResponse.getBody())) {
            throw new ServiceException(OPENID_PROVIDER_CONFIGURATION_FAILED_TO_LOAD, "OpenId Provider Configuration metadata failed to load with status: " + statusCode, null);
        }
        final OpenIdProviderConfiguration parsedConfig = parseMetadata(providerConfigResponse.getBody());
        // Cache our config in memory for later
        cacheConfiguration(configUrl, parsedConfig);
        return parsedConfig;
    } catch (IOException e) {
        throw new ServiceException(OPENID_PROVIDER_CONFIGURATION_FAILED_TO_LOAD, "IOException while requesting metadata", e);
    }
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Example 15 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 performTokenRequest.

protected HttpResponse performTokenRequest(final GenericTokenRequest request) throws IOException, ClientException {
    final String methodName = ":performTokenRequest";
    Logger.verbose(TAG + methodName, "Performing token request...");
    final String requestBody = getRequestBody(request);
    final Map<String, String> headers = new TreeMap<>();
    headers.put(CLIENT_REQUEST_ID, DiagnosticContext.getRequestContext().get(DiagnosticContext.CORRELATION_ID));
    if (request instanceof MicrosoftTokenRequest && !TextUtils.isEmpty(((MicrosoftTokenRequest) request).getBrokerVersion())) {
        headers.put(Device.PlatformIdParameters.BROKER_VERSION, ((MicrosoftTokenRequest) request).getBrokerVersion());
    }
    headers.putAll(Device.getPlatformIdParameters());
    headers.put(AuthenticationConstants.SdkPlatformFields.PRODUCT, DiagnosticContext.getRequestContext().get(AuthenticationConstants.SdkPlatformFields.PRODUCT));
    headers.put(AuthenticationConstants.SdkPlatformFields.VERSION, Device.getProductVersion());
    headers.putAll(EstsTelemetry.getInstance().getTelemetryHeaders());
    headers.put(HttpConstants.HeaderField.CONTENT_TYPE, TOKEN_REQUEST_CONTENT_TYPE);
    if (request instanceof MicrosoftTokenRequest) {
        headers.put(AuthenticationConstants.AAD.APP_PACKAGE_NAME, ((MicrosoftTokenRequest) request).getClientAppName());
        headers.put(AuthenticationConstants.AAD.APP_VERSION, ((MicrosoftTokenRequest) request).getClientAppVersion());
    }
    final URL requestUrl = new URL(getTokenEndpoint());
    final HttpResponse response = httpClient.post(requestUrl, headers, requestBody.getBytes(ObjectMapper.ENCODING_SCHEME));
    // Record the clock skew between *this device* and EVO...
    if (null != response.getDate()) {
        recordClockSkew(response.getDate().getTime());
    }
    return response;
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) TreeMap(java.util.TreeMap) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) URL(java.net.URL)

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