use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.
the class Authority method getKnownAuthorityResult.
public static KnownAuthorityResult getKnownAuthorityResult(Authority authority) {
final String methodName = ":getKnownAuthorityResult";
Logger.verbose(TAG + methodName, "Getting known authority result...");
ClientException clientException = null;
boolean known = false;
try {
Logger.info(TAG + methodName, "Performing cloud discovery");
performCloudDiscovery();
} catch (final IOException ex) {
clientException = new ClientException(ClientException.IO_ERROR, "Unable to perform cloud discovery", ex);
}
Logger.info(TAG + methodName, "Cloud discovery complete.");
if (clientException == null) {
if (!isKnownAuthority(authority)) {
clientException = new ClientException(ClientException.UNKNOWN_AUTHORITY, "Provided authority is not known. MSAL will only make requests to known authorities");
} else {
Logger.info(TAG + methodName, "Cloud is known.");
known = true;
}
}
return new KnownAuthorityResult(known, clientException);
}
use of com.microsoft.identity.common.exception.ClientException 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.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.
the class BrokerOperationExecutor method execute.
/**
* A generic method that would initialize and iterate through available strategies.
* It will return a result immediately if any of the strategy succeeds, or throw an exception if all of the strategies fails.
*/
public <T extends CommandParameters, U> U execute(@Nullable final T parameters, @NonNull final BrokerOperation<U> operation) throws BaseException {
final String methodName = ":execute";
emitOperationStartEvent(parameters, operation);
if (mStrategies.size() == 0) {
final ClientException exception = new ClientException(ErrorStrings.BROKER_BIND_SERVICE_FAILED, "No strategies can be used to connect to the broker.");
emitOperationFailureEvent(operation, exception);
throw exception;
}
final List<BrokerCommunicationException> communicationExceptionStack = new ArrayList<>();
for (final IIpcStrategy strategy : mStrategies) {
try {
final U result = performStrategy(strategy, operation);
emitOperationSuccessEvent(operation, result);
return result;
} catch (final BrokerCommunicationException communicationException) {
// Fails to communicate to the . Try next strategy.
communicationExceptionStack.add(communicationException);
} catch (final BaseException exception) {
emitOperationFailureEvent((BrokerOperation<U>) operation, exception);
throw exception;
}
}
final ClientException exception = new ClientException(ErrorStrings.BROKER_BIND_SERVICE_FAILED, "Unable to connect to the broker. Please refer to MSAL/Broker logs " + "or suppressed exception (API 19+) for more details.");
// This means that we've tried every strategies... log everything...
for (final BrokerCommunicationException e : communicationExceptionStack) {
Logger.error(TAG + methodName, e.getMessage(), e);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
exception.addSuppressed(e);
}
}
emitOperationFailureEvent(operation, exception);
throw exception;
}
use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.
the class ExceptionAdapter method exceptionFromAcquireTokenResult.
@Nullable
public static BaseException exceptionFromAcquireTokenResult(final AcquireTokenResult result, final CommandParameters commandParameters) {
final String methodName = ":exceptionFromAcquireTokenResult";
@SuppressWarnings(WarningType.rawtype_warning) final AuthorizationResult authorizationResult = result.getAuthorizationResult();
if (null != authorizationResult) {
final AuthorizationErrorResponse authorizationErrorResponse = authorizationResult.getAuthorizationErrorResponse();
if (!authorizationResult.getSuccess()) {
// THERE ARE CURRENTLY NO USAGES of INVALID_REQUEST
switch(result.getAuthorizationResult().getAuthorizationStatus()) {
case FAIL:
// Check if the error is to register device and throw DEVICE_REGISTRATION_NEEDED exception
if (authorizationErrorResponse instanceof MicrosoftAuthorizationErrorResponse) {
MicrosoftAuthorizationErrorResponse microsoftAuthorizationErrorResponse = (MicrosoftAuthorizationErrorResponse) authorizationErrorResponse;
if (microsoftAuthorizationErrorResponse.getError().equals(MicrosoftAuthorizationErrorResponse.DEVICE_REGISTRATION_NEEDED)) {
return new DeviceRegistrationRequiredException(microsoftAuthorizationErrorResponse.getError(), microsoftAuthorizationErrorResponse.getErrorDescription(), microsoftAuthorizationErrorResponse.getUserName());
}
}
return new ServiceException(authorizationErrorResponse.getError(), authorizationErrorResponse.getErrorDescription(), ServiceException.DEFAULT_STATUS_CODE, null);
case SDK_CANCEL:
return new ClientException(authorizationErrorResponse.getError(), authorizationErrorResponse.getErrorDescription());
case USER_CANCEL:
return new UserCancelException();
}
}
} else {
Logger.warn(TAG + methodName, "AuthorizationResult was null -- expected for ATS cases.");
}
return exceptionFromTokenResult(result.getTokenResult(), commandParameters);
}
use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.
the class AndroidKeystoreAsymmetricRsaKeyFactory method generateAsymmetricKey.
@Override
public synchronized AsymmetricRsaKey generateAsymmetricKey(@NonNull final String alias) throws ClientException {
final Exception exception;
final String errCode;
try {
return new AndroidKeystoreAsymmetricRsaKey(mContext, new DevicePopManager(alias), alias);
} catch (final KeyStoreException e) {
exception = e;
errCode = KEYSTORE_NOT_INITIALIZED;
} catch (final CertificateException e) {
exception = e;
errCode = CERTIFICATE_LOAD_FAILURE;
} catch (final NoSuchAlgorithmException e) {
exception = e;
errCode = NO_SUCH_ALGORITHM;
} catch (final IOException e) {
exception = e;
errCode = IO_ERROR;
}
throw new ClientException(errCode, exception.getMessage(), exception);
}
Aggregations