Search in sources :

Example 6 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testGetAccountWithLocalAccountIdAppUidCache.

@Test
public void testGetAccountWithLocalAccountIdAppUidCache() throws ClientException {
    configureMocksForAppUid();
    mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final AccountRecord account = mBrokerOAuth2TokenCache.getAccountByLocalAccountId(ENVIRONMENT, CLIENT_ID, LOCAL_ACCOUNT_ID);
    assertNotNull(account);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 7 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testGetAccountWithLocalAccountIdFociCache.

@Test
public void testGetAccountWithLocalAccountIdFociCache() throws ClientException {
    configureMocksForFoci();
    mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final AccountRecord account = mBrokerOAuth2TokenCache.getAccountByLocalAccountId(ENVIRONMENT, CLIENT_ID, LOCAL_ACCOUNT_ID);
    assertNotNull(account);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 8 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testRemoveAccountFromDevice.

@Test
public void testRemoveAccountFromDevice() throws ClientException {
    // Load up the 'other caches' which a bunch of test credentials, see if we can get them out...
    int ii = 0;
    for (final OAuth2TokenCache cache : mOtherAppTokenCaches) {
        configureMocks(mOtherCacheTestBundles.get(ii));
        final ICacheRecord cacheRecord = cache.save(mockStrategy, mockRequest, mockResponse);
        final BrokerApplicationMetadata applicationMetadata = new BrokerApplicationMetadata();
        applicationMetadata.setClientId(cacheRecord.getIdToken().getClientId());
        applicationMetadata.setEnvironment(cacheRecord.getIdToken().getEnvironment());
        applicationMetadata.setFoci(cacheRecord.getRefreshToken().getFamilyId());
        applicationMetadata.setUid(testAppUids[ii++]);
        mApplicationMetadataCache.insert(applicationMetadata);
    }
    final List<String> clientIds = new ArrayList<>();
    for (final MsalOAuth2TokenCacheTest.AccountCredentialTestBundle testBundle : mOtherCacheTestBundles) {
        clientIds.add(testBundle.mGeneratedRefreshToken.getClientId());
    }
    final List<AccountRecord> xAppAccounts = mBrokerOAuth2TokenCache.getAccounts();
    // Deleting one of these AccountRecords should remove all of them...
    final AccountDeletionRecord deletionRecord = mBrokerOAuth2TokenCache.removeAccountFromDevice(xAppAccounts.get(0));
    assertEquals(xAppAccounts.size(), deletionRecord.size());
    assertEquals(0, mBrokerOAuth2TokenCache.getAccounts().size());
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) MicrosoftFamilyOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache) BrokerOAuth2TokenCache(com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 9 with AccountRecord

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

the class MicrosoftFamilyOAuth2TokenCacheTest method testOnlyOneFrtMayExistAcrossClientsForAccount.

@Test
public void testOnlyOneFrtMayExistAcrossClientsForAccount() throws ClientException {
    // Save an FRT
    final String randomHomeAccountId = UUID.randomUUID().toString();
    final String localAccountId = UUID.randomUUID().toString();
    final String realm = UUID.randomUUID().toString();
    final AccountCredentialTestBundle frtTestBundle = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, localAccountId, "test.user@tenant.onmicrosoft.com", randomHomeAccountId, ENVIRONMENT, realm, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID, SECRET, MicrosoftStsAccountCredentialAdapterTest.MOCK_ID_TOKEN_WITH_CLAIMS, "1", SESSION_KEY, CredentialType.IdToken);
    when(mockCredentialAdapter.createAccount(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle.mGeneratedAccount);
    when(mockCredentialAdapter.createAccessToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle.mGeneratedAccessToken);
    when(mockCredentialAdapter.createRefreshToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle.mGeneratedRefreshToken);
    when(mockCredentialAdapter.createIdToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle.mGeneratedIdToken);
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    // Save another FRT, this time with a different client id
    final AccountCredentialTestBundle frtTestBundle2 = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, localAccountId, "test.user@tenant.onmicrosoft.com", randomHomeAccountId, ENVIRONMENT, realm, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID + "2", SECRET, MicrosoftStsAccountCredentialAdapterTest.MOCK_ID_TOKEN_WITH_CLAIMS, "1", SESSION_KEY, CredentialType.IdToken);
    when(mockCredentialAdapter.createAccount(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle2.mGeneratedAccount);
    when(mockCredentialAdapter.createAccessToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle2.mGeneratedAccessToken);
    when(mockCredentialAdapter.createRefreshToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle2.mGeneratedRefreshToken);
    when(mockCredentialAdapter.createIdToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundle2.mGeneratedIdToken);
    // Save the family token data
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    // Test only one FRT exists and it is the second one saved...
    final ICacheRecord cacheRecord = mOauth2TokenCache.loadByFamilyId(CLIENT_ID, TARGET, frtTestBundle2.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(cacheRecord);
    assertNotNull(cacheRecord.getRefreshToken());
    assertNotNull(cacheRecord.getAccessToken());
    assertNotNull(cacheRecord.getIdToken());
    assertEquals(CLIENT_ID + "2", cacheRecord.getRefreshToken().getClientId());
    // Check querying for the FRT in the second app yields the same FRT
    final ICacheRecord cacheRecord2 = mOauth2TokenCache.loadByFamilyId(CLIENT_ID + "2", TARGET, frtTestBundle2.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(cacheRecord2);
    assertNotNull(cacheRecord2.getRefreshToken());
    assertNotNull(cacheRecord2.getAccessToken());
    assertNotNull(cacheRecord2.getIdToken());
    assertEquals(CLIENT_ID + "2", cacheRecord2.getRefreshToken().getClientId());
    // Test querying with a different account yields nothing at all....
    final AccountRecord randomAcct = new AccountRecord();
    randomAcct.setAuthorityType(AUTHORITY_TYPE);
    randomAcct.setLocalAccountId(UUID.randomUUID().toString());
    randomAcct.setUsername("foo@bar.com");
    randomAcct.setHomeAccountId(UUID.randomUUID().toString());
    randomAcct.setEnvironment(ENVIRONMENT);
    randomAcct.setRealm(REALM);
    final ICacheRecord cacheRecord3 = mOauth2TokenCache.loadByFamilyId(CLIENT_ID + "2", TARGET, randomAcct, BEARER_SCHEME);
    assertNotNull(cacheRecord3);
    assertNotNull(cacheRecord3.getAccount());
    assertNull(cacheRecord3.getRefreshToken());
    assertNull(cacheRecord3.getAccessToken());
    assertNull(cacheRecord3.getIdToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 10 with AccountRecord

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

the class MsalCppOAuth2TokenCacheTest method forceRemoveAccountWithHomeAccountIdTest.

@Test
public void forceRemoveAccountWithHomeAccountIdTest() throws ClientException {
    // Get the generated account
    final AccountRecord generatedAccount = mTestBundle.mGeneratedAccount;
    // Save it to the cache
    mCppCache.saveAccountRecord(generatedAccount);
    // Do not save any credentials for this account...
    final AccountDeletionRecord deletionRecord = mCppCache.forceRemoveAccount(generatedAccount.getHomeAccountId(), "", "");
    Assert.assertEquals(1, deletionRecord.size());
    // Try to restore it
    final AccountRecord restoredAccount = mCppCache.getAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    // Make sure it doesn't exist....
    Assert.assertNull(restoredAccount);
}
Also used : AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) 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