Search in sources :

Example 31 with AuthorizationResult

use of com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class LocalMSALController method deviceCodeFlowAuthRequest.

// Suppressing rawtype warnings due to the generic types AuthorizationResult and OAuth2Strategy
@SuppressWarnings(WarningType.rawtype_warning)
@Override
public AuthorizationResult deviceCodeFlowAuthRequest(final DeviceCodeFlowCommandParameters parameters) throws ServiceException, ClientException, IOException {
    // Logging start of method
    final String methodName = ":deviceCodeFlowAuthRequest";
    Logger.verbose(TAG + methodName, "Device Code Flow: Authorizing user code...");
    // Default scopes here
    final Set<String> mergedScopes = addDefaultScopes(parameters);
    final DeviceCodeFlowCommandParameters parametersWithScopes = parameters.toBuilder().scopes(mergedScopes).build();
    logParameters(TAG, parametersWithScopes);
    // Start telemetry with LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE
    Telemetry.emit(new ApiStartEvent().putProperties(parametersWithScopes).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
    final Authority.KnownAuthorityResult authorityResult = Authority.getKnownAuthorityResult(parametersWithScopes.getAuthority());
    // If not known throw resulting exception
    if (!authorityResult.getKnown()) {
        Telemetry.emit(new ApiEndEvent().putException(authorityResult.getClientException()).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
        throw authorityResult.getClientException();
    }
    final AuthorizationResult authorizationResult;
    try {
        // Create OAuth2Strategy using commandParameters and strategyParameters
        final OAuth2StrategyParameters strategyParameters = new OAuth2StrategyParameters();
        strategyParameters.setContext(parametersWithScopes.getAndroidApplicationContext());
        final OAuth2Strategy oAuth2Strategy = parametersWithScopes.getAuthority().createOAuth2Strategy(strategyParameters);
        // DCF protocol step 1: Get user code
        // Populate global authorization request
        mAuthorizationRequest = getAuthorizationRequest(oAuth2Strategy, parametersWithScopes);
        // Call method defined in oAuth2Strategy to request authorization
        authorizationResult = oAuth2Strategy.getDeviceCode((MicrosoftStsAuthorizationRequest) mAuthorizationRequest);
        validateServiceResult(authorizationResult);
    } catch (Exception error) {
        Telemetry.emit(new ApiEndEvent().putException(error).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
        throw error;
    }
    Logger.verbose(TAG + methodName, "Device Code Flow authorization step finished...");
    logResult(TAG, authorizationResult);
    // End telemetry with LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE
    Telemetry.emit(new ApiEndEvent().putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
    return authorizationResult;
}
Also used : MicrosoftStsAuthorizationRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationRequest) ApiEndEvent(com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent) Authority(com.microsoft.identity.common.internal.authorities.Authority) DeviceCodeFlowCommandParameters(com.microsoft.identity.common.internal.commands.parameters.DeviceCodeFlowCommandParameters) ApiStartEvent(com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) ServiceException(com.microsoft.identity.common.exception.ServiceException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) ArgumentException(com.microsoft.identity.common.exception.ArgumentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 32 with AuthorizationResult

use of com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class DeviceCodeFlowCommand method execute.

@Override
public AcquireTokenResult execute() throws Exception {
    final String methodName = ":execute";
    Logger.verbose(TAG + methodName, "Device Code Flow command initiating...");
    // Get the controller used to execute the command
    final BaseController controller = getDefaultController();
    // Fetch the parameters
    final DeviceCodeFlowCommandParameters commandParameters = (DeviceCodeFlowCommandParameters) getParameters();
    // Call deviceCodeFlowAuthRequest to get authorization result (Part 1 of DCF)
    @SuppressWarnings(WarningType.rawtype_warning) final AuthorizationResult authorizationResult = controller.deviceCodeFlowAuthRequest(commandParameters);
    // Fetch the authorization response
    final MicrosoftStsAuthorizationResponse authorizationResponse = (MicrosoftStsAuthorizationResponse) authorizationResult.getAuthorizationResponse();
    final Date expiredDate = new Date();
    try {
        long expiredInInMilliseconds = TimeUnit.SECONDS.toMillis(Long.parseLong(authorizationResponse.getExpiresIn()));
        expiredDate.setTime(expiredDate.getTime() + expiredInInMilliseconds);
    } catch (final NumberFormatException e) {
        // Shouldn't happen, but if it does, we don't want to fail the request because of this.
        Logger.error(TAG + methodName, "Failed to parse authorizationResponse.getExpiresIn()", e);
    }
    // Communicate with user app and provide authentication information
    @SuppressWarnings(WarningType.rawtype_warning) final DeviceCodeFlowCommandCallback deviceCodeFlowCommandCallback = (DeviceCodeFlowCommandCallback) getCallback();
    deviceCodeFlowCommandCallback.onUserCodeReceived(authorizationResponse.getVerificationUri(), authorizationResponse.getUserCode(), authorizationResponse.getMessage(), expiredDate);
    // Call acquireDeviceCodeFlowToken to get token result (Part 2 of DCF)
    final AcquireTokenResult tokenResult = controller.acquireDeviceCodeFlowToken(authorizationResult, commandParameters);
    Logger.verbose(TAG + methodName, "Device Code Flow command exiting with token...");
    return tokenResult;
}
Also used : AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) MicrosoftStsAuthorizationResponse(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResponse) BaseController(com.microsoft.identity.common.internal.controllers.BaseController) DeviceCodeFlowCommandParameters(com.microsoft.identity.common.internal.commands.parameters.DeviceCodeFlowCommandParameters) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) Date(java.util.Date)

Example 33 with AuthorizationResult

use of com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrowserAuthorizationStrategy method completeAuthorization.

@Override
public void completeAuthorization(int requestCode, int resultCode, Intent data) {
    if (requestCode == AuthenticationConstants.UIRequest.BROWSER_FLOW) {
        dispose();
        // Suppressing unchecked warnings due to method createAuthorizationResult being a member of the raw type AuthorizationResultFactory
        @SuppressWarnings(WarningType.unchecked_warning) final AuthorizationResult result = mOAuth2Strategy.getAuthorizationResultFactory().createAuthorizationResult(resultCode, data, mAuthorizationRequest);
        mAuthorizationResultFuture.setResult(result);
    } else {
        Logger.warnPII(TAG, "Unknown request code " + requestCode);
    }
}
Also used : AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)

Example 34 with AuthorizationResult

use of com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class CurrentTaskBrowserAuthorizationStrategy method completeAuthorization.

@Override
public void completeAuthorization(int requestCode, int resultCode, Intent data) {
    if (requestCode == AuthenticationConstants.UIRequest.BROWSER_FLOW) {
        // Suppressing unchecked warnings due to method createAuthorizationResult being a member of the raw type AuthorizationResultFactory
        @SuppressWarnings(WarningType.unchecked_warning) final AuthorizationResult result = mOAuth2Strategy.getAuthorizationResultFactory().createAuthorizationResult(resultCode, data, mAuthorizationRequest);
        mAuthorizationResultFuture.setResult(result);
        dispose();
    } else {
        Logger.warnPII(TAG, "Unknown request code " + requestCode);
    }
}
Also used : AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)

Example 35 with AuthorizationResult

use of com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockTestStrategy method requestAuthorization.

/**
 * Template method for executing an OAuth2 authorization request.
 *
 * @param request               microsoft sts authorization request.
 * @param authorizationStrategy authorization strategy.
 * @return GenericAuthorizationResponse
 */
@Override
public Future<AuthorizationResult> requestAuthorization(final MicrosoftStsAuthorizationRequest request, final AuthorizationStrategy authorizationStrategy) {
    final MockSuccessAuthorizationResultMockedTests authorizationResult = new MockSuccessAuthorizationResultMockedTests();
    final ResultFuture<AuthorizationResult> future = new ResultFuture<>();
    future.setResult(authorizationResult);
    return future;
}
Also used : ResultFuture(com.microsoft.identity.common.internal.result.ResultFuture) MockSuccessAuthorizationResultMockedTests(com.microsoft.identity.internal.testutils.mocks.MockSuccessAuthorizationResultMockedTests) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)

Aggregations

AuthorizationResult (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)35 MicrosoftAuthorizationResult (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAuthorizationResult)22 Test (org.junit.Test)21 Intent (android.content.Intent)20 MicrosoftAuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAuthorizationErrorResponse)20 AuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationErrorResponse)20 AzureActiveDirectoryAuthorizationResult (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAuthorizationResult)11 MicrosoftStsAuthorizationResult (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResult)11 AzureActiveDirectoryAuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAuthorizationErrorResponse)10 Bundle (android.os.Bundle)8 ClientException (com.microsoft.identity.common.exception.ClientException)5 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)4 ServiceException (com.microsoft.identity.common.exception.ServiceException)3 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)3 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)3 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)3 ResultFuture (com.microsoft.identity.common.internal.result.ResultFuture)3 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)3 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)3 ArgumentException (com.microsoft.identity.common.exception.ArgumentException)2