Search in sources :

Example 31 with Credential

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

the class SharedPreferencesAccountCredentialCacheTest method getCredentialsNoCredentialType.

@Test
public void getCredentialsNoCredentialType() {
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm("Foo");
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    // Save a RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    final List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, null, CLIENT_ID, null, TARGET, BEARER_AUTHENTICATION_SCHEME.getName());
    assertEquals(2, credentials.size());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 32 with Credential

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

the class SharedPreferencesAccountCredentialCacheTest method getCorrectCredentialWhenRequestedClaimsAreSpecified.

@Test
public void getCorrectCredentialWhenRequestedClaimsAreSpecified() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setSecret(SECRET);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setTarget(TARGET);
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret("SecretA");
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm(REALM);
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setRequestedClaims("{\"access_token\":{\"deviceid\":{\"essential\":false}}}");
    final AccessTokenRecord accessToken2 = new AccessTokenRecord();
    accessToken2.setCachedAt(CACHED_AT);
    accessToken2.setExpiresOn(EXPIRES_ON);
    accessToken2.setSecret("SecretB");
    accessToken2.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken2.setRealm(REALM);
    accessToken2.setEnvironment(ENVIRONMENT);
    accessToken2.setCredentialType(CredentialType.AccessToken.name());
    accessToken2.setClientId(CLIENT_ID);
    accessToken2.setTarget(TARGET);
    accessToken2.setRequestedClaims("{\"access_token\":{\"deviceid\":{\"essential\":true}}}");
    // Save the Credentials
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken2);
    List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, CredentialType.AccessToken, CLIENT_ID, REALM, null, BEARER_AUTHENTICATION_SCHEME.getName(), "{\"access_token\":{\"deviceid\":{\"essential\":true}}}");
    assertEquals(1, credentials.size());
    assertEquals("SecretB", credentials.get(0).getSecret());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 33 with Credential

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

the class MsalOAuth2TokenCacheTest method saveTokensWithIntersect.

@Test
public void saveTokensWithIntersect() throws Exception {
    // Manually insert an AT with a ltd scope into the cache
    final String extendedScopes = "calendar.modify user.read user.write https://graph.windows.net";
    AccessTokenRecord accessTokenToClear = new AccessTokenRecord();
    accessTokenToClear.setRealm(REALM);
    accessTokenToClear.setCachedAt(CACHED_AT);
    accessTokenToClear.setExpiresOn(EXPIRES_ON);
    accessTokenToClear.setSecret(SECRET);
    accessTokenToClear.setHomeAccountId(HOME_ACCOUNT_ID);
    accessTokenToClear.setEnvironment(ENVIRONMENT);
    accessTokenToClear.setCredentialType(AccessToken.name());
    accessTokenToClear.setClientId(CLIENT_ID);
    accessTokenToClear.setTarget(TARGET);
    // Save this dummy AT
    accountCredentialCache.saveCredential(accessTokenToClear);
    // Set the wider target on the new AT to write...
    defaultTestBundleV2.mGeneratedAccessToken.setTarget(extendedScopes);
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<AccountRecord> accounts = accountCredentialCache.getAccounts();
    assertEquals(1, accounts.size());
    assertEquals(defaultTestBundleV2.mGeneratedAccount, accounts.get(0));
    final List<Credential> credentials = accountCredentialCache.getCredentials();
    assertEquals(3, credentials.size());
    final List<Credential> rts = new ArrayList<>();
    final List<Credential> ats = new ArrayList<>();
    final List<Credential> ids = new ArrayList<>();
    sortResultToLists(credentials, rts, ats, ids);
    assertEquals(defaultTestBundleV2.mGeneratedAccessToken, ats.get(0));
    assertEquals(defaultTestBundleV2.mGeneratedRefreshToken, rts.get(0));
    assertEquals(defaultTestBundleV2.mGeneratedIdToken, ids.get(0));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 34 with Credential

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

the class MsalOAuth2TokenCacheTest method saveTokensV1Compat.

@Test
public void saveTokensV1Compat() throws ClientException {
    // This test asserts that if an IdToken is returned in the v1 format (broker cases),
    // it is saved property.
    loadTestBundleIntoCache(defaultTestBundleV1);
    final List<AccountRecord> accounts = accountCredentialCache.getAccounts();
    assertEquals(1, accounts.size());
    assertEquals(defaultTestBundleV1.mGeneratedAccount, accounts.get(0));
    final List<Credential> credentials = accountCredentialCache.getCredentials();
    assertEquals(3, credentials.size());
    final List<Credential> rts = new ArrayList<>();
    final List<Credential> ats = new ArrayList<>();
    final List<Credential> ids = new ArrayList<>();
    for (final Credential credential : credentials) {
        switch(CredentialType.fromString(credential.getCredentialType())) {
            case AccessToken:
                ats.add(credential);
                break;
            case RefreshToken:
                rts.add(credential);
                break;
            case V1IdToken:
                ids.add(credential);
                break;
            default:
                fail("Unexpected value: " + credential.getCredentialType());
        }
    }
    assertEquals(defaultTestBundleV1.mGeneratedAccessToken, ats.get(0));
    assertEquals(defaultTestBundleV1.mGeneratedRefreshToken, rts.get(0));
    assertEquals(defaultTestBundleV1.mGeneratedIdToken, ids.get(0));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 35 with Credential

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

the class BrokerOAuth2TokenCacheTest method testCanSaveIntoFociCache.

@Test
public void testCanSaveIntoFociCache() throws ClientException {
    configureMocksForFoci();
    mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<AccountRecord> accounts = mFociCredentialCache.getAccounts();
    assertEquals(1, accounts.size());
    assertEquals(mDefaultFociTestBundle.mGeneratedAccount, accounts.get(0));
    final List<Credential> credentials = mFociCredentialCache.getCredentials();
    assertEquals(3, credentials.size());
    final List<Credential> rts = new ArrayList<>();
    final List<Credential> ats = new ArrayList<>();
    final List<Credential> ids = new ArrayList<>();
    for (final Credential credential : credentials) {
        if (credential.getCredentialType().equalsIgnoreCase(CredentialType.AccessToken.name())) {
            ats.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.RefreshToken.name())) {
            rts.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.IdToken.name())) {
            ids.add(credential);
        } else {
            fail();
        }
    }
    assertEquals(mDefaultFociTestBundle.mGeneratedAccessToken, ats.get(0));
    assertEquals(mDefaultFociTestBundle.mGeneratedRefreshToken, rts.get(0));
    assertEquals(mDefaultFociTestBundle.mGeneratedIdToken, ids.get(0));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Credential (com.microsoft.identity.common.internal.dto.Credential)64 Test (org.junit.Test)45 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)33 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)31 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)30 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 JsonElement (com.google.gson.JsonElement)7 JsonPrimitive (com.google.gson.JsonPrimitive)7 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)7 CredentialType (com.microsoft.identity.common.internal.dto.CredentialType)3 Map (java.util.Map)3 Nullable (androidx.annotation.Nullable)2 HashSet (java.util.HashSet)2 NonNull (androidx.annotation.NonNull)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)1 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)1 CacheStartEvent (com.microsoft.identity.common.internal.telemetry.events.CacheStartEvent)1