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);
}
}
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"));
}
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));
}
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));
}
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));
}
Aggregations