Search in sources :

Example 31 with RefreshTokenRecord

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

the class MsalOAuth2TokenCacheTest method testGetFamilyRefreshTokenForHomeAccountIdValidCase.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdValidCase() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    accountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(AccessToken.name());
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm("Foo");
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    accountCredentialCache.saveCredential(accessToken);
    // Save a Family RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setFamilyId("1");
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    accountCredentialCache.saveCredential(refreshToken);
    final IdTokenRecord id = new IdTokenRecord();
    id.setHomeAccountId(HOME_ACCOUNT_ID);
    id.setEnvironment(ENVIRONMENT);
    id.setRealm(REALM2);
    id.setCredentialType(IdToken.name());
    id.setClientId(CLIENT_ID);
    id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
    id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
    accountCredentialCache.saveCredential(id);
    final RefreshTokenRecord refreshTokenRecord = mOauth2TokenCache.getFamilyRefreshTokenForHomeAccountId(HOME_ACCOUNT_ID);
    assertNotNull(refreshTokenRecord);
    assertEquals(refreshTokenRecord.getSecret(), SECRET);
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 32 with RefreshTokenRecord

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

the class MsalOAuth2TokenCacheTest method testGetFamilyRefreshTokenForHomeAccountIdNullCase.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdNullCase() {
    final RefreshTokenRecord refreshTokenRecord = mOauth2TokenCache.getFamilyRefreshTokenForHomeAccountId(HOME_ACCOUNT_ID);
    assertNull(refreshTokenRecord);
}
Also used : RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Example 33 with RefreshTokenRecord

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

the class MsalOAuth2TokenCacheTest method testFrtFallback.

@Test
public void testFrtFallback() throws ClientException {
    // This test verifies changes in common/#893
    // We will fallback on an FRT in the local cache for the current user if one is avail
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV2);
    assertEquals(1, result.size());
    final ICacheRecord entry = result.get(0);
    assertNotNull(entry.getAccount());
    assertNotNull(entry.getIdToken());
    assertNotNull(entry.getAccessToken());
    assertNotNull(entry.getRefreshToken());
    // delete the existing RT and insert our FRT
    accountCredentialCache.removeCredential(entry.getRefreshToken());
    // Modify the existing RT to change the client_id and set a FoCI affiliation
    final String fociRtClientId = UUID.randomUUID().toString();
    final RefreshTokenRecord modifiedRT = entry.getRefreshToken();
    modifiedRT.setFamilyId("1");
    modifiedRT.setClientId(fociRtClientId);
    accountCredentialCache.saveCredential(modifiedRT);
    final ICacheRecord secondaryLoad = mOauth2TokenCache.load(entry.getAccessToken().getClientId(), TARGET, entry.getAccount(), BEARER_AUTHENTICATION_SCHEME);
    assertNotNull(secondaryLoad.getRefreshToken());
    assertEquals(fociRtClientId, secondaryLoad.getRefreshToken().getClientId());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) Test(org.junit.Test)

Example 34 with RefreshTokenRecord

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

the class MicrosoftStsAccountCredentialAdapterTest method createRefreshToken.

@Test
public void createRefreshToken() {
    final RefreshTokenRecord refreshToken = mAccountCredentialAdapter.createRefreshToken(mockStrategy, mockRequest, mockResponse);
    assertNotNull(refreshToken);
    assertEquals(MOCK_SCOPE, refreshToken.getTarget());
    assertNotNull(refreshToken.getCachedAt());
    assertEquals(MOCK_FAMILY_ID, refreshToken.getFamilyId());
}
Also used : RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 35 with RefreshTokenRecord

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

the class BaseController method getAccountWithFRTIfAvailable.

@Nullable
private AccountRecord getAccountWithFRTIfAvailable(@NonNull final SilentTokenCommandParameters parameters, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final MsalOAuth2TokenCache msalOAuth2TokenCache) {
    final String homeAccountId = parameters.getAccount().getHomeAccountId();
    final String clientId = parameters.getClientId();
    // check for FOCI tokens for the homeAccountId
    final RefreshTokenRecord refreshTokenRecord = msalOAuth2TokenCache.getFamilyRefreshTokenForHomeAccountId(homeAccountId);
    if (refreshTokenRecord != null) {
        try {
            // foci token is available, make a request to service to see if the client id is FOCI and save the tokens
            TokenCacheItemMigrationAdapter.tryFociTokenWithGivenClientId(parameters.getOAuth2TokenCache(), clientId, parameters.getRedirectUri(), refreshTokenRecord, parameters.getAccount());
            // Try to look for account again in the cache
            return parameters.getOAuth2TokenCache().getAccountByLocalAccountId(null, clientId, parameters.getAccount().getLocalAccountId());
        } catch (IOException | ClientException e) {
            Logger.warn(TAG, "Error while attempting to validate client: " + clientId + " is part of family " + e.getMessage());
        }
    } else {
        Logger.info(TAG, "No Foci tokens found for homeAccountId " + homeAccountId);
    }
    return null;
}
Also used : RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) IOException(java.io.IOException) ClientException(com.microsoft.identity.common.exception.ClientException) Nullable(androidx.annotation.Nullable)

Aggregations

RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)63 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)51 Test (org.junit.Test)51 Credential (com.microsoft.identity.common.internal.dto.Credential)32 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)29 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)9 JsonElement (com.google.gson.JsonElement)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 HashMap (java.util.HashMap)4 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 JSONObject (org.json.JSONObject)3 Nullable (androidx.annotation.Nullable)2 ClientException (com.microsoft.identity.common.exception.ClientException)2 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)2 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)2 JSONArray (org.json.JSONArray)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1