Search in sources :

Example 1 with IdTokenRecord

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

Example 2 with IdTokenRecord

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;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)

Example 3 with IdTokenRecord

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;
}
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 4 with IdTokenRecord

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

Example 5 with IdTokenRecord

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;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Nullable(androidx.annotation.Nullable)

Aggregations

IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)31 Test (org.junit.Test)17 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)9 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)9 Credential (com.microsoft.identity.common.internal.dto.Credential)7 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)4 ArrayList (java.util.ArrayList)4 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)2 JSONArray (org.json.JSONArray)2 Nullable (androidx.annotation.Nullable)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1