Skip to content

Commit e28cee9

Browse files
authored
Merge pull request #264 from IABTechLab/ccm-UID2-2830-add-site-new-app-names
UID2-2830 add site new app names
2 parents e9aee6e + 3b22fed commit e28cee9

File tree

6 files changed

+273
-27
lines changed

6 files changed

+273
-27
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ and re-initialize your localstack by running `docker-compose restart`.
1717

1818
### Authentication and Authorization
1919

20-
When running locally, Okta OAuth is disabled and users are logged in as *[email protected]* via the
21-
`is_auth_disabled` flag. The user has all the rights available.
20+
When running locally, set the `is_auth_disabled` flag to true. It disables Okta OAuth and users are logged in as *[email protected]*. The user has all the rights available.
2221

2322
If you want to test with Okta OAuth, set the `is_auth_disabled` flag to `false`, and fill in the `okta_client_secret` with the value under "Okta localhost deployment" in 1Password.
2423

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<!-- check micrometer.version vertx-micrometer-metrics consumes before bumping up -->
1717
<micrometer.version>1.1.0</micrometer.version>
1818
<junit-jupiter.version>5.7.0</junit-jupiter.version>
19-
<uid2-shared.version>7.7.6-1e644a0ded</uid2-shared.version>
19+
<uid2-shared.version>7.9.0</uid2-shared.version>
2020
<okta-jwt.version>0.5.8</okta-jwt.version>
2121
<image.version>${project.version}</image.version>
2222
</properties>

src/main/java/com/uid2/admin/vertx/service/SiteService.java

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.uid2.admin.vertx.service;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.ObjectWriter;
45
import com.uid2.admin.auth.AdminAuthMiddleware;
56
import com.uid2.admin.legacy.ILegacyClientKeyProvider;
@@ -83,6 +84,11 @@ public void setupRoutes(Router router) {
8384
this.handleSiteDomains(ctx);
8485
}
8586
}, Role.MAINTAINER, Role.SHARING_PORTAL));
87+
router.post("/api/site/app_names").blockingHandler(auth.handle((ctx) -> {
88+
synchronized (writeLock) {
89+
this.handleSiteAppNames(ctx);
90+
}
91+
}, Role.MAINTAINER, Role.SHARING_PORTAL));
8692
router.post("/api/site/update").blockingHandler(auth.handle((ctx) -> {
8793
synchronized (writeLock) {
8894
this.handleSiteUpdate(ctx);
@@ -127,12 +133,16 @@ private static JsonObject createSiteJsonObject(Site site, Map<Integer, List<Lega
127133
JsonArray domainNamesJa = new JsonArray();
128134
site.getDomainNames().forEach(domainNamesJa::add);
129135

136+
JsonArray appNamesJa = new JsonArray();
137+
site.getAppNames().forEach(appNamesJa::add);
138+
130139
jo.put("id", site.getId());
131140
jo.put("name", site.getName());
132141
jo.put("description", site.getDescription());
133142
jo.put("enabled", site.isEnabled());
134143
jo.put("clientTypes", site.getClientTypes());
135144
jo.put("domain_names", domainNamesJa);
145+
jo.put("app_names", appNamesJa);
136146
jo.put("visible", site.isVisible());
137147
jo.put("created", site.getCreated());
138148

@@ -197,6 +207,15 @@ private void handleSiteAdd(RoutingContext rc) {
197207
}
198208
}
199209

210+
Set<String> normalizedAppNames = new HashSet<>();
211+
if (body != null) {
212+
JsonArray appNamesJa = body.getJsonArray("app_names");
213+
if (appNamesJa != null) {
214+
normalizedAppNames = getNormalizedAppNames(rc, appNamesJa);
215+
if (normalizedAppNames == null) return;
216+
}
217+
}
218+
200219
boolean enabled = false;
201220
List<String> enabledFlags = rc.queryParam("enabled");
202221
if (!enabledFlags.isEmpty()) {
@@ -220,7 +239,7 @@ private void handleSiteAdd(RoutingContext rc) {
220239
.collect(Collectors.toList());
221240
final int siteId = 1 + sites.stream().mapToInt(Site::getId).max().orElse(Const.Data.AdvertisingTokenSiteId);
222241

223-
final Site newSite = new Site(siteId, name, description, enabled, types, new HashSet<>(normalizedDomainNames), true);
242+
final Site newSite = new Site(siteId, name, description, enabled, types, new HashSet<>(normalizedDomainNames), normalizedAppNames, true);
224243
// add site to the array
225244
sites.add(newSite);
226245

@@ -247,15 +266,9 @@ private void handleSiteTypesSet(RoutingContext rc) {
247266
return;
248267
}
249268

250-
final List<Site> sites = this.siteProvider.getAllSites()
251-
.stream().sorted(Comparator.comparingInt(Site::getId))
252-
.collect(Collectors.toList());
253-
254269
existingSite.setClientTypes(types);
255270

256-
storeWriter.upload(sites, null);
257-
258-
rc.response().end(jsonWriter.writeValueAsString(existingSite));
271+
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
259272
} catch (Exception e) {
260273
rc.fail(500, e);
261274
}
@@ -318,15 +331,36 @@ private void handleSiteDomains(RoutingContext rc) {
318331

319332
existingSite.setDomainNames(new HashSet<>(normalizedDomainNames));
320333

321-
final List<Site> sites = this.siteProvider.getAllSites()
322-
.stream().sorted(Comparator.comparingInt(Site::getId))
323-
.collect(Collectors.toList());
334+
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
335+
} catch (Exception e) {
336+
ResponseUtil.errorInternal(rc, "set site domain_names failed", e);
337+
}
338+
}
324339

325-
storeWriter.upload(sites, null);
340+
private void handleSiteAppNames(RoutingContext rc) {
341+
try {
342+
// refresh manually
343+
siteProvider.loadContent();
326344

327-
rc.response().end(jsonWriter.writeValueAsString(existingSite));
345+
final Site existingSite = RequestUtil.getSiteFromParam(rc, "id", siteProvider);
346+
if (existingSite == null) {
347+
return;
348+
}
349+
350+
JsonObject body = rc.body().asJsonObject();
351+
JsonArray appNamesJa = body.getJsonArray("app_names");
352+
if (appNamesJa == null) {
353+
ResponseUtil.error(rc, 400, "required parameters: app_names");
354+
return;
355+
}
356+
Set<String> normalizedAppNames = getNormalizedAppNames(rc, appNamesJa);
357+
if (normalizedAppNames == null) return;
358+
359+
existingSite.setAppNames(normalizedAppNames);
360+
361+
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
328362
} catch (Exception e) {
329-
ResponseUtil.errorInternal(rc, "set site domain_names failed", e);
363+
ResponseUtil.errorInternal(rc, "set site app_names failed", e);
330364
}
331365
}
332366

@@ -355,13 +389,7 @@ private void handleSiteUpdate(RoutingContext rc) {
355389
}
356390
}
357391

358-
final List<Site> sites = this.siteProvider.getAllSites()
359-
.stream().sorted(Comparator.comparingInt(Site::getId))
360-
.collect(Collectors.toList());
361-
362-
storeWriter.upload(sites, null);
363-
364-
rc.response().end(jsonWriter.writeValueAsString(existingSite));
392+
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
365393
} catch (Exception e) {
366394
rc.fail(500, e);
367395
}
@@ -389,6 +417,17 @@ private static List<String> getNormalizedDomainNames(RoutingContext rc, JsonArra
389417
return normalizedDomainNames;
390418
}
391419

420+
private static Set<String> getNormalizedAppNames(RoutingContext rc, JsonArray appNamesJa) {
421+
List<String> appNames = appNamesJa.stream().map(String::valueOf).collect(Collectors.toList());
422+
423+
boolean containsDuplicates = appNames.stream().distinct().count() < appNames.size();
424+
if (containsDuplicates) {
425+
ResponseUtil.error(rc, 400, "duplicate app_names not permitted");
426+
return null;
427+
}
428+
return new HashSet<>(appNames);
429+
}
430+
392431
public static String getTopLevelDomainName(String origin) throws MalformedURLException {
393432
String host;
394433
try {
@@ -409,4 +448,13 @@ public static String getTopLevelDomainName(String origin) throws MalformedURLExc
409448
}
410449
throw new MalformedURLException();
411450
}
451+
452+
private void uploadSiteToStoreWriterAndWriteExistingSiteToResponse(Site existingSite, RoutingContext rc) throws Exception {
453+
final List<Site> sites = this.siteProvider.getAllSites()
454+
.stream().sorted(Comparator.comparingInt(Site::getId))
455+
.collect(Collectors.toList());
456+
457+
storeWriter.upload(sites, null);
458+
rc.response().end(jsonWriter.writeValueAsString(existingSite));
459+
}
412460
}

src/main/resources/localstack/s3/core/sites/sites.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"enabled": true,
2121
"clientTypes": ["PUBLISHER"],
2222
"visible": true,
23-
"domain_names": ["example.com"]
23+
"domain_names": ["example.com"],
24+
"app_names": ["com.123.Game.App.android", "123456789", "com.123.Game.App.ios"]
2425
},
2526
{
2627
"id": 125,

0 commit comments

Comments
 (0)