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);
}
}
}
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);
}
}
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);
}
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());
}
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);
}
Aggregations