Search in sources :

Example 36 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 getAccountsNoHomeAccountIdNoRealm.

@Test
public void getAccountsNoHomeAccountIdNoRealm() {
    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);
    // Save the Account
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Test retrieval
    final List<AccountRecord> accounts = mSharedPreferencesAccountCredentialCache.getAccountsFilteredBy(null, ENVIRONMENT, null);
    assertEquals(1, accounts.size());
    final AccountRecord retrievedAccount = accounts.get(0);
    assertEquals(HOME_ACCOUNT_ID, retrievedAccount.getHomeAccountId());
    assertEquals(ENVIRONMENT, retrievedAccount.getEnvironment());
    assertEquals(REALM, retrievedAccount.getRealm());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 37 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 getAccountsWithMatchingEnvironmentRealm.

@Test
public void getAccountsWithMatchingEnvironmentRealm() {
    final AccountRecord account1 = new AccountRecord();
    account1.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account1.setUsername(USERNAME);
    account1.setAuthorityType(AUTHORITY_TYPE);
    account1.setHomeAccountId("Foo");
    account1.setEnvironment(ENVIRONMENT);
    account1.setRealm(REALM);
    final AccountRecord account2 = new AccountRecord();
    account2.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account2.setUsername(USERNAME);
    account2.setAuthorityType(AUTHORITY_TYPE);
    account2.setHomeAccountId("Bar");
    account2.setEnvironment(ENVIRONMENT);
    account2.setRealm(REALM);
    final AccountRecord account3 = new AccountRecord();
    account3.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account3.setUsername(USERNAME);
    account3.setAuthorityType(AUTHORITY_TYPE);
    account3.setHomeAccountId("Baz");
    account3.setEnvironment(ENVIRONMENT);
    account3.setRealm(REALM);
    final AccountRecord account4 = new AccountRecord();
    account4.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account4.setUsername(USERNAME);
    account4.setAuthorityType(AUTHORITY_TYPE);
    account4.setHomeAccountId("qux");
    account4.setEnvironment(ENVIRONMENT);
    account4.setRealm("quz");
    // Save the Accounts
    mSharedPreferencesAccountCredentialCache.saveAccount(account1);
    mSharedPreferencesAccountCredentialCache.saveAccount(account2);
    mSharedPreferencesAccountCredentialCache.saveAccount(account3);
    mSharedPreferencesAccountCredentialCache.saveAccount(account4);
    final List<AccountRecord> accounts = mSharedPreferencesAccountCredentialCache.getAccountsFilteredBy(null, ENVIRONMENT, REALM);
    assertEquals(3, accounts.size());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 38 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 persistAndRestoreExtraClaimsAccount.

@Test
public void persistAndRestoreExtraClaimsAccount() {
    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);
    account.setMiddleName(MIDDLE_NAME);
    account.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 String secondAdditionalKey = "extra-prop-2";
    final String secondAdditionalValue = "extra-value-2";
    final JsonElement secondAdditionalValueElement = new JsonPrimitive(secondAdditionalValue);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    additionalFields.put(additionalKey, additionalValueElement);
    additionalFields.put(secondAdditionalKey, secondAdditionalValueElement);
    account.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(account);
    // Resurrect the Credential
    final AccountRecord restoredAccount = mSharedPreferencesAccountCredentialCache.getAccount(credentialCacheKey);
    assertTrue(account.equals(restoredAccount));
    assertEquals(additionalValue, restoredAccount.getAdditionalFields().get(additionalKey).getAsString());
    assertEquals(secondAdditionalValue, restoredAccount.getAdditionalFields().get(secondAdditionalKey).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 39 with AccountRecord

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

the class MsalOAuth2TokenCacheTest method getAccountByLocalAccountId.

@Test
public void getAccountByLocalAccountId() throws ClientException {
    // Save an Account into the cache
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    // Find it by the local_account_id
    final AccountRecord account = mOauth2TokenCache.getAccountByLocalAccountId(ENVIRONMENT, CLIENT_ID, LOCAL_ACCOUNT_ID);
    assertNotNull(account);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 40 with AccountRecord

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

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