Search in sources :

Example 26 with IdTokenRecord

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

the class MicrosoftStsAccountCredentialAdapter method createIdToken.

@Override
public IdTokenRecord createIdToken(final MicrosoftStsOAuth2Strategy strategy, final MicrosoftStsAuthorizationRequest request, final MicrosoftStsTokenResponse response) {
    try {
        final ClientInfo clientInfo = new ClientInfo(response.getClientInfo());
        final IdTokenRecord idToken = new IdTokenRecord();
        // Required fields
        idToken.setHomeAccountId(SchemaUtil.getHomeAccountId(clientInfo));
        idToken.setEnvironment(strategy.getIssuerCacheIdentifierFromTokenEndpoint());
        idToken.setRealm(getRealm(strategy, response));
        idToken.setCredentialType(SchemaUtil.getCredentialTypeFromVersion(response.getIdToken()));
        idToken.setClientId(request.getClientId());
        idToken.setSecret(response.getIdToken());
        idToken.setAuthority(strategy.getAuthorityFromTokenEndpoint());
        return idToken;
    } catch (ServiceException e) {
        // TODO handle this properly
        throw new RuntimeException(e);
    }
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ServiceException(com.microsoft.identity.common.exception.ServiceException) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)

Example 27 with IdTokenRecord

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

the class CacheKeyValueDelegateTest method idTokenExtraValueSerialization.

@Test
public void idTokenExtraValueSerialization() throws JSONException {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setCredentialType(CredentialType.IdToken.name().toLowerCase(Locale.US));
    idToken.setClientId(CLIENT_ID);
    idToken.setRealm(REALM);
    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);
    idToken.setAdditionalFields(additionalFields);
    String serializedValue = mDelegate.generateCacheValue(idToken);
    JSONObject derivedCacheValueJsonObject = new JSONObject(serializedValue);
    assertEquals(HOME_ACCOUNT_ID, derivedCacheValueJsonObject.getString(Credential.SerializedNames.HOME_ACCOUNT_ID));
    assertEquals(ENVIRONMENT, derivedCacheValueJsonObject.getString(Credential.SerializedNames.ENVIRONMENT));
    assertEquals(CredentialType.IdToken.name().toLowerCase(Locale.US), derivedCacheValueJsonObject.getString("credential_type"));
    assertEquals(CLIENT_ID, derivedCacheValueJsonObject.getString(Credential.SerializedNames.CLIENT_ID));
    assertEquals(REALM, derivedCacheValueJsonObject.getString(IdTokenRecord.SerializedNames.REALM));
    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) IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JSONArray(org.json.JSONArray) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 28 with IdTokenRecord

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

the class CacheKeyValueDelegateTest method idTokenCreateCacheValue.

@Test
public void idTokenCreateCacheValue() throws JSONException {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setCredentialType(CredentialType.IdToken.name().toLowerCase(Locale.US));
    idToken.setClientId(CLIENT_ID);
    idToken.setRealm(REALM);
    final String serializedValue = mDelegate.generateCacheValue(idToken);
    // Turn the serialized value into a JSONObject and start testing field equality.
    final JSONObject jsonObject = new JSONObject(serializedValue);
    assertEquals(HOME_ACCOUNT_ID, jsonObject.getString(Credential.SerializedNames.HOME_ACCOUNT_ID));
    assertEquals(ENVIRONMENT, jsonObject.getString(Credential.SerializedNames.ENVIRONMENT));
    assertEquals(CredentialType.IdToken.name().toLowerCase(Locale.US), jsonObject.getString("credential_type"));
    assertEquals(CLIENT_ID, jsonObject.getString(Credential.SerializedNames.CLIENT_ID));
    assertEquals(REALM, jsonObject.getString(IdTokenRecord.SerializedNames.REALM));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 29 with IdTokenRecord

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

the class CacheKeyValueDelegateTest method idTokenCreateCacheKeyNoRealm.

@Test
public void idTokenCreateCacheKeyNoRealm() {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setCredentialType(CredentialType.IdToken.name());
    idToken.setClientId(CLIENT_ID);
    final String expectedKey = // just for formatting
    "" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_ID_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + CACHE_VALUE_SEPARATOR;
    assertEquals(expectedKey, mDelegate.generateCacheKey(idToken));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Test(org.junit.Test)

Example 30 with IdTokenRecord

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

the class CacheKeyValueDelegateTest method idTokenCreateCacheKeyNoHomeAccountId.

@Test
public void idTokenCreateCacheKeyNoHomeAccountId() {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setCredentialType(CredentialType.IdToken.name());
    idToken.setClientId(CLIENT_ID);
    idToken.setRealm(REALM);
    final String expectedKey = // just for formatting
    "" + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR + CREDENTIAL_TYPE_ID_TOKEN + CACHE_VALUE_SEPARATOR + CLIENT_ID + CACHE_VALUE_SEPARATOR + REALM + CACHE_VALUE_SEPARATOR;
    assertEquals(expectedKey, mDelegate.generateCacheKey(idToken));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Test(org.junit.Test)

Aggregations

IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)31 Test (org.junit.Test)17 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)9 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)9 Credential (com.microsoft.identity.common.internal.dto.Credential)7 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)4 ArrayList (java.util.ArrayList)4 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)2 JSONArray (org.json.JSONArray)2 Nullable (androidx.annotation.Nullable)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1