Skip to content

Commit

Permalink
Merge pull request #264 from IABTechLab/ccm-UID2-2830-add-site-new-ap…
Browse files Browse the repository at this point in the history
…p-names

UID2-2830 add site new app names
  • Loading branch information
caroline-ttd authored Apr 22, 2024
2 parents e9aee6e + 3b22fed commit e28cee9
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ and re-initialize your localstack by running `docker-compose restart`.

### Authentication and Authorization

When running locally, Okta OAuth is disabled and users are logged in as *[email protected]* via the
`is_auth_disabled` flag. The user has all the rights available.
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.

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.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- check micrometer.version vertx-micrometer-metrics consumes before bumping up -->
<micrometer.version>1.1.0</micrometer.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<uid2-shared.version>7.7.6-1e644a0ded</uid2-shared.version>
<uid2-shared.version>7.9.0</uid2-shared.version>
<okta-jwt.version>0.5.8</okta-jwt.version>
<image.version>${project.version}</image.version>
</properties>
Expand Down
90 changes: 69 additions & 21 deletions src/main/java/com/uid2/admin/vertx/service/SiteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uid2.admin.vertx.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.uid2.admin.auth.AdminAuthMiddleware;
import com.uid2.admin.legacy.ILegacyClientKeyProvider;
Expand Down Expand Up @@ -83,6 +84,11 @@ public void setupRoutes(Router router) {
this.handleSiteDomains(ctx);
}
}, Role.MAINTAINER, Role.SHARING_PORTAL));
router.post("/api/site/app_names").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteAppNames(ctx);
}
}, Role.MAINTAINER, Role.SHARING_PORTAL));
router.post("/api/site/update").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteUpdate(ctx);
Expand Down Expand Up @@ -127,12 +133,16 @@ private static JsonObject createSiteJsonObject(Site site, Map<Integer, List<Lega
JsonArray domainNamesJa = new JsonArray();
site.getDomainNames().forEach(domainNamesJa::add);

JsonArray appNamesJa = new JsonArray();
site.getAppNames().forEach(appNamesJa::add);

jo.put("id", site.getId());
jo.put("name", site.getName());
jo.put("description", site.getDescription());
jo.put("enabled", site.isEnabled());
jo.put("clientTypes", site.getClientTypes());
jo.put("domain_names", domainNamesJa);
jo.put("app_names", appNamesJa);
jo.put("visible", site.isVisible());
jo.put("created", site.getCreated());

Expand Down Expand Up @@ -197,6 +207,15 @@ private void handleSiteAdd(RoutingContext rc) {
}
}

Set<String> normalizedAppNames = new HashSet<>();
if (body != null) {
JsonArray appNamesJa = body.getJsonArray("app_names");
if (appNamesJa != null) {
normalizedAppNames = getNormalizedAppNames(rc, appNamesJa);
if (normalizedAppNames == null) return;
}
}

boolean enabled = false;
List<String> enabledFlags = rc.queryParam("enabled");
if (!enabledFlags.isEmpty()) {
Expand All @@ -220,7 +239,7 @@ private void handleSiteAdd(RoutingContext rc) {
.collect(Collectors.toList());
final int siteId = 1 + sites.stream().mapToInt(Site::getId).max().orElse(Const.Data.AdvertisingTokenSiteId);

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

Expand All @@ -247,15 +266,9 @@ private void handleSiteTypesSet(RoutingContext rc) {
return;
}

final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());

existingSite.setClientTypes(types);

storeWriter.upload(sites, null);

rc.response().end(jsonWriter.writeValueAsString(existingSite));
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
} catch (Exception e) {
rc.fail(500, e);
}
Expand Down Expand Up @@ -318,15 +331,36 @@ private void handleSiteDomains(RoutingContext rc) {

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

final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
} catch (Exception e) {
ResponseUtil.errorInternal(rc, "set site domain_names failed", e);
}
}

storeWriter.upload(sites, null);
private void handleSiteAppNames(RoutingContext rc) {
try {
// refresh manually
siteProvider.loadContent();

rc.response().end(jsonWriter.writeValueAsString(existingSite));
final Site existingSite = RequestUtil.getSiteFromParam(rc, "id", siteProvider);
if (existingSite == null) {
return;
}

JsonObject body = rc.body().asJsonObject();
JsonArray appNamesJa = body.getJsonArray("app_names");
if (appNamesJa == null) {
ResponseUtil.error(rc, 400, "required parameters: app_names");
return;
}
Set<String> normalizedAppNames = getNormalizedAppNames(rc, appNamesJa);
if (normalizedAppNames == null) return;

existingSite.setAppNames(normalizedAppNames);

uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
} catch (Exception e) {
ResponseUtil.errorInternal(rc, "set site domain_names failed", e);
ResponseUtil.errorInternal(rc, "set site app_names failed", e);
}
}

Expand Down Expand Up @@ -355,13 +389,7 @@ private void handleSiteUpdate(RoutingContext rc) {
}
}

final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());

storeWriter.upload(sites, null);

rc.response().end(jsonWriter.writeValueAsString(existingSite));
uploadSiteToStoreWriterAndWriteExistingSiteToResponse(existingSite, rc);
} catch (Exception e) {
rc.fail(500, e);
}
Expand Down Expand Up @@ -389,6 +417,17 @@ private static List<String> getNormalizedDomainNames(RoutingContext rc, JsonArra
return normalizedDomainNames;
}

private static Set<String> getNormalizedAppNames(RoutingContext rc, JsonArray appNamesJa) {
List<String> appNames = appNamesJa.stream().map(String::valueOf).collect(Collectors.toList());

boolean containsDuplicates = appNames.stream().distinct().count() < appNames.size();
if (containsDuplicates) {
ResponseUtil.error(rc, 400, "duplicate app_names not permitted");
return null;
}
return new HashSet<>(appNames);
}

public static String getTopLevelDomainName(String origin) throws MalformedURLException {
String host;
try {
Expand All @@ -409,4 +448,13 @@ public static String getTopLevelDomainName(String origin) throws MalformedURLExc
}
throw new MalformedURLException();
}

private void uploadSiteToStoreWriterAndWriteExistingSiteToResponse(Site existingSite, RoutingContext rc) throws Exception {
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());

storeWriter.upload(sites, null);
rc.response().end(jsonWriter.writeValueAsString(existingSite));
}
}
3 changes: 2 additions & 1 deletion src/main/resources/localstack/s3/core/sites/sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"enabled": true,
"clientTypes": ["PUBLISHER"],
"visible": true,
"domain_names": ["example.com"]
"domain_names": ["example.com"],
"app_names": ["com.123.Game.App.android", "123456789", "com.123.Game.App.ios"]
},
{
"id": 125,
Expand Down
Loading

0 comments on commit e28cee9

Please sign in to comment.