Search in sources :

Example 6 with StorageHelper

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

the class DefaultTokenCacheStore method getStorageHelper.

/**
 * Method that allows to mock StorageHelper class and use custom encryption in UTs.
 */
protected StorageHelper getStorageHelper() {
    synchronized (LOCK) {
        if (sHelper == null) {
            Logger.v(TAG, "Started to initialize storage helper");
            sHelper = new StorageHelper(mContext);
            Logger.v(TAG, "Finished to initialize storage helper");
        }
    }
    return sHelper;
}
Also used : StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper)

Example 7 with StorageHelper

use of com.microsoft.identity.common.adal.internal.cache.StorageHelper 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 8 with StorageHelper

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

the class DefaultTokenCacheStoreTests method mockDefaultCacheStore.

private DefaultTokenCacheStore mockDefaultCacheStore(final String dateTimeString) throws GeneralSecurityException, IOException {
    final StorageHelper mockSecure = Mockito.mock(StorageHelper.class);
    Context mockContext = mock(Context.class);
    SharedPreferences prefs = mock(SharedPreferences.class);
    when(prefs.contains("testkey")).thenReturn(true);
    when(prefs.getString("testkey", null)).thenReturn("test_encrypted");
    when(mockSecure.decrypt("test_encrypted")).thenReturn("{\"mClientId\":\"clientId23\",\"mExpiresOn\":\"" + dateTimeString + "\"}");
    when(mockContext.getSharedPreferences("com.microsoft.aad.adal.cache", Activity.MODE_PRIVATE)).thenReturn(prefs);
    DefaultTokenCacheStore cache = new DefaultTokenCacheStore(mockContext) {

        @Override
        protected StorageHelper getStorageHelper() {
            return mockSecure;
        }
    };
    return cache;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper)

Example 9 with StorageHelper

use of com.microsoft.identity.common.adal.internal.cache.StorageHelper 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);
}
Also used : Context(android.content.Context) MicrosoftStsAuthorizationRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationRequest) IAccountCredentialAdapter(com.microsoft.identity.common.internal.cache.IAccountCredentialAdapter) SharedPreferencesAccountCredentialCache(com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache) MicrosoftStsOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper) ICacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.ICacheKeyValueDelegate) MicrosoftStsTokenResponse(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenResponse) CacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate) ICacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.ICacheKeyValueDelegate) Before(org.junit.Before)

Example 10 with StorageHelper

use of com.microsoft.identity.common.adal.internal.cache.StorageHelper 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);
}
Also used : Context(android.content.Context) SharedPreferencesAccountCredentialCache(com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper) CacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate) Before(org.junit.Before)

Aggregations

StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)12 Context (android.content.Context)5 IStorageHelper (com.microsoft.identity.common.adal.internal.cache.IStorageHelper)4 CacheKeyValueDelegate (com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate)4 SharedPreferencesAccountCredentialCache (com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache)4 SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)3 Test (org.junit.Test)3 IAccountCredentialCache (com.microsoft.identity.common.internal.cache.IAccountCredentialCache)2 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)2 Before (org.junit.Before)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 SharedPreferences (android.content.SharedPreferences)1 CookieManager (android.webkit.CookieManager)1 UiThreadTest (androidx.test.annotation.UiThreadTest)1 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)1 IAccountCredentialAdapter (com.microsoft.identity.common.internal.cache.IAccountCredentialAdapter)1 ICacheKeyValueDelegate (com.microsoft.identity.common.internal.cache.ICacheKeyValueDelegate)1 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1