use of com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache in project azure-activedirectory-library-for-android by AzureAD.
the class TokenCacheAccessorTests method testMsalCacheIsUpdated.
/**
* This test asserts that the MSAL cache is updated by writes to the ADAL cache.
* The ADAL class {@link TokenCacheAccessor} receives an instance of the cache supplied by the host
* app. If the caller has set an instance of {@link DefaultTokenCacheStore}, then ADAL should write a
* matching ID, AT, and Account to the MSAL cache for migration/SSO purposes.
*/
@Test
public void testMsalCacheIsUpdated() throws ServiceException, MalformedURLException {
// Assert our cache is configured for WW
assertEquals(WORLDWIDE_AUTHORITY, mTokenCacheAccessor.getAuthorityUrlWithPreferredCache());
// Create a request to WW
final AuthenticationRequest request = new AuthenticationRequest(WORLDWIDE_AUTHORITY, RESOURCE, CLIENT, REDIRECT, "", PromptBehavior.Auto, "", UUID.randomUUID(), false, null);
final AuthenticationResult result = new AuthenticationResult(MOCK_AT, MOCK_RT, new Date(System.currentTimeMillis() + (3600 * 1000)), false, new UserInfo(USERID_1, GIVEN_NAME, FAMILY_NAME, IDENTITY, USERID_1), TID, MOCK_ID_TOKEN_WITH_CLAIMS, null, CLIENT);
result.setAuthority(WORLDWIDE_AUTHORITY);
result.setClientInfo(new ClientInfo(MOCK_CLIENT_INFO));
result.setResponseReceived(System.currentTimeMillis());
result.setExpiresIn(TimeUnit.HOURS.toSeconds(1));
// Save this to the cache
mTokenCacheAccessor.updateTokenCache(request, result);
assertEquals(WORLDWIDE_AUTHORITY, mTokenCacheAccessor.getAuthorityUrlWithPreferredCache());
// Assert the MSAL replicated cache now contains the account & RT
final IAccountCredentialCache accountCredentialCache = new SharedPreferencesAccountCredentialCache(new CacheKeyValueDelegate(), new SharedPreferencesFileManager(mContext, DEFAULT_ACCOUNT_CREDENTIAL_SHARED_PREFERENCES, new StorageHelper(mContext)));
final MsalOAuth2TokenCache msalCache = new MsalOAuth2TokenCache(mContext, accountCredentialCache, new MicrosoftStsAccountCredentialAdapter());
// Assert the presence of the account
final AccountRecord accountRecord = msalCache.getAccount(LOGIN_WINDOWS_NET, CLIENT, MOCK_UID + "." + MOCK_UTID, MOCK_UTID);
Assert.assertNotNull(accountRecord);
// The RT
final ICacheRecord cacheRecord = msalCache.load(CLIENT, null, accountRecord, new BearerAuthenticationSchemeInternal());
final IdTokenRecord idToken = cacheRecord.getIdToken();
final RefreshTokenRecord refreshToken = cacheRecord.getRefreshToken();
Assert.assertEquals(MOCK_UTID, idToken.getRealm());
Assert.assertEquals(CLIENT, idToken.getClientId());
Assert.assertEquals(accountRecord.getHomeAccountId(), idToken.getHomeAccountId());
Assert.assertEquals(LOGIN_WINDOWS_NET, refreshToken.getEnvironment());
Assert.assertEquals(CLIENT, refreshToken.getClientId());
Assert.assertEquals(accountRecord.getHomeAccountId(), refreshToken.getHomeAccountId());
}
use of com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache 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());
}
use of com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCacheTest method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
mockStrategy = Mockito.mock(MicrosoftStsOAuth2Strategy.class);
mockRequest = Mockito.mock(MicrosoftStsAuthorizationRequest.class);
mockResponse = Mockito.mock(MicrosoftStsTokenResponse.class);
mockCredentialAdapter = Mockito.mock(IAccountCredentialAdapter.class);
// Used by mocks
defaultTestBundleV1 = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, LOCAL_ACCOUNT_ID, USERNAME, HOME_ACCOUNT_ID, ENVIRONMENT, REALM, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID, SECRET, MOCK_ID_TOKEN_WITH_CLAIMS, null, SESSION_KEY, V1IdToken);
defaultTestBundleV2 = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, LOCAL_ACCOUNT_ID, USERNAME, HOME_ACCOUNT_ID, ENVIRONMENT, REALM, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID, SECRET, MOCK_ID_TOKEN_WITH_CLAIMS, null, SESSION_KEY, IdToken);
// Mocks
configureMocksForTestBundle(defaultTestBundleV2);
// Context and related init
final Context context = InstrumentationRegistry.getTargetContext();
mSharedPreferencesFileManager = SharedPreferencesFileManager.getSharedPreferences(context, "test_prefs", new StorageHelper(context));
final ICacheKeyValueDelegate keyValueDelegate = new CacheKeyValueDelegate();
accountCredentialCache = new SharedPreferencesAccountCredentialCache(keyValueDelegate, mSharedPreferencesFileManager);
mOauth2TokenCache = new MsalOAuth2TokenCache<>(context, accountCredentialCache, mockCredentialAdapter);
}
use of com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache in project microsoft-authentication-library-common-for-android by AzureAD.
the class SharedPreferencesAccountCredentialCacheTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
final Context testContext = InstrumentationRegistry.getTargetContext();
mDelegate = new CacheKeyValueDelegate();
mSharedPreferencesFileManager = SharedPreferencesFileManager.getSharedPreferences(testContext, sAccountCredentialSharedPreferences, // Use encrypted storage for tests...
new StorageHelper(testContext));
mSharedPreferencesAccountCredentialCache = new SharedPreferencesAccountCredentialCache(mDelegate, mSharedPreferencesFileManager);
}
Aggregations