Search in sources :

Example 71 with AccountRecord

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

the class SharedPreferencesAccountCredentialCacheTest method saveAccountNoHomeAccountIdNoRealm.

@Test
public void saveAccountNoHomeAccountIdNoRealm() {
    final AccountRecord account = new AccountRecord();
    account.setEnvironment(ENVIRONMENT);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    // Save the Account
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Synthesize a cache key for it
    final String accountCacheKey = mDelegate.generateCacheKey(account);
    // Resurrect the Account
    final AccountRecord restoredAccount = mSharedPreferencesAccountCredentialCache.getAccount(accountCacheKey);
    assertTrue(account.equals(restoredAccount));
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 72 with AccountRecord

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

the class SharedPreferencesAccountCredentialCacheTest method getCredentials.

@Test
public void getCredentials() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // 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);
    // Verify getCredentials() returns two matching elements
    final List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentials();
    assertTrue(credentials.size() == 2);
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) 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 73 with AccountRecord

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

the class SharedPreferencesAccountCredentialCacheTest method testAccountMerge.

@Test
public void testAccountMerge() {
    final AccountRecord accountFirst = new AccountRecord();
    accountFirst.setHomeAccountId(HOME_ACCOUNT_ID);
    accountFirst.setEnvironment(ENVIRONMENT);
    accountFirst.setRealm(REALM);
    accountFirst.setLocalAccountId(LOCAL_ACCOUNT_ID);
    accountFirst.setUsername(USERNAME);
    accountFirst.setAuthorityType(AUTHORITY_TYPE);
    accountFirst.setMiddleName(MIDDLE_NAME);
    accountFirst.setName(NAME);
    // Create and set some additional field data...
    final String additionalKey = "extra-prop-1";
    final String additionalValue = "extra-value-1";
    final JsonElement additionalValueElement = new JsonPrimitive(additionalValue);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    additionalFields.put(additionalKey, additionalValueElement);
    accountFirst.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveAccount(accountFirst);
    // Save the second Account, with fields to merge...
    final AccountRecord accountSecond = new AccountRecord();
    accountSecond.setHomeAccountId(HOME_ACCOUNT_ID);
    accountSecond.setEnvironment(ENVIRONMENT);
    accountSecond.setRealm(REALM);
    accountSecond.setLocalAccountId(LOCAL_ACCOUNT_ID);
    accountSecond.setUsername(USERNAME);
    accountSecond.setAuthorityType(AUTHORITY_TYPE);
    accountSecond.setMiddleName(MIDDLE_NAME);
    accountSecond.setName(NAME);
    // Create and set some additional field data...
    final String additionalKey2 = "extra-prop-2";
    final String additionalValue2 = "extra-value-2";
    final JsonElement additionalValueElement2 = new JsonPrimitive(additionalValue2);
    final Map<String, JsonElement> additionalFields2 = new HashMap<>();
    additionalFields2.put(additionalKey2, additionalValueElement2);
    accountSecond.setAdditionalFields(additionalFields2);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveAccount(accountSecond);
    // Synthesize a cache key for it - either is fine.
    final String credentialCacheKey = mDelegate.generateCacheKey(accountFirst);
    // Resurrect the Credential
    final AccountRecord restoredAccount = mSharedPreferencesAccountCredentialCache.getAccount(credentialCacheKey);
    assertTrue(accountFirst.equals(restoredAccount));
    // Assert the presence of both additionalFields
    assertEquals(additionalValue, restoredAccount.getAdditionalFields().get(additionalKey).getAsString());
    assertEquals(additionalValue2, restoredAccount.getAdditionalFields().get(additionalKey2).getAsString());
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 74 with AccountRecord

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

the class SharedPreferencesAccountCredentialCacheTest method clearAll.

@Test
public void clearAll() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // 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);
    // Call clearAll()
    mSharedPreferencesAccountCredentialCache.clearAll();
    // Verify getAccounts() returns zero items
    assertTrue(mSharedPreferencesAccountCredentialCache.getAccounts().isEmpty());
    // Verify getCredentials() returns zero items
    assertTrue(mSharedPreferencesAccountCredentialCache.getCredentials().isEmpty());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) 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 75 with AccountRecord

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

the class SharedPreferencesAccountCredentialCacheTest method saveAccountNoRealm.

@Test
public void saveAccountNoRealm() {
    final AccountRecord account = new AccountRecord();
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    // Save the Account
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Synthesize a cache key for it
    final String accountCacheKey = mDelegate.generateCacheKey(account);
    // Resurrect the Account
    final AccountRecord restoredAccount = mSharedPreferencesAccountCredentialCache.getAccount(accountCacheKey);
    assertTrue(account.equals(restoredAccount));
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Aggregations

AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)92 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)20 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)11 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)11 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)11 Credential (com.microsoft.identity.common.internal.dto.Credential)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)10 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)10 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)9 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)7 Nullable (androidx.annotation.Nullable)6 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)5 HashMap (java.util.HashMap)5 IAccountRecord (com.microsoft.identity.common.internal.dto.IAccountRecord)4 Context (android.content.Context)3 NonNull (androidx.annotation.NonNull)3 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3