Search in sources :

Example 1 with MicrosoftStsAuthorizationErrorResponse

use of com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationErrorResponse 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)

Aggregations

Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 HttpResponse (com.microsoft.identity.common.internal.net.HttpResponse)1 MicrosoftStsAuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationErrorResponse)1 MicrosoftStsAuthorizationResponse (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResponse)1 MicrosoftStsAuthorizationResult (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResult)1 TreeMap (java.util.TreeMap)1