Skip to content

Commit e357416

Browse files
committed
update client to make it capable of calling oneauth upn json
1 parent 3d10e79 commit e357416

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

LabApiUtilities/src/main/com/microsoft/identity/labapi/utilities/client/LabClient.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class LabClient implements ILabClient {
7373
public static final long TEMP_USER_WAIT_TIME = TimeUnit.SECONDS.toMillis(35);
7474

7575
private static final String ACCOUNT_UPN_JSON_STRING_SECRET_NAME = "Android-ID4SLAB2-User-Identifiers";
76+
private String mAlternativeUpnJsonStringSecretName;
7677
private Map<String, LabJsonStringAccountEntry> labUPNJsonMap = null;
7778

7879
/**
@@ -108,6 +109,18 @@ private static void setLatestLabAccount(final ILabAccount account) {
108109
latestLabAccount = account;
109110
}
110111

112+
/**
113+
* This method allows tests to specify an alternative secret name for the UPN JSON string in Key Vault,
114+
* as the default points specifically to Android Team's upn json. T
115+
*
116+
* IF YOU ARE THE ONEAUTH TEAM, PLEASE USE THIS METHOD TO POINT TO YOUR OWN JSON SECRET NAME
117+
*
118+
* @param secretName The name of the alternative Key Vault secret to use for fetching the UPN JSON string.
119+
*/
120+
public void setAccountUpnJsonStringSecretName(final String secretName) {
121+
mAlternativeUpnJsonStringSecretName = secretName;
122+
}
123+
111124
@Override
112125
public ILabAccount getLabAccount(@NonNull final LabQuery labQuery) throws LabApiException {
113126
// Adding a second attempt here, api sometimes fails to fetch the user.
@@ -375,8 +388,15 @@ public Map<String, LabJsonStringAccountEntry> getAccountMapJsonFromMobileBuildKe
375388
);
376389
final KeyVaultSecretsApi keyVaultSecretsApi = new KeyVaultSecretsApi(KeyVaultSecretsApi.MOBILE_BUILD_VAULT_URL);
377390

391+
final String keyvaultSecret;
392+
if (mAlternativeUpnJsonStringSecretName != null && !mAlternativeUpnJsonStringSecretName.isEmpty()) {
393+
keyvaultSecret = mAlternativeUpnJsonStringSecretName;
394+
} else {
395+
keyvaultSecret = ACCOUNT_UPN_JSON_STRING_SECRET_NAME;
396+
}
397+
378398
try {
379-
final SecretBundle secretBundle = keyVaultSecretsApi.getKeyVaultSecret(ACCOUNT_UPN_JSON_STRING_SECRET_NAME);
399+
final SecretBundle secretBundle = keyVaultSecretsApi.getKeyVaultSecret(keyvaultSecret);
380400

381401
labUPNJsonMap = LabJsonStringAccountEntry.parseJsonToMap(secretBundle.getValue());
382402
return labUPNJsonMap;

LabApiUtilities/src/main/com/microsoft/identity/labapi/utilities/client/LabJsonStringAccountEntry.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@
2424
package com.microsoft.identity.labapi.utilities.client;
2525

2626
import com.google.gson.Gson;
27+
import com.google.gson.GsonBuilder;
28+
import com.google.gson.JsonDeserializationContext;
29+
import com.google.gson.JsonDeserializer;
30+
import com.google.gson.JsonElement;
31+
import com.google.gson.JsonObject;
32+
import com.google.gson.JsonParseException;
2733
import com.google.gson.annotations.SerializedName;
2834
import com.google.gson.reflect.TypeToken;
2935

3036
import java.io.Serializable;
37+
import java.lang.reflect.Field;
3138
import java.lang.reflect.Type;
39+
import java.util.HashMap;
40+
import java.util.Locale;
3241
import java.util.Map;
3342

3443
import lombok.Getter;
@@ -70,8 +79,48 @@ public class LabJsonStringAccountEntry implements Serializable {
7079
* @return a map of key to LabJsonStringAccountEntry
7180
*/
7281
public static Map<String, LabJsonStringAccountEntry> parseJsonToMap(String json) {
73-
Gson gson = new Gson();
82+
final Gson gson = new GsonBuilder()
83+
.registerTypeAdapter(LabJsonStringAccountEntry.class, new CaseInsensitiveDeserializer())
84+
.create();
7485
Type type = new TypeToken<Map<String, LabJsonStringAccountEntry>>(){}.getType();
7586
return gson.fromJson(json, type);
7687
}
88+
89+
/**
90+
* Gson deserializer that matches JSON property names to {@link SerializedName} values
91+
* in a case-insensitive manner, so {@code upn}, {@code Upn} and {@code UPN} are all
92+
* treated as the same field.
93+
*/
94+
private static class CaseInsensitiveDeserializer implements JsonDeserializer<LabJsonStringAccountEntry> {
95+
@Override
96+
public LabJsonStringAccountEntry deserialize(JsonElement json, Type typeOfT,
97+
JsonDeserializationContext context) throws JsonParseException {
98+
final JsonObject src = json.getAsJsonObject();
99+
100+
// Build a lowercase-keyed view of the incoming JSON object.
101+
final Map<String, JsonElement> lowerCased = new HashMap<>();
102+
for (Map.Entry<String, JsonElement> e : src.entrySet()) {
103+
lowerCased.put(e.getKey().toLowerCase(Locale.ROOT), e.getValue());
104+
}
105+
106+
final LabJsonStringAccountEntry result = new LabJsonStringAccountEntry();
107+
for (Field field : LabJsonStringAccountEntry.class.getDeclaredFields()) {
108+
final SerializedName annotation = field.getAnnotation(SerializedName.class);
109+
if (annotation == null) {
110+
continue;
111+
}
112+
final JsonElement value = lowerCased.get(annotation.value().toLowerCase(Locale.ROOT));
113+
if (value == null || value.isJsonNull()) {
114+
continue;
115+
}
116+
try {
117+
field.setAccessible(true);
118+
field.set(result, context.deserialize(value, field.getGenericType()));
119+
} catch (IllegalAccessException ex) {
120+
throw new JsonParseException("Failed to set field " + field.getName(), ex);
121+
}
122+
}
123+
return result;
124+
}
125+
}
77126
}

LabApiUtilities/src/test/com/microsoft/identity/labapi/utilities/client/LabClientTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ public void canFetchAccountUpnJsonStringOtherAccounts() {
209209
}
210210
}
211211

212+
@Test
213+
public void canFetchAccountFromDifferentJson() {
214+
try {
215+
mLabClient.setAccountUpnJsonStringSecretName("OneAuth-ID4SLAB2-User-Identifiers");
216+
final ILabAccount labAccount = mLabClient.getAccountFromLabJsonStringInMobileBuildVault(UserType.BASIC);
217+
assertLabAccount(labAccount, UserType.BASIC, DEFAULT_LAB_NAME);
218+
} catch (final LabApiException e) {
219+
throw new AssertionError(e);
220+
}
221+
}
222+
212223
// Helper to assert common properties of a lab account
213224
private void assertLabAccount(final ILabAccount labAccount,
214225
final UserType expectedUserType,

0 commit comments

Comments
 (0)