Search in sources :

Example 46 with AccountRecord

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

the class AuthenticationContextTest method testClearAdalCacheRemovesReplicaCache.

@Test
public void testClearAdalCacheRemovesReplicaCache() {
    // Init a Context
    final Context context = getInstrumentation().getContext();
    // Create an instance of the MSAL cache, populate it with some data
    final IAccountCredentialCache accountCredentialCache = new SharedPreferencesAccountCredentialCache(new CacheKeyValueDelegate(), new SharedPreferencesFileManager(context, DEFAULT_ACCOUNT_CREDENTIAL_SHARED_PREFERENCES, new StorageHelper(context)));
    // Create a dummy account
    final AccountRecord dummyAccount = new AccountRecord();
    dummyAccount.setHomeAccountId("23bfff3f-2664-495d-8196-79e28eaf400b");
    dummyAccount.setEnvironment("login.windows.net");
    dummyAccount.setRealm("0b96cce5-7aef-4ecb-bb27-2e50e3ed69de");
    dummyAccount.setName("Garth Fort");
    // Save it
    accountCredentialCache.saveAccount(dummyAccount);
    // Assert that the dummy account exists
    assertEquals(dummyAccount, accountCredentialCache.getAccounts().get(0));
    // Create an instance of the Authentication Context
    final AuthenticationContext authContext = new AuthenticationContext(getInstrumentation().getContext(), "https://github.com/MSOpenTech/some/some", false);
    // Clear the ADAL cache
    authContext.getCache().removeAll();
    // Assert that the replica is empty
    assertTrue(accountCredentialCache.getAccounts().isEmpty());
}
Also used : Context(android.content.Context) SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager) SharedPreferencesAccountCredentialCache(com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper) IAccountCredentialCache(com.microsoft.identity.common.internal.cache.IAccountCredentialCache) CacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 47 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 assertAccountLoaded.

private void assertAccountLoaded() {
    final AccountRecord account = mOauth2TokenCache.getAccount(ENVIRONMENT, CLIENT_ID, HOME_ACCOUNT_ID, REALM);
    assertNotNull(account);
    assertEquals(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, account.getAuthorityType());
    assertEquals(LOCAL_ACCOUNT_ID, account.getLocalAccountId());
    assertEquals(USERNAME, account.getUsername());
    assertEquals(HOME_ACCOUNT_ID, account.getHomeAccountId());
    assertEquals(ENVIRONMENT, account.getEnvironment());
    assertEquals(REALM, account.getRealm());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 48 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 testGetFamilyRefreshTokenForHomeAccountIdNoAccountWithHomeAccountId.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdNoAccountWithHomeAccountId() {
    // 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);
    accountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(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);
    accountCredentialCache.saveCredential(accessToken);
    // Save a Family RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setFamilyId("1");
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    accountCredentialCache.saveCredential(refreshToken);
    final IdTokenRecord id = new IdTokenRecord();
    id.setHomeAccountId(HOME_ACCOUNT_ID);
    id.setEnvironment(ENVIRONMENT);
    id.setRealm(REALM2);
    id.setCredentialType(IdToken.name());
    id.setClientId(CLIENT_ID);
    id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
    id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
    accountCredentialCache.saveCredential(id);
    final RefreshTokenRecord refreshTokenRecord = mOauth2TokenCache.getFamilyRefreshTokenForHomeAccountId(// different home account id
    "26685724-1f8e-4b97-a0ca-1863e33b9fb1");
    assertNull(refreshTokenRecord);
}
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) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 49 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 saveTokensWithAggregationMultiEntry.

@Test
public void saveTokensWithAggregationMultiEntry() throws ClientException {
    // Load additional creds into the cache to simulate a guest account...
    // at, id, account
    final AccessTokenRecord at = new AccessTokenRecord();
    at.setRealm(REALM2);
    at.setCachedAt(CACHED_AT);
    at.setExpiresOn(EXPIRES_ON);
    at.setSecret(SECRET);
    at.setHomeAccountId(HOME_ACCOUNT_ID);
    at.setEnvironment(ENVIRONMENT);
    at.setCredentialType(AccessToken.name());
    at.setClientId(CLIENT_ID);
    at.setTarget(TARGET);
    final IdTokenRecord id = new IdTokenRecord();
    id.setHomeAccountId(HOME_ACCOUNT_ID);
    id.setEnvironment(ENVIRONMENT);
    id.setRealm(REALM2);
    id.setCredentialType(IdToken.name());
    id.setClientId(CLIENT_ID);
    id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
    id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
    final AccountRecord acct = new AccountRecord();
    acct.setAuthorityType(AUTHORITY_TYPE);
    acct.setLocalAccountId(UUID.randomUUID().toString());
    acct.setUsername(USERNAME);
    acct.setHomeAccountId(HOME_ACCOUNT_ID);
    acct.setEnvironment(ENVIRONMENT);
    acct.setRealm(REALM2);
    accountCredentialCache.saveAccount(acct);
    accountCredentialCache.saveCredential(at);
    accountCredentialCache.saveCredential(id);
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV2);
    assertEquals(2, result.size());
    final ICacheRecord entry1 = result.get(0);
    assertNotNull(entry1.getAccount());
    assertNotNull(entry1.getIdToken());
    assertNotNull(entry1.getAccessToken());
    assertNotNull(entry1.getRefreshToken());
    final ICacheRecord entry2 = result.get(1);
    assertNotNull(entry2.getAccount());
    assertNotNull(entry2.getIdToken());
    assertNull(entry2.getAccessToken());
    assertNull(entry2.getRefreshToken());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 50 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 testGetFamilyRefreshTokenForHomeAccountIdValidCase.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdValidCase() {
    // 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);
    accountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(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);
    accountCredentialCache.saveCredential(accessToken);
    // Save a Family RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setFamilyId("1");
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    accountCredentialCache.saveCredential(refreshToken);
    final IdTokenRecord id = new IdTokenRecord();
    id.setHomeAccountId(HOME_ACCOUNT_ID);
    id.setEnvironment(ENVIRONMENT);
    id.setRealm(REALM2);
    id.setCredentialType(IdToken.name());
    id.setClientId(CLIENT_ID);
    id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
    id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
    accountCredentialCache.saveCredential(id);
    final RefreshTokenRecord refreshTokenRecord = mOauth2TokenCache.getFamilyRefreshTokenForHomeAccountId(HOME_ACCOUNT_ID);
    assertNotNull(refreshTokenRecord);
    assertEquals(refreshTokenRecord.getSecret(), SECRET);
}
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) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) 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