Search in sources :

Example 16 with ServiceException

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

the class AzureActiveDirectoryOAuth2Strategy method createAccount.

/**
 * Stubbed out for now, but should create a new AzureActiveDirectory account.
 * Should accept a parameter (TokenResponse) for producing that user
 *
 * @return
 */
@Override
public AzureActiveDirectoryAccount createAccount(@NonNull final AzureActiveDirectoryTokenResponse response) {
    final String methodName = "createAccount";
    IDToken idToken = null;
    ClientInfo clientInfo = null;
    try {
        Logger.info(TAG, "Constructing IDToken from response");
        idToken = new IDToken(response.getIdToken());
        Logger.info(TAG, "Constructing ClientInfo from response");
        clientInfo = new ClientInfo(response.getClientInfo());
    } catch (ServiceException ccse) {
        Logger.error(TAG + ":" + methodName, "Failed to construct IDToken or ClientInfo", null);
        Logger.errorPII(TAG + ":" + methodName, "Failed with Exception", ccse);
        throw new RuntimeException();
    }
    final AzureActiveDirectoryAccount account = new AzureActiveDirectoryAccount(idToken, clientInfo);
    Logger.info(TAG, "Account created");
    Logger.infoPII(TAG, account.toString());
    return account;
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken)

Example 17 with ServiceException

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

the class LocalMSALController method deviceCodeFlowAuthRequest.

// Suppressing rawtype warnings due to the generic types AuthorizationResult and OAuth2Strategy
@SuppressWarnings(WarningType.rawtype_warning)
@Override
public AuthorizationResult deviceCodeFlowAuthRequest(final DeviceCodeFlowCommandParameters parameters) throws ServiceException, ClientException, IOException {
    // Logging start of method
    final String methodName = ":deviceCodeFlowAuthRequest";
    Logger.verbose(TAG + methodName, "Device Code Flow: Authorizing user code...");
    // Default scopes here
    final Set<String> mergedScopes = addDefaultScopes(parameters);
    final DeviceCodeFlowCommandParameters parametersWithScopes = parameters.toBuilder().scopes(mergedScopes).build();
    logParameters(TAG, parametersWithScopes);
    // Start telemetry with LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE
    Telemetry.emit(new ApiStartEvent().putProperties(parametersWithScopes).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
    final Authority.KnownAuthorityResult authorityResult = Authority.getKnownAuthorityResult(parametersWithScopes.getAuthority());
    // If not known throw resulting exception
    if (!authorityResult.getKnown()) {
        Telemetry.emit(new ApiEndEvent().putException(authorityResult.getClientException()).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
        throw authorityResult.getClientException();
    }
    final AuthorizationResult authorizationResult;
    try {
        // Create OAuth2Strategy using commandParameters and strategyParameters
        final OAuth2StrategyParameters strategyParameters = new OAuth2StrategyParameters();
        strategyParameters.setContext(parametersWithScopes.getAndroidApplicationContext());
        final OAuth2Strategy oAuth2Strategy = parametersWithScopes.getAuthority().createOAuth2Strategy(strategyParameters);
        // DCF protocol step 1: Get user code
        // Populate global authorization request
        mAuthorizationRequest = getAuthorizationRequest(oAuth2Strategy, parametersWithScopes);
        // Call method defined in oAuth2Strategy to request authorization
        authorizationResult = oAuth2Strategy.getDeviceCode((MicrosoftStsAuthorizationRequest) mAuthorizationRequest);
        validateServiceResult(authorizationResult);
    } catch (Exception error) {
        Telemetry.emit(new ApiEndEvent().putException(error).putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
        throw error;
    }
    Logger.verbose(TAG + methodName, "Device Code Flow authorization step finished...");
    logResult(TAG, authorizationResult);
    // End telemetry with LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE
    Telemetry.emit(new ApiEndEvent().putApiId(TelemetryEventStrings.Api.LOCAL_DEVICE_CODE_FLOW_ACQUIRE_URL_AND_CODE));
    return authorizationResult;
}
Also used : MicrosoftStsAuthorizationRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationRequest) ApiEndEvent(com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent) Authority(com.microsoft.identity.common.internal.authorities.Authority) DeviceCodeFlowCommandParameters(com.microsoft.identity.common.internal.commands.parameters.DeviceCodeFlowCommandParameters) ApiStartEvent(com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) ServiceException(com.microsoft.identity.common.exception.ServiceException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) ArgumentException(com.microsoft.identity.common.exception.ArgumentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with ServiceException

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

the class EstsTelemetry method isTelemetryLoggedByServer.

private boolean isTelemetryLoggedByServer(@SuppressWarnings(WarningType.rawtype_warning) @NonNull final BaseCommand command, @NonNull final CommandResult commandResult) {
    // This was a local operation - we didn't reach token endpoint and hence telemetry wasn't sent
    if (!(command instanceof TokenCommand)) {
        return false;
    }
    if (commandResult.getStatus() == CommandResult.ResultStatus.ERROR) {
        BaseException baseException = (BaseException) commandResult.getResult();
        if (!(baseException instanceof ServiceException)) {
            // (request did not reach token endpoint)
            return false;
        } else {
            final ServiceException serviceException = (ServiceException) baseException;
            final int statusCode = serviceException.getHttpStatusCode();
            // for these status codes, headers aren't logged by ests
            return !(statusCode == ServiceException.DEFAULT_STATUS_CODE || statusCode == 429 || statusCode >= 500);
        }
    } else if (commandResult.getStatus() == CommandResult.ResultStatus.CANCEL) {
        // we did not go to token endpoint
        return false;
    } else if (commandResult.getStatus() == CommandResult.ResultStatus.COMPLETED) {
        if (commandResult.getResult() instanceof ILocalAuthenticationResult) {
            final ILocalAuthenticationResult localAuthenticationResult = (ILocalAuthenticationResult) commandResult.getResult();
            if (localAuthenticationResult.isServicedFromCache()) {
                // we did not go to token endpoint
                return false;
            }
        } else {
            // command probably wasn't a token command - we should never get here in that case
            return false;
        }
    }
    // if we get here that means we went to token endpoint and headers were logged by sts
    return true;
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) ServiceException(com.microsoft.identity.common.exception.ServiceException) TokenCommand(com.microsoft.identity.common.internal.commands.TokenCommand) ILocalAuthenticationResult(com.microsoft.identity.common.internal.result.ILocalAuthenticationResult)

Example 19 with ServiceException

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

the class SchemaUtil method getCredentialTypeFromVersion.

public static String getCredentialTypeFromVersion(@Nullable final String idTokenString) {
    final String methodName = "getCredentialTypeFromVersion";
    // Default is v2
    String idTokenVersion = CredentialType.IdToken.name();
    if (!TextUtils.isEmpty(idTokenString)) {
        IDToken idToken;
        try {
            idToken = new IDToken(idTokenString);
            final Map<String, ?> idTokenClaims = idToken.getTokenClaims();
            final String aadVersion = (String) idTokenClaims.get(AuthenticationConstants.OAuth2.AAD_VERSION);
            if (AuthenticationConstants.OAuth2.AAD_VERSION_V1.equalsIgnoreCase(aadVersion)) {
                idTokenVersion = CredentialType.V1IdToken.name();
            }
        } catch (ServiceException e) {
            Logger.warn(TAG + ":" + methodName, EXCEPTION_CONSTRUCTING_IDTOKEN + e.getMessage());
        }
    }
    return idTokenVersion;
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken)

Example 20 with ServiceException

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

the class SchemaUtil method getIdentityProvider.

public static String getIdentityProvider(final String idTokenString) {
    final String methodName = "getIdentityProvider";
    String idp = null;
    if (null != idTokenString) {
        IDToken idToken;
        try {
            idToken = new IDToken(idTokenString);
            final Map<String, ?> idTokenClaims = idToken.getTokenClaims();
            if (null != idTokenClaims) {
                // IDP claim is present only in case of guest scenerio and is empty for home tenants.
                // Few Apps consuming ADAL use this to differentiate between home vs guest accounts.
                idp = (String) idTokenClaims.get(AzureActiveDirectoryIdToken.IDENTITY_PROVIDER);
                Logger.verbosePII(TAG + ":" + methodName, "idp: " + idp);
                if (null == idp) {
                    Logger.info(TAG + ":" + methodName, "idp claim was null.");
                }
            } else {
                Logger.warn(TAG + ":" + methodName, "IDToken claims were null.");
            }
        } catch (ServiceException e) {
            Logger.warn(TAG + ":" + methodName, EXCEPTION_CONSTRUCTING_IDTOKEN + e.getMessage());
        }
    } else {
        Logger.warn(TAG + ":" + methodName, "IDToken was null.");
    }
    return idp;
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken)

Aggregations

ServiceException (com.microsoft.identity.common.exception.ServiceException)23 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)8 IDToken (com.microsoft.identity.common.internal.providers.oauth2.IDToken)7 ClientException (com.microsoft.identity.common.exception.ClientException)4 Nullable (androidx.annotation.Nullable)3 IOException (java.io.IOException)3 URL (java.net.URL)3 NonNull (androidx.annotation.NonNull)2 ArgumentException (com.microsoft.identity.common.exception.ArgumentException)2 BaseException (com.microsoft.identity.common.exception.BaseException)2 BrokerResult (com.microsoft.identity.common.internal.broker.BrokerResult)2 AuthorizationResult (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)2 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)2 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)2 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)2 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)2 JSONException (org.json.JSONException)2 Bundle (android.os.Bundle)1 JsonObject (com.google.gson.JsonObject)1 JWSBuilder (com.microsoft.identity.common.adal.internal.JWSBuilder)1