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