use of com.microsoft.identity.common.exception.ServiceException in project microsoft-authentication-library-common-for-android by AzureAD.
the class IDToken method parseJWT.
public static Map<String, ?> parseJWT(@NonNull final String rawIdToken) throws ServiceException {
final String methodName = ":getClaims(String)";
final Map<String, Object> result = new HashMap<>();
try {
final JWT jwt = JWTParser.parse(rawIdToken);
final JWTClaimsSet claimsSet = jwt.getJWTClaimsSet();
result.putAll(claimsSet.getClaims());
} catch (ParseException e) {
Logger.error(TAG + methodName, "Failed to parse IdToken", e);
throw new ServiceException("Failed to parse JWT", ErrorStrings.INVALID_JWT, e);
}
return result;
}
use of com.microsoft.identity.common.exception.ServiceException in project microsoft-authentication-library-common-for-android by AzureAD.
the class MicrosoftStsOAuth2Strategy method createAccount.
@Override
public MicrosoftStsAccount createAccount(@NonNull final MicrosoftStsTokenResponse response) {
final String methodName = ":createAccount";
Logger.verbose(TAG + methodName, "Creating account from TokenResponse...");
IDToken idToken = null;
ClientInfo clientInfo = null;
try {
idToken = new IDToken(response.getIdToken());
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();
}
MicrosoftStsAccount account = new MicrosoftStsAccount(idToken, clientInfo);
account.setEnvironment(getIssuerCacheIdentifierFromTokenEndpoint());
return account;
}
use of com.microsoft.identity.common.exception.ServiceException in project microsoft-authentication-library-common-for-android by AzureAD.
the class BrokerOperationExecutorTests method expectServiceException.
private void expectServiceException(final List<IIpcStrategy> strategyList) {
try {
final BrokerOperationExecutor executor = new BrokerOperationExecutor(strategyList);
executor.execute(getMockParameter(), getBrokerOperation());
Assert.fail("Failure is expected.");
} catch (final BaseException e) {
Assert.assertTrue(e instanceof ServiceException);
Assert.assertEquals(e.getErrorCode(), SERVICE_EXCEPTION_BUNDLE_ERROR_CODE);
}
}
use of com.microsoft.identity.common.exception.ServiceException in project microsoft-authentication-library-common-for-android by AzureAD.
the class SchemaUtil method getTenantId.
/**
* Get tenant id claim from Id token , if not present returns the tenant id from client info
*
* @param clientInfoString : ClientInfo
* @param idTokenString : Id Token
* @return tenantId
*/
@Nullable
public static String getTenantId(@Nullable final String clientInfoString, @Nullable final String idTokenString) {
String tenantId = null;
try {
if (!TextUtils.isEmpty(idTokenString) && !TextUtils.isEmpty(clientInfoString)) {
final IDToken idToken = new IDToken(idTokenString);
final ClientInfo clientInfo = new ClientInfo(clientInfoString);
final Map<String, ?> claims = idToken.getTokenClaims();
if (!TextUtils.isEmpty((CharSequence) claims.get(AzureActiveDirectoryIdToken.TENANT_ID))) {
tenantId = (String) claims.get(AzureActiveDirectoryIdToken.TENANT_ID);
} else if (!TextUtils.isEmpty(clientInfo.getUtid())) {
Logger.warn(TAG, "realm is not returned from server. Use utid as realm.");
tenantId = clientInfo.getUtid();
} else {
Logger.warn(TAG, "realm and utid is not returned from server. " + "Using empty string as default tid.");
}
}
} catch (final ServiceException e) {
Logger.errorPII(TAG, "Failed to construct IDToken or ClientInfo", e);
}
return tenantId;
}
use of com.microsoft.identity.common.exception.ServiceException in project microsoft-authentication-library-common-for-android by AzureAD.
the class AdalMigrationAdapter method createAccount.
/**
* Creates a {@link MicrosoftAccount} from the supplied {@link ADALTokenCacheItem}.
*
* @param refreshToken The credential used to derive the new account.
* @return The newly created MicrosoftAccount.
*/
@Nullable
public static MicrosoftAccount createAccount(@NonNull final ADALTokenCacheItem refreshToken) {
final String methodName = ":createAccount";
try {
final String rawIdToken = refreshToken.getRawIdToken();
final String uid = refreshToken.getUserInfo().getUserId();
final String utid = refreshToken.getTenantId();
final String environment = new URL(refreshToken.getAuthority()).getHost();
final JsonObject clientInfo = new JsonObject();
clientInfo.addProperty("uid", uid);
clientInfo.addProperty("utid", utid);
final String clientInfoJson = clientInfo.toString();
final String base64EncodedClientInfo = new String(Base64.encode(clientInfoJson.getBytes(), 0));
final ClientInfo clientInfoObj = new ClientInfo(base64EncodedClientInfo);
final IDToken idToken = new IDToken(rawIdToken);
AzureActiveDirectoryAccount account = new AzureActiveDirectoryAccount(idToken, clientInfoObj);
account.setEnvironment(environment);
return account;
} catch (MalformedURLException | ServiceException e) {
final String errorMsg = "Failed to create Account";
Logger.error(TAG + methodName, errorMsg, null);
Logger.errorPII(TAG + methodName, errorMsg, e);
return null;
}
}
Aggregations