Search in sources :

Example 56 with RefreshTokenRecord

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

the class MicrosoftStsAccountCredentialAdapter method createRefreshToken.

@Override
public RefreshTokenRecord createRefreshToken(final MicrosoftStsOAuth2Strategy strategy, final MicrosoftStsAuthorizationRequest request, final MicrosoftStsTokenResponse response) {
    try {
        final long cachedAt = getCachedAt();
        final ClientInfo clientInfo = new ClientInfo(response.getClientInfo());
        final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
        // Required
        refreshToken.setCredentialType(CredentialType.RefreshToken.name());
        refreshToken.setEnvironment(strategy.getIssuerCacheIdentifierFromTokenEndpoint());
        refreshToken.setHomeAccountId(SchemaUtil.getHomeAccountId(clientInfo));
        refreshToken.setClientId(request.getClientId());
        refreshToken.setSecret(response.getRefreshToken());
        // Optional
        refreshToken.setFamilyId(response.getFamilyId());
        refreshToken.setTarget(getTarget(request.getScope(), response.getScope()));
        // TODO are these needed? Expected?
        // generated @ client side
        refreshToken.setCachedAt(String.valueOf(cachedAt));
        return refreshToken;
    } catch (ServiceException e) {
        // TODO handle this properly
        throw new RuntimeException(e);
    }
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)

Example 57 with RefreshTokenRecord

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

the class CacheKeyValueDelegateTest method refreshTokenExtraValueDeserialization.

@Test
public void refreshTokenExtraValueDeserialization() throws JSONException {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name().toLowerCase(Locale.US));
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setTarget(TARGET);
    String serializedValue = mDelegate.generateCacheValue(refreshToken);
    // Turn the serialized value into a JSONObject and start testing field equality.
    final JSONObject jsonObject = new JSONObject(serializedValue);
    // Add more non-standard data to this object...
    final JSONArray numbers = new JSONArray("[1, 2, 3]");
    final JSONArray objects = new JSONArray("[{\"hello\" : \"hallo\"}, {\"goodbye\" : \"auf wiedersehen\"}]");
    jsonObject.put("foo", "bar");
    jsonObject.put("numbers", numbers);
    jsonObject.put("objects", objects);
    serializedValue = jsonObject.toString();
    final RefreshTokenRecord deserializedValue = mDelegate.fromCacheValue(serializedValue, RefreshTokenRecord.class);
    assertNotNull(deserializedValue);
    assertNull(deserializedValue.getAdditionalFields().get(Credential.SerializedNames.ENVIRONMENT));
    assertEquals(HOME_ACCOUNT_ID, deserializedValue.getHomeAccountId());
    assertEquals(ENVIRONMENT, deserializedValue.getEnvironment());
    assertEquals(CredentialType.RefreshToken.name().toLowerCase(Locale.US), deserializedValue.getCredentialType());
    assertEquals(CLIENT_ID, deserializedValue.getClientId());
    assertEquals(TARGET, deserializedValue.getTarget());
    assertEquals(3, deserializedValue.getAdditionalFields().size());
    assertEquals("bar", deserializedValue.getAdditionalFields().get("foo").getAsString());
    assertEquals(numbers.toString(), deserializedValue.getAdditionalFields().get("numbers").toString());
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 58 with RefreshTokenRecord

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

the class CacheKeyValueDelegateTest method refreshTokenCreateCacheKeyCompleteWithFociAlternate.

@Test
public void refreshTokenCreateCacheKeyCompleteWithFociAlternate() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setFamilyId("2");
    refreshToken.setTarget(TARGET);
    final String expectedKey = // just for formatting
    "" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_REFRESH_TOKEN + CACHE_VALUE_SEPARATOR + "2" + CACHE_VALUE_SEPARATOR + CACHE_VALUE_SEPARATOR + TARGET;
    assertEquals(expectedKey, mDelegate.generateCacheKey(refreshToken));
}
Also used : PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 59 with RefreshTokenRecord

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

the class CacheKeyValueDelegateTest method refreshTokenCreateCacheKeyNoHomeAccountId.

@Test
public void refreshTokenCreateCacheKeyNoHomeAccountId() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setTarget(TARGET);
    final String expectedKey = // just for formatting
    "" + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_REFRESH_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + CACHE_VALUE_SEPARATOR + TARGET;
    assertEquals(expectedKey, mDelegate.generateCacheKey(refreshToken));
}
Also used : PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 60 with RefreshTokenRecord

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

the class CacheKeyValueDelegateTest method refreshTokenExtraValueSerialization.

@Test
public void refreshTokenExtraValueSerialization() throws JSONException {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name().toLowerCase(Locale.US));
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setTarget(TARGET);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    // Add some random Json to this object
    JsonElement jsonStr = new JsonPrimitive("bar");
    JsonArray jsonNumberArr = new JsonArray();
    jsonNumberArr.add(1);
    jsonNumberArr.add(2);
    jsonNumberArr.add(3);
    JsonObject jsonObject = new JsonObject();
    jsonObject.add("object_key", new JsonPrimitive("object_value"));
    additionalFields.put("foo", jsonStr);
    additionalFields.put("numbers", jsonNumberArr);
    additionalFields.put("object", jsonObject);
    refreshToken.setAdditionalFields(additionalFields);
    String serializedValue = mDelegate.generateCacheValue(refreshToken);
    JSONObject derivedCacheValueJsonObject = new JSONObject(serializedValue);
    assertEquals(HOME_ACCOUNT_ID, derivedCacheValueJsonObject.getString(RefreshTokenRecord.SerializedNames.HOME_ACCOUNT_ID));
    assertEquals(ENVIRONMENT, derivedCacheValueJsonObject.getString(RefreshTokenRecord.SerializedNames.ENVIRONMENT));
    assertEquals(CredentialType.RefreshToken.name().toLowerCase(Locale.US), derivedCacheValueJsonObject.getString("credential_type"));
    assertEquals(CLIENT_ID, derivedCacheValueJsonObject.getString(RefreshTokenRecord.SerializedNames.CLIENT_ID));
    assertEquals(TARGET, derivedCacheValueJsonObject.getString(RefreshTokenRecord.SerializedNames.TARGET));
    assertEquals("bar", derivedCacheValueJsonObject.getString("foo"));
    final JSONArray jsonArr = derivedCacheValueJsonObject.getJSONArray("numbers");
    assertEquals(3, jsonArr.length());
    final JSONObject jsonObj = derivedCacheValueJsonObject.getJSONObject("object");
    assertEquals("object_value", jsonObj.getString("object_key"));
}
Also used : JsonArray(com.google.gson.JsonArray) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JSONArray(org.json.JSONArray) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

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