Search in sources :

Example 1 with SharedPreferencesFileManager

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

the class TestUtils method getEncryptedSharedPreferences.

/**
 * Return a SharedPreferences instance that works with stores containing encrypted values.
 *
 * @param sharedPrefName the name of the shared preferences file.
 * @return A SharedPreferences that decrypts and encrypts the values.
 */
public static SharedPreferencesFileManager getEncryptedSharedPreferences(final String sharedPrefName) {
    final Context context = ApplicationProvider.getApplicationContext();
    final StorageHelper storageHelper = new StorageHelper(context);
    final SharedPreferencesFileManager barePreferences = SharedPreferencesFileManager.getSharedPreferences(context, sharedPrefName, storageHelper);
    return barePreferences;
}
Also used : Context(android.content.Context) SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper)

Example 2 with SharedPreferencesFileManager

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

the class TestUtils method clearCache.

public static void clearCache(final String sharedPrefName) {
    SharedPreferencesFileManager sharedPreferences = getSharedPreferences(sharedPrefName);
    sharedPreferences.clear();
}
Also used : SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)

Example 3 with SharedPreferencesFileManager

use of com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager 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());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) BearerAuthenticationSchemeInternal(com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal) SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache) IAccountCredentialCache(com.microsoft.identity.common.internal.cache.IAccountCredentialCache) Date(java.util.Date) CacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate) MicrosoftStsAccountCredentialAdapter(com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter) 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) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo) Test(org.junit.Test)

Example 4 with SharedPreferencesFileManager

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

the class BrokerProxy method saveAccount.

/**
 * Tracks accounts that user of the ADAL accessed from AccountManager. It
 * uses this list, when app calls remove accounts. It limits the account
 * removal to specific subset.
 */
@Override
public void saveAccount(String accountName) {
    if (accountName == null || accountName.isEmpty()) {
        return;
    }
    SharedPreferencesFileManager prefs = new SharedPreferencesFileManager(mContext, KEY_SHARED_PREF_ACCOUNT_LIST);
    String accountList = prefs.getString(KEY_APP_ACCOUNTS_FOR_TOKEN_REMOVAL);
    accountList = null != accountList ? accountList : "";
    if (!accountList.contains(KEY_ACCOUNT_LIST_DELIM + accountName)) {
        accountList += KEY_ACCOUNT_LIST_DELIM + accountName;
        prefs.putString(KEY_APP_ACCOUNTS_FOR_TOKEN_REMOVAL, accountList);
    }
}
Also used : SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)

Example 5 with SharedPreferencesFileManager

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

Aggregations

SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)8 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)3 Context (android.content.Context)2 CacheKeyValueDelegate (com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate)2 IAccountCredentialCache (com.microsoft.identity.common.internal.cache.IAccountCredentialCache)2 SharedPreferencesAccountCredentialCache (com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache)2 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)2 Test (org.junit.Test)2 UiThreadTest (androidx.test.annotation.UiThreadTest)1 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)1 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1 MicrosoftStsAccountCredentialAdapter (com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter)1 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)1 Credential (com.microsoft.identity.common.internal.dto.Credential)1 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)1 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)1 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)1 Date (java.util.Date)1 Map (java.util.Map)1