Search in sources :

Example 1 with PrimaryRefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class CacheKeyValueDelegateTest method primaryRefreshTokenCacheKeyComplete.

// End RefreshTokens
// PrimaryRefreshTokens
@Test
public void primaryRefreshTokenCacheKeyComplete() {
    final PrimaryRefreshTokenRecord primaryRefreshTokenRecord = new PrimaryRefreshTokenRecord();
    primaryRefreshTokenRecord.setHomeAccountId(HOME_ACCOUNT_ID);
    primaryRefreshTokenRecord.setEnvironment(ENVIRONMENT);
    primaryRefreshTokenRecord.setCredentialType(CredentialType.PrimaryRefreshToken.name());
    // Client_id, target and realm not in cache key.
    primaryRefreshTokenRecord.setClientId(CLIENT_ID);
    final String expectedKey = // just for formatting
    "" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_PRIMARY_REFRESH_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + CACHE_VALUE_SEPARATOR;
    assertEquals(expectedKey, mDelegate.generateCacheKey(primaryRefreshTokenRecord));
}
Also used : PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Example 2 with PrimaryRefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord 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)

Example 3 with PrimaryRefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getCredentialsPRTClientId.

@Test
public void getCredentialsPRTClientId() {
    final PrimaryRefreshTokenRecord primaryRefreshToken = new PrimaryRefreshTokenRecord();
    primaryRefreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    primaryRefreshToken.setEnvironment(ENVIRONMENT);
    primaryRefreshToken.setCredentialType(CredentialType.PrimaryRefreshToken.name().toLowerCase(Locale.US));
    primaryRefreshToken.setClientId(CLIENT_ID);
    primaryRefreshToken.setSessionKey(SESSION_KEY);
    mSharedPreferencesAccountCredentialCache.saveCredential(primaryRefreshToken);
    List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, CredentialType.PrimaryRefreshToken, CLIENT_ID, null, null, null);
    assertEquals(1, credentials.size());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Example 4 with PrimaryRefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getCredentialsPRTAnotherClientId.

@Test
public void getCredentialsPRTAnotherClientId() {
    final PrimaryRefreshTokenRecord primaryRefreshToken = new PrimaryRefreshTokenRecord();
    primaryRefreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    primaryRefreshToken.setEnvironment(ENVIRONMENT);
    primaryRefreshToken.setCredentialType(CredentialType.PrimaryRefreshToken.name().toLowerCase(Locale.US));
    primaryRefreshToken.setClientId(CLIENT_ID);
    primaryRefreshToken.setSessionKey(SESSION_KEY);
    mSharedPreferencesAccountCredentialCache.saveCredential(primaryRefreshToken);
    List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, CredentialType.PrimaryRefreshToken, "another-client-id", null, null, null);
    assertTrue(credentials.isEmpty());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Example 5 with PrimaryRefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getCredentialsPRTNoClientId.

@Test
public void getCredentialsPRTNoClientId() {
    final PrimaryRefreshTokenRecord primaryRefreshToken = new PrimaryRefreshTokenRecord();
    primaryRefreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    primaryRefreshToken.setEnvironment(ENVIRONMENT);
    primaryRefreshToken.setCredentialType(CredentialType.PrimaryRefreshToken.name().toLowerCase(Locale.US));
    primaryRefreshToken.setClientId(CLIENT_ID);
    primaryRefreshToken.setSessionKey(SESSION_KEY);
    mSharedPreferencesAccountCredentialCache.saveCredential(primaryRefreshToken);
    List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, CredentialType.PrimaryRefreshToken, null, /* client id */
    null, null, null);
    assertEquals(1, credentials.size());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Aggregations

PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)6 Test (org.junit.Test)5 Credential (com.microsoft.identity.common.internal.dto.Credential)3 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)1 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)1 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)1 JSONObject (org.json.JSONObject)1