Search in sources :

Example 71 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerActivity method returnsExceptionOnActivityUnexpectedlyKilled.

private void returnsExceptionOnActivityUnexpectedlyKilled() {
    final IBrokerResultAdapter resultAdapter = BrokerResultAdapterFactory.getBrokerResultAdapter(SdkType.MSAL);
    final Bundle resultBundle = resultAdapter.bundleFromBaseException(new ClientException(ErrorStrings.BROKER_REQUEST_CANCELLED, "The activity is killed unexpectedly."), null);
    final Intent data = new Intent();
    data.putExtras(resultBundle);
    data.setAction(RETURN_INTERACTIVE_REQUEST_RESULT);
    data.putExtra(REQUEST_CODE, AuthenticationConstants.UIRequest.BROWSER_FLOW);
    data.putExtra(RESULT_CODE, AuthenticationConstants.UIResponse.BROWSER_CODE_CANCEL);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(data);
}
Also used : IBrokerResultAdapter(com.microsoft.identity.common.internal.result.IBrokerResultAdapter) Bundle(android.os.Bundle) Intent(android.content.Intent) ClientException(com.microsoft.identity.common.exception.ClientException)

Example 72 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class AuthorizationStrategyFactory method getAuthorizationStrategy.

public GenericAuthorizationStrategy getAuthorizationStrategy(@NonNull final InteractiveTokenCommandParameters parameters) {
    final Context context = parameters.getAndroidApplicationContext();
    // Valid if available browser installed. Will fallback to embedded webView if no browser available.
    final AuthorizationAgent validatedAuthorizationAgent = validAuthorizationAgent(parameters.getAuthorizationAgent(), context);
    boolean isBrokerRequest = (parameters instanceof BrokerInteractiveTokenCommandParameters);
    if (validatedAuthorizationAgent == AuthorizationAgent.WEBVIEW) {
        Logger.info(TAG, "Use webView for authorization.");
        return getGenericAuthorizationStrategy(parameters, context);
    } else if (validatedAuthorizationAgent == AuthorizationAgent.DEFAULT) {
        // Fall back to webview if no browser found.
        try {
            BrowserSelector.select(context, parameters.getBrowserSafeList());
        } catch (final ClientException exception) {
            Logger.info(TAG, "No supported browser available found. Fallback to the webView authorization agent.");
            if (ErrorStrings.NO_AVAILABLE_BROWSER_FOUND.equalsIgnoreCase(exception.getErrorCode())) {
                return getGenericAuthorizationStrategy(parameters, context);
            }
        }
        Logger.info(TAG, "Use browser for authorization.");
        return getBrowserAuthorizationStrategy(context, parameters.getActivity(), parameters.getFragment(), isBrokerRequest, parameters.getBrowserSafeList());
    } else {
        Logger.info(TAG, "Use browser for authorization.");
        return getBrowserAuthorizationStrategy(context, parameters.getActivity(), parameters.getFragment(), isBrokerRequest, parameters.getBrowserSafeList());
    }
}
Also used : Context(android.content.Context) ClientException(com.microsoft.identity.common.exception.ClientException) BrokerInteractiveTokenCommandParameters(com.microsoft.identity.common.internal.commands.parameters.BrokerInteractiveTokenCommandParameters)

Example 73 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class PKeyAuthChallengeHandler method getChallengeHeader.

public static Map<String, String> getChallengeHeader(final PKeyAuthChallenge pKeyAuthChallenge) throws ClientException {
    String authorizationHeaderValue = String.format("%s Context=\"%s\",Version=\"%s\"", AuthenticationConstants.Broker.CHALLENGE_RESPONSE_TYPE, pKeyAuthChallenge.getContext(), pKeyAuthChallenge.getVersion());
    // If not device cert exists, alias or private key will not exist on the device
    // Suppressing unchecked warnings due to the generic type not provided in the object returned from method getDeviceCertificateProxy
    @SuppressWarnings(WarningType.unchecked_warning) Class<IDeviceCertificate> certClazz = (Class<IDeviceCertificate>) AuthenticationSettings.INSTANCE.getDeviceCertificateProxy();
    if (certClazz != null) {
        IDeviceCertificate deviceCertProxy = getWPJAPIInstance(certClazz);
        if (deviceCertProxy.isValidIssuer(pKeyAuthChallenge.getCertAuthorities()) || StringUtil.equalsIgnoreCase(deviceCertProxy.getThumbPrint(), pKeyAuthChallenge.getThumbprint())) {
            RSAPrivateKey privateKey = deviceCertProxy.getRSAPrivateKey();
            if (privateKey == null) {
                throw new ClientException(ErrorStrings.KEY_CHAIN_PRIVATE_KEY_EXCEPTION);
            }
            final String jwt = (new JWSBuilder()).generateSignedJWT(pKeyAuthChallenge.getNonce(), pKeyAuthChallenge.getSubmitUrl(), privateKey, deviceCertProxy.getRSAPublicKey(), deviceCertProxy.getCertificate());
            authorizationHeaderValue = String.format("%s AuthToken=\"%s\",Context=\"%s\",Version=\"%s\"", AuthenticationConstants.Broker.CHALLENGE_RESPONSE_TYPE, jwt, pKeyAuthChallenge.getContext(), pKeyAuthChallenge.getVersion());
            Logger.info(TAG, "Receive challenge response. ");
        }
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put(AuthenticationConstants.Broker.CHALLENGE_RESPONSE_HEADER, authorizationHeaderValue);
    return headers;
}
Also used : IDeviceCertificate(com.microsoft.identity.common.adal.internal.IDeviceCertificate) HashMap(java.util.HashMap) ClientException(com.microsoft.identity.common.exception.ClientException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) JWSBuilder(com.microsoft.identity.common.adal.internal.JWSBuilder)

Example 74 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class ConfidentialClientHelper method requestAccessTokenForAutomation.

/**
 * Yep.  Hardcoding this method to retrieve access token for MSIDLABS
 */
private String requestAccessTokenForAutomation() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    String accessToken = null;
    final TokenRequest tokenRequest = this.createTokenRequest();
    tokenRequest.setGrantType(CLIENT_CREDENTIALS);
    final AccountsInOneOrganization aadAudience = new AccountsInOneOrganization(TENANT_ID);
    final AzureActiveDirectoryAuthority authority = new AzureActiveDirectoryAuthority(aadAudience);
    try {
        final OAuth2StrategyParameters strategyParameters = new OAuth2StrategyParameters();
        OAuth2Strategy<AccessToken, BaseAccount, AuthorizationRequest, AuthorizationRequest.Builder, AuthorizationStrategy, OAuth2Configuration, OAuth2StrategyParameters, AuthorizationResponse, RefreshToken, TokenRequest, TokenResponse, TokenResult, AuthorizationResult> strategy = authority.createOAuth2Strategy(strategyParameters);
        TokenResult tokenResult = strategy.requestToken(tokenRequest);
        if (tokenResult.getSuccess()) {
            accessToken = tokenResult.getTokenResponse().getAccessToken();
        } else {
            throw new RuntimeException(tokenResult.getErrorResponse().getErrorDescription());
        }
    } catch (final ClientException e) {
        e.printStackTrace();
    }
    return accessToken;
}
Also used : AuthorizationStrategy(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationStrategy) AuthorizationRequest(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationRequest) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) AuthorizationResponse(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResponse) RefreshToken(com.microsoft.identity.common.internal.providers.oauth2.RefreshToken) TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) AccessToken(com.microsoft.identity.common.internal.providers.oauth2.AccessToken) BaseAccount(com.microsoft.identity.common.BaseAccount) AccountsInOneOrganization(com.microsoft.identity.common.internal.authorities.AccountsInOneOrganization) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) AzureActiveDirectoryAuthority(com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority) OAuth2Configuration(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Configuration) ClientException(com.microsoft.identity.common.exception.ClientException)

Aggregations

ClientException (com.microsoft.identity.common.exception.ClientException)74 IOException (java.io.IOException)23 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)23 InvalidKeyException (java.security.InvalidKeyException)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 KeyStoreException (java.security.KeyStoreException)17 BadPaddingException (javax.crypto.BadPaddingException)17 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)17 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)17 UnrecoverableEntryException (java.security.UnrecoverableEntryException)15 CertificateException (java.security.cert.CertificateException)13 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 SignatureException (java.security.SignatureException)11 KeyPermanentlyInvalidatedException (android.security.keystore.KeyPermanentlyInvalidatedException)10 StrongBoxUnavailableException (android.security.keystore.StrongBoxUnavailableException)10 NonNull (androidx.annotation.NonNull)10 JOSEException (com.nimbusds.jose.JOSEException)10 NoSuchProviderException (java.security.NoSuchProviderException)10 ProviderException (java.security.ProviderException)10 JSONException (org.json.JSONException)10