Search in sources :

Example 1 with BrokerResult

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

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

the class MsalBrokerResultAdapter method getBaseExceptionFromBundle.

@Override
@NonNull
public BaseException getBaseExceptionFromBundle(@NonNull final Bundle resultBundle) {
    Logger.info(TAG, "Constructing exception from result bundle");
    final BrokerResult brokerResult;
    try {
        brokerResult = brokerResultFromBundle(resultBundle);
    } catch (final ClientException e) {
        return e;
    }
    final String exceptionType = brokerResult.getExceptionType();
    if (!StringUtil.isEmpty(exceptionType)) {
        return getBaseExceptionFromExceptionType(exceptionType, brokerResult);
    } else {
        // This code is here for legacy purposes where old versions of broker (3.1.8 or below)
        // wouldn't return exception type in the result.
        Logger.info(TAG, "Exception type is not returned from the broker, " + "using error codes to transform to the right exception");
        return getBaseExceptionFromErrorCodes(brokerResult);
    }
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) ClientException(com.microsoft.identity.common.exception.ClientException) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) NonNull(androidx.annotation.NonNull)

Example 3 with BrokerResult

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

the class MsalBrokerResultAdapter method verifyRemoveAccountResultFromBundle.

public void verifyRemoveAccountResultFromBundle(@Nullable final Bundle bundle) throws BaseException {
    if (bundle == null) {
        // Backward compatibility. We treated null = success.
        return;
    }
    final String brokerResultString = bundle.getString(AuthenticationConstants.Broker.BROKER_RESULT_V2);
    if (StringUtil.isEmpty(brokerResultString)) {
        throw getBaseExceptionFromBundle(bundle);
    }
    final BrokerResult brokerResult = JsonExtensions.getBrokerResultFromJsonString(brokerResultString);
    if (brokerResult != null && brokerResult.isSuccess()) {
        return;
    }
    Logger.warn(TAG, "Failed to remove account.");
    throw getBaseExceptionFromBundle(bundle);
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString)

Example 4 with BrokerResult

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

the class MsalBrokerResultAdapter method authenticationResultFromBundle.

@Override
@NonNull
public ILocalAuthenticationResult authenticationResultFromBundle(@NonNull final Bundle resultBundle) throws ClientException {
    final BrokerResult brokerResult = brokerResultFromBundle(resultBundle);
    Logger.info(TAG, "Broker Result returned from Bundle, constructing authentication result");
    final List<ICacheRecord> tenantProfileCacheRecords = brokerResult.getTenantProfileData();
    if (tenantProfileCacheRecords == null) {
        Logger.error(TAG, "getTenantProfileData is null", null);
        throw new ClientException(INVALID_BROKER_BUNDLE, "getTenantProfileData is null.");
    }
    return new LocalAuthenticationResult(tenantProfileCacheRecords.get(0), tenantProfileCacheRecords, SdkType.MSAL, brokerResult.isServicedFromCache());
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 5 with BrokerResult

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

the class MsalBrokerResultAdapter method verifyHelloFromResultBundle.

@NonNull
public String verifyHelloFromResultBundle(@Nullable final Bundle bundle) throws ClientException {
    final String methodName = ":verifyHelloFromResultBundle";
    // This means that the Broker doesn't support hello().
    if (bundle == null) {
        Logger.warn(TAG + methodName, "The hello result bundle is null.");
        throw new ClientException(ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_CODE, ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_MESSAGE);
    }
    final String negotiatedBrokerProtocolVersion = bundle.getString(AuthenticationConstants.Broker.NEGOTIATED_BP_VERSION_KEY);
    if (!StringUtil.isEmpty(negotiatedBrokerProtocolVersion)) {
        Logger.info(TAG + methodName, "Able to establish the connect, " + "the broker protocol version in common is [" + negotiatedBrokerProtocolVersion + "]");
        return negotiatedBrokerProtocolVersion;
    }
    if (!StringUtil.isEmpty(bundle.getString(AuthenticationConstants.OAuth2.ERROR)) && !StringUtil.isEmpty(bundle.getString(AuthenticationConstants.OAuth2.ERROR_DESCRIPTION))) {
        final String errorCode = bundle.getString(AuthenticationConstants.OAuth2.ERROR);
        final String errorMessage = bundle.getString(AuthenticationConstants.OAuth2.ERROR_DESCRIPTION);
        throw new ClientException(errorCode, errorMessage);
    }
    final Object resultObject = bundle.get(AuthenticationConstants.Broker.BROKER_RESULT_V2);
    if (resultObject instanceof BrokerResult) {
        // for the back compatibility purpose to version 3.0.4 and 3.0.6.
        final BrokerResult brokerResult = (BrokerResult) resultObject;
        throw new ClientException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    }
    // This means that the Broker doesn't support hello().
    Logger.warn(TAG + methodName, "The result bundle is not in a recognizable format.");
    throw new ClientException(ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_CODE, ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_MESSAGE);
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Aggregations

BrokerResult (com.microsoft.identity.common.internal.broker.BrokerResult)6 NonNull (androidx.annotation.NonNull)4 ClientException (com.microsoft.identity.common.exception.ClientException)4 GzipUtil.compressString (com.microsoft.identity.common.internal.util.GzipUtil.compressString)3 Bundle (android.os.Bundle)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)1 IAccountRecord (com.microsoft.identity.common.internal.dto.IAccountRecord)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 MsalBrokerResultAdapter (com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter)1