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