use of com.microsoft.identity.common.internal.dto.IdTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class CacheKeyValueDelegateTest method idTokenExtraValueDeserialization.
@Test
public void idTokenExtraValueDeserialization() throws JSONException {
final IdTokenRecord idToken = new IdTokenRecord();
idToken.setHomeAccountId(HOME_ACCOUNT_ID);
idToken.setEnvironment(ENVIRONMENT);
idToken.setCredentialType(CredentialType.IdToken.name().toLowerCase(Locale.US));
idToken.setClientId(CLIENT_ID);
idToken.setRealm(REALM);
String serializedValue = mDelegate.generateCacheValue(idToken);
// Turn the serialized value into a JSONObject and start testing field equality.
final JSONObject jsonObject = new JSONObject(serializedValue);
// Add more non-standard data to this object...
final JSONArray numbers = new JSONArray("[1, 2, 3]");
final JSONArray objects = new JSONArray("[{\"hello\" : \"hallo\"}, {\"goodbye\" : \"auf wiedersehen\"}]");
jsonObject.put("foo", "bar");
jsonObject.put("numbers", numbers);
jsonObject.put("objects", objects);
serializedValue = jsonObject.toString();
final IdTokenRecord deserializedValue = mDelegate.fromCacheValue(serializedValue, IdTokenRecord.class);
assertNotNull(deserializedValue);
assertNull(deserializedValue.getAdditionalFields().get(Credential.SerializedNames.ENVIRONMENT));
assertEquals(HOME_ACCOUNT_ID, deserializedValue.getHomeAccountId());
assertEquals(ENVIRONMENT, deserializedValue.getEnvironment());
assertEquals(CredentialType.IdToken.name().toLowerCase(Locale.US), deserializedValue.getCredentialType());
assertEquals(CLIENT_ID, deserializedValue.getClientId());
assertEquals(REALM, deserializedValue.getRealm());
assertEquals(3, deserializedValue.getAdditionalFields().size());
assertEquals("bar", deserializedValue.getAdditionalFields().get("foo").getAsString());
assertEquals(numbers.toString(), deserializedValue.getAdditionalFields().get("numbers").toString());
}
use of com.microsoft.identity.common.internal.dto.IdTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class BrokerOAuth2TokenCache method getIdTokensForAccountRecord.
@Override
public List<IdTokenRecord> getIdTokensForAccountRecord(@NonNull final String clientId, @NonNull final AccountRecord accountRecord) {
final List<IdTokenRecord> result;
final String accountEnv = accountRecord.getEnvironment();
if (null == clientId) {
// this feature...
throw new UnsupportedOperationException("Aggregating IdTokens across ClientIds is not supported - do you have a feature request?");
} else {
final OAuth2TokenCache cache = getTokenCacheForClient(clientId, accountEnv, mCallingProcessUid);
// Suppressing unchecked warning as the generic type was not provided for cache
@SuppressWarnings(WarningType.unchecked_warning) List<IdTokenRecord> cacheIdTokensForAccountRecord = cache.getIdTokensForAccountRecord(clientId, accountRecord);
result = cacheIdTokensForAccountRecord;
}
return result;
}
use of com.microsoft.identity.common.internal.dto.IdTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class CacheKeyValueDelegate method generateCacheKey.
@SuppressWarnings("checkstyle:innerassignment")
@Override
public String generateCacheKey(Credential credential) {
String cacheKey = HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + REALM + CACHE_VALUE_SEPARATOR + TARGET;
cacheKey = cacheKey.replace(HOME_ACCOUNT_ID, sanitizeNull(credential.getHomeAccountId()));
cacheKey = cacheKey.replace(ENVIRONMENT, sanitizeNull(credential.getEnvironment()));
cacheKey = cacheKey.replace(CREDENTIAL_TYPE, sanitizeNull(credential.getCredentialType()));
RefreshTokenRecord rt;
if ((credential instanceof RefreshTokenRecord) && !StringExtensions.isNullOrBlank((rt = (RefreshTokenRecord) credential).getFamilyId())) {
String familyIdForCacheKey = rt.getFamilyId();
if (familyIdForCacheKey.startsWith(FOCI_PREFIX)) {
familyIdForCacheKey = familyIdForCacheKey.replace(FOCI_PREFIX, "");
}
cacheKey = cacheKey.replace(CLIENT_ID, familyIdForCacheKey);
} else {
cacheKey = cacheKey.replace(CLIENT_ID, sanitizeNull(credential.getClientId()));
}
if (credential instanceof AccessTokenRecord) {
final AccessTokenRecord accessToken = (AccessTokenRecord) credential;
cacheKey = cacheKey.replace(REALM, sanitizeNull(accessToken.getRealm()));
cacheKey = cacheKey.replace(TARGET, sanitizeNull(accessToken.getTarget()));
if (TokenRequest.TokenType.POP.equalsIgnoreCase(accessToken.getAccessTokenType())) {
cacheKey += CACHE_VALUE_SEPARATOR + AUTH_SCHEME;
cacheKey = cacheKey.replace(AUTH_SCHEME, sanitizeNull(accessToken.getAccessTokenType()));
}
if (!StringExtensions.isNullOrBlank(accessToken.getRequestedClaims())) {
// The Requested Claims string has no guarantee it doesn't contain a delimiter, so we hash it
cacheKey += CACHE_VALUE_SEPARATOR + REQUESTED_CLAIMS;
String reqClaimsHash = String.valueOf(sanitizeNull(accessToken.getRequestedClaims()).hashCode());
cacheKey = cacheKey.replace(REQUESTED_CLAIMS, sanitizeNull(reqClaimsHash));
}
} else if (credential instanceof RefreshTokenRecord) {
final RefreshTokenRecord refreshToken = (RefreshTokenRecord) credential;
cacheKey = cacheKey.replace(REALM, "");
cacheKey = cacheKey.replace(TARGET, sanitizeNull(refreshToken.getTarget()));
} else if (credential instanceof IdTokenRecord) {
final IdTokenRecord idToken = (IdTokenRecord) credential;
cacheKey = cacheKey.replace(REALM, sanitizeNull(idToken.getRealm()));
cacheKey = cacheKey.replace(TARGET, "");
} else if (credential instanceof PrimaryRefreshTokenRecord) {
cacheKey = cacheKey.replace(REALM, "");
cacheKey = cacheKey.replace(TARGET, "");
}
return cacheKey;
}
use of com.microsoft.identity.common.internal.dto.IdTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCache method setSingleSignOnState.
@Override
public void setSingleSignOnState(final GenericAccount account, final GenericRefreshToken refreshToken) throws ClientException {
Logger.info(TAG + ":setSingleSignOnState", "Set SSO state called.");
final AccountRecord accountDto = mAccountCredentialAdapter.asAccount(account);
final RefreshTokenRecord rt = mAccountCredentialAdapter.asRefreshToken(refreshToken);
final IdTokenRecord idToken = mAccountCredentialAdapter.asIdToken(account, refreshToken);
validateCacheArtifacts(accountDto, null, rt, idToken);
saveAccounts(accountDto);
saveCredentialsInternal(idToken, rt);
removeAllRefreshTokensExcept(accountDto, rt);
}
use of com.microsoft.identity.common.internal.dto.IdTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCache method getAccountWithAggregatedAccountDataByLocalAccountId.
@Override
@Nullable
public ICacheRecord getAccountWithAggregatedAccountDataByLocalAccountId(@Nullable String environment, @NonNull String clientId, @NonNull String localAccountId) {
CacheRecord.CacheRecordBuilder result = null;
final AccountRecord acct = getAccountByLocalAccountId(environment, clientId, localAccountId);
if (null != acct) {
final List<IdTokenRecord> acctIdTokens = getIdTokensForAccountRecord(clientId, acct);
result = CacheRecord.builder();
result.account(acct);
for (final IdTokenRecord idTokenRecord : acctIdTokens) {
setToCacheRecord(result, idTokenRecord);
}
return result.build();
}
return null;
}
Aggregations