use of com.microsoft.identity.common.internal.dto.AccessTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class CacheKeyValueDelegateTest method accessTokenCreateCacheKeyComplete.
// AccessTokens
@Test
public void accessTokenCreateCacheKeyComplete() {
final AccessTokenRecord accessToken = new AccessTokenRecord();
accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
accessToken.setEnvironment(ENVIRONMENT);
accessToken.setCredentialType(CredentialType.AccessToken.name());
accessToken.setClientId(CLIENT_ID);
accessToken.setRealm(REALM);
accessToken.setTarget(TARGET);
final String expectedKey = // just for formatting
"" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_ACCESS_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + REALM + CACHE_VALUE_SEPARATOR + TARGET;
assertEquals(expectedKey, mDelegate.generateCacheKey(accessToken));
}
use of com.microsoft.identity.common.internal.dto.AccessTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class CacheKeyValueDelegateTest method accessTokenCreateCacheKeyCompleteWithEscapeSequences.
@Test
public void accessTokenCreateCacheKeyCompleteWithEscapeSequences() {
final AccessTokenRecord accessToken = new AccessTokenRecord();
accessToken.setHomeAccountId(wrapInEscapeSequenceChars(HOME_ACCOUNT_ID));
accessToken.setEnvironment(wrapInEscapeSequenceChars(ENVIRONMENT));
accessToken.setCredentialType(wrapInEscapeSequenceChars(CredentialType.AccessToken.name()));
accessToken.setClientId(wrapInEscapeSequenceChars(CLIENT_ID));
accessToken.setRealm(wrapInEscapeSequenceChars(REALM));
accessToken.setTarget(wrapInEscapeSequenceChars(TARGET));
final String expectedKey = // just for formatting
"" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_ACCESS_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + REALM + CACHE_VALUE_SEPARATOR + TARGET;
assertEquals(expectedKey, mDelegate.generateCacheKey(accessToken));
}
use of com.microsoft.identity.common.internal.dto.AccessTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class CacheKeyValueDelegateTest method accessTokenExtraValueSerialization.
@Test
public void accessTokenExtraValueSerialization() throws JSONException {
final AccessTokenRecord accessToken = new AccessTokenRecord();
accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
accessToken.setEnvironment(ENVIRONMENT);
accessToken.setCredentialType(CredentialType.AccessToken.name().toLowerCase(Locale.US));
accessToken.setClientId(CLIENT_ID);
accessToken.setRealm(REALM);
accessToken.setTarget(TARGET);
final Map<String, JsonElement> additionalFields = new HashMap<>();
// Add some random Json to this object
JsonElement jsonStr = new JsonPrimitive("bar");
JsonArray jsonNumberArr = new JsonArray();
jsonNumberArr.add(1);
jsonNumberArr.add(2);
jsonNumberArr.add(3);
JsonObject jsonObject = new JsonObject();
jsonObject.add("object_key", new JsonPrimitive("object_value"));
additionalFields.put("foo", jsonStr);
additionalFields.put("numbers", jsonNumberArr);
additionalFields.put("object", jsonObject);
accessToken.setAdditionalFields(additionalFields);
String serializedValue = mDelegate.generateCacheValue(accessToken);
JSONObject derivedCacheValueJsonObject = new JSONObject(serializedValue);
assertEquals(HOME_ACCOUNT_ID, derivedCacheValueJsonObject.getString(Credential.SerializedNames.HOME_ACCOUNT_ID));
assertEquals(ENVIRONMENT, derivedCacheValueJsonObject.getString(Credential.SerializedNames.ENVIRONMENT));
assertEquals(CredentialType.AccessToken.name().toLowerCase(Locale.US), derivedCacheValueJsonObject.getString("credential_type"));
assertEquals(CLIENT_ID, derivedCacheValueJsonObject.getString(Credential.SerializedNames.CLIENT_ID));
assertEquals(REALM, derivedCacheValueJsonObject.getString(AccessTokenRecord.SerializedNames.REALM));
assertEquals(TARGET, derivedCacheValueJsonObject.getString(AccessTokenRecord.SerializedNames.TARGET));
assertEquals("bar", derivedCacheValueJsonObject.getString("foo"));
final JSONArray jsonArr = derivedCacheValueJsonObject.getJSONArray("numbers");
assertEquals(3, jsonArr.length());
final JSONObject jsonObj = derivedCacheValueJsonObject.getJSONObject("object");
assertEquals("object_value", jsonObj.getString("object_key"));
}
use of com.microsoft.identity.common.internal.dto.AccessTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MicrosoftStsAccountCredentialAdapterTest method createAccessToken.
@Test
public void createAccessToken() {
final AccessTokenRecord accessToken = mAccountCredentialAdapter.createAccessToken(mockStrategy, mockRequest, mockResponse);
assertNotNull(accessToken);
assertEquals(MOCK_SCOPE, accessToken.getTarget());
assertNotNull(accessToken.getCachedAt());
assertNotNull(accessToken.getExpiresOn());
assertNotNull(accessToken.getExpiresOn());
assertEquals(MOCK_TID, accessToken.getRealm());
assertEquals(MOCK_AUTHORITY, accessToken.getAuthority());
assertEquals(MOCK_ENVIRONMENT, accessToken.getEnvironment());
assertNotNull(accessToken.getExtendedExpiresOn());
assertEquals(MOCK_UID + "." + MOCK_UTID, accessToken.getHomeAccountId());
}
use of com.microsoft.identity.common.internal.dto.AccessTokenRecord 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;
}
Aggregations