Search in sources :

Example 6 with ICacheRecord

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

the class BrokerOAuth2TokenCacheTest method testGetFociCacheRecordsEmpty.

@Test
public void testGetFociCacheRecordsEmpty() throws ClientException {
    configureMocksForAppUid();
    final ICacheRecord result = mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<ICacheRecord> fociCacheRecords = mBrokerOAuth2TokenCache.getFociCacheRecords();
    assertNotNull(fociCacheRecords);
    assertTrue(fociCacheRecords.isEmpty());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 7 with ICacheRecord

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

the class BrokerOAuth2TokenCacheTest method testGetFociCacheRecords.

@Test
public void testGetFociCacheRecords() throws ClientException {
    configureMocksForFoci();
    final ICacheRecord result = mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<ICacheRecord> fociCacheRecords = mBrokerOAuth2TokenCache.getFociCacheRecords();
    assertNotNull(fociCacheRecords);
    assertFalse(fociCacheRecords.isEmpty());
    assertEquals(result.getRefreshToken(), fociCacheRecords.get(0).getRefreshToken());
    assertEquals(result.getIdToken(), fociCacheRecords.get(0).getIdToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 8 with ICacheRecord

use of com.microsoft.identity.common.internal.cache.ICacheRecord 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 ICacheRecord

use of com.microsoft.identity.common.internal.cache.ICacheRecord 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 ICacheRecord

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

the class MicrosoftFamilyOAuth2TokenCacheTest method testRetrieveFrt.

@Test
public void testRetrieveFrt() throws ClientException {
    final String randomHomeAccountId = UUID.randomUUID().toString();
    final AccountCredentialTestBundle frtTestBundle = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, UUID.randomUUID().toString(), "test.user@tenant.onmicrosoft.com", randomHomeAccountId, ENVIRONMENT, UUID.randomUUID().toString(), 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);
    // Save the family token data
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final ICacheRecord familyCacheRecord = mOauth2TokenCache.loadByFamilyId(null, TARGET, frtTestBundle.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(familyCacheRecord);
    assertNotNull(familyCacheRecord.getAccount());
    assertNotNull(familyCacheRecord.getRefreshToken());
    assertNull(familyCacheRecord.getIdToken());
    assertNull(familyCacheRecord.getAccessToken());
    final ICacheRecord familyCacheRecordWithClientId = mOauth2TokenCache.loadByFamilyId(CLIENT_ID, TARGET, frtTestBundle.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(familyCacheRecordWithClientId);
    assertNotNull(familyCacheRecordWithClientId.getAccount());
    assertNotNull(familyCacheRecordWithClientId.getRefreshToken());
    assertNotNull(familyCacheRecordWithClientId.getIdToken());
    assertNotNull(familyCacheRecordWithClientId.getAccessToken());
    final ICacheRecord familyCacheRecordWithClientIdButNonMatchingTarget = mOauth2TokenCache.loadByFamilyId(CLIENT_ID, TARGET, frtTestBundle.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(familyCacheRecordWithClientIdButNonMatchingTarget);
    assertNotNull(familyCacheRecordWithClientIdButNonMatchingTarget.getAccount());
    assertNotNull(familyCacheRecordWithClientIdButNonMatchingTarget.getRefreshToken());
    assertNotNull(familyCacheRecordWithClientIdButNonMatchingTarget.getIdToken());
    assertNotNull(familyCacheRecordWithClientIdButNonMatchingTarget.getAccessToken());
    final ICacheRecord wrongClientIdResult = mOauth2TokenCache.loadByFamilyId("12345", TARGET, frtTestBundle.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(wrongClientIdResult);
    assertNotNull(wrongClientIdResult.getAccount());
    assertNotNull(wrongClientIdResult.getRefreshToken());
    assertNull(wrongClientIdResult.getIdToken());
    assertNull(wrongClientIdResult.getAccessToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Aggregations

ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)51 Test (org.junit.Test)40 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)11 ArrayList (java.util.ArrayList)5 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)4 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)4 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)4 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)4 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)4 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)4 ClientException (com.microsoft.identity.common.exception.ClientException)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3 BrokerApplicationMetadata (com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata)3 BrokerOAuth2TokenCache (com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache)3 MicrosoftFamilyOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache)3 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)3 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)3 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)3 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)3 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)3