Search in sources :

Example 6 with AccessTokenRecord

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));
}
Also used : AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 7 with AccessTokenRecord

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));
}
Also used : AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 8 with AccessTokenRecord

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"));
}
Also used : JsonArray(com.google.gson.JsonArray) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JSONArray(org.json.JSONArray) JsonObject(com.google.gson.JsonObject) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 9 with AccessTokenRecord

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());
}
Also used : AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 10 with AccessTokenRecord

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;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord)

Aggregations

AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)60 Test (org.junit.Test)52 Credential (com.microsoft.identity.common.internal.dto.Credential)29 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)29 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)25 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)11 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)9 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 Bundle (android.os.Bundle)1 NonNull (androidx.annotation.NonNull)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1