use of com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter 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.MicrosoftStsAccountCredentialAdapter in project microsoft-authentication-library-common-for-android by AzureAD.
the class MicrosoftStsAccountCredentialAdapterTest method setUp.
@Before
public void setUp() throws MalformedURLException {
mockStrategy = Mockito.mock(MicrosoftStsOAuth2Strategy.class);
mockRequest = Mockito.mock(MicrosoftStsAuthorizationRequest.class);
mockResponse = Mockito.mock(MicrosoftStsTokenResponse.class);
mockAccount = Mockito.mock(MicrosoftStsAccount.class);
when(mockStrategy.createAccount(any(MicrosoftStsTokenResponse.class))).thenReturn(mockAccount);
when(mockStrategy.getIssuerCacheIdentifier(mockRequest)).thenReturn(MOCK_ENVIRONMENT);
when(mockStrategy.getIssuerCacheIdentifierFromTokenEndpoint()).thenReturn(MOCK_ENVIRONMENT);
when(mockStrategy.getAuthorityFromTokenEndpoint()).thenReturn(MOCK_AUTHORITY);
when(mockRequest.getAuthority()).thenReturn(new URL(MOCK_AUTHORITY));
when(mockResponse.getIdToken()).thenReturn(MOCK_ID_TOKEN_WITH_CLAIMS);
when(mockResponse.getClientInfo()).thenReturn(MOCK_CLIENT_INFO);
when(mockResponse.getScope()).thenReturn(MOCK_SCOPE);
when(mockAccount.getRealm()).thenReturn(MOCK_TID);
when(mockAccount.getHomeAccountId()).thenReturn(MOCK_UID + "." + MOCK_UTID);
when(mockAccount.getEnvironment()).thenReturn(MOCK_ENVIRONMENT);
when(mockAccount.getRealm()).thenReturn(MOCK_TID);
when(mockAccount.getLocalAccountId()).thenReturn(MOCK_OID);
when(mockAccount.getUsername()).thenReturn(MOCK_PREFERRED_USERNAME);
when(mockAccount.getAuthorityType()).thenReturn("MSSTS");
when(mockAccount.getFirstName()).thenReturn(MOCK_GIVEN_NAME);
when(mockAccount.getName()).thenReturn(MOCK_NAME);
when(mockAccount.getMiddleName()).thenReturn(MOCK_MIDDLE_NAME);
when(mockAccount.getFamilyName()).thenReturn(MOCK_FAMILY_NAME);
when(mockAccount.getClientInfo()).thenReturn(MOCK_CLIENT_INFO);
when(mockRequest.getScope()).thenReturn(MOCK_SCOPE);
when(mockResponse.getExpiresIn()).thenReturn(MOCK_EXPIRES_IN);
when(mockResponse.getExtExpiresIn()).thenReturn(MOCK_EXT_EXPIRES_IN);
when(mockResponse.getFamilyId()).thenReturn(MOCK_FAMILY_ID);
mAccountCredentialAdapter = new MicrosoftStsAccountCredentialAdapter();
}
Aggregations