Search in sources :

Example 1 with BrokerResultFuture

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

the class BrokerMsalController method acquireToken.

/**
 * Performs interactive acquire token with Broker.
 *
 * @param parameters a {@link InteractiveTokenCommandParameters}
 * @return an {@link AcquireTokenResult}.
 */
@Override
public AcquireTokenResult acquireToken(@NonNull final InteractiveTokenCommandParameters parameters) throws BaseException, InterruptedException {
    Telemetry.emit(new ApiStartEvent().putProperties(parameters).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
    // Create BrokerResultFuture to block on response from the broker... response will be return as an activity result
    // BrokerActivity will receive the result and ask the API dispatcher to complete the request
    // In completeAcquireToken below we will set the result on the future and unblock the flow.
    mBrokerResultFuture = new BrokerResultFuture();
    // Get the broker interactive parameters intent
    final Intent interactiveRequestIntent = getBrokerAuthorizationIntent(parameters);
    // Pass this intent to the BrokerActivity which will be used to start this activity
    final Intent brokerActivityIntent = new Intent(parameters.getAndroidApplicationContext(), BrokerActivity.class);
    brokerActivityIntent.putExtra(BrokerActivity.BROKER_INTENT, interactiveRequestIntent);
    if (null == parameters.getActivity()) {
        // To support calling from OneAuth-MSAL, which may be initialized without an Activity
        // add Flags to start as a NEW_TASK if we are launching from an application Context
        brokerActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mApplicationContext.startActivity(brokerActivityIntent);
    } else {
        // Start the BrokerActivity using our existing Activity
        parameters.getActivity().startActivity(brokerActivityIntent);
    }
    // Wait to be notified of the result being returned... we could add a timeout here if we want to
    final Bundle resultBundle = mBrokerResultFuture.get();
    // If the request is from MSALCPP , OAuth2TokenCache will be null.
    if (parameters.getOAuth2TokenCache() != null) {
        saveMsaAccountToCache(resultBundle, (MsalOAuth2TokenCache) parameters.getOAuth2TokenCache());
    }
    final AcquireTokenResult result;
    try {
        result = new MsalBrokerResultAdapter().getAcquireTokenResultFromResultBundle(resultBundle);
    } catch (BaseException e) {
        Telemetry.emit(new ApiEndEvent().putException(e).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
        throw e;
    }
    Telemetry.emit(new ApiEndEvent().putResult(result).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
    return result;
}
Also used : AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) MsalBrokerResultAdapter(com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter) BaseException(com.microsoft.identity.common.exception.BaseException) ApiEndEvent(com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent) Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) ApiStartEvent(com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent) BrokerResultFuture(com.microsoft.identity.common.internal.broker.BrokerResultFuture) Intent(android.content.Intent)

Aggregations

Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 BaseException (com.microsoft.identity.common.exception.BaseException)1 BrokerResultFuture (com.microsoft.identity.common.internal.broker.BrokerResultFuture)1 BrokerOperationBundle (com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 MsalBrokerResultAdapter (com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter)1 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)1 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)1