Search in sources :

Example 1 with MsalBrokerResultAdapter

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

the class BrokerMsalController method saveMsaAccountToCache.

/**
 * Checks if the account returns is a MSA Account and sets single on state in cache
 */
private void saveMsaAccountToCache(@NonNull final Bundle resultBundle, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final MsalOAuth2TokenCache msalOAuth2TokenCache) throws BaseException {
    final String methodName = ":saveMsaAccountToCache";
    final BrokerResult brokerResult = new MsalBrokerResultAdapter().brokerResultFromBundle(resultBundle);
    if (resultBundle.getBoolean(AuthenticationConstants.Broker.BROKER_REQUEST_V2_SUCCESS) && AzureActiveDirectoryAudience.MSA_MEGA_TENANT_ID.equalsIgnoreCase(brokerResult.getTenantId())) {
        Logger.info(TAG + methodName, "Result returned for MSA Account, saving to cache");
        if (StringUtil.isEmpty(brokerResult.getClientInfo())) {
            Logger.error(TAG + methodName, "ClientInfo is empty.", null);
            throw new ClientException(ErrorStrings.UNKNOWN_ERROR, "ClientInfo is empty.");
        }
        try {
            final ClientInfo clientInfo = new ClientInfo(brokerResult.getClientInfo());
            final MicrosoftStsAccount microsoftStsAccount = new MicrosoftStsAccount(new IDToken(brokerResult.getIdToken()), clientInfo);
            microsoftStsAccount.setEnvironment(brokerResult.getEnvironment());
            final MicrosoftRefreshToken microsoftRefreshToken = new MicrosoftRefreshToken(brokerResult.getRefreshToken(), clientInfo, brokerResult.getScope(), brokerResult.getClientId(), brokerResult.getEnvironment(), brokerResult.getFamilyId());
            msalOAuth2TokenCacheSetSingleSignOnState(msalOAuth2TokenCache, microsoftStsAccount, microsoftRefreshToken);
        } catch (ServiceException e) {
            Logger.errorPII(TAG + methodName, "Exception while creating Idtoken or ClientInfo," + " cannot save MSA account tokens", e);
            throw new ClientException(ErrorStrings.INVALID_JWT, e.getMessage(), e);
        }
    }
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) MsalBrokerResultAdapter(com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter) MicrosoftStsAccount(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAccount) MicrosoftRefreshToken(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftRefreshToken) ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken) ClientException(com.microsoft.identity.common.exception.ClientException) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)

Example 2 with MsalBrokerResultAdapter

use of com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter 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

MsalBrokerResultAdapter (com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter)2 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 BaseException (com.microsoft.identity.common.exception.BaseException)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 BrokerResult (com.microsoft.identity.common.internal.broker.BrokerResult)1 BrokerResultFuture (com.microsoft.identity.common.internal.broker.BrokerResultFuture)1 BrokerOperationBundle (com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle)1 MicrosoftRefreshToken (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftRefreshToken)1 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)1 MicrosoftStsAccount (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAccount)1 IDToken (com.microsoft.identity.common.internal.providers.oauth2.IDToken)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)1 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)1