From 838d5eccc299283ffa153d29501232e876b48040 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Wed, 25 Oct 2023 13:25:17 +1100 Subject: [PATCH] Add duplication check for keyid --- .../admin/vertx/service/AdminKeyService.java | 21 ++++++++++++++++-- .../admin/vertx/service/ClientKeyService.java | 22 +++++++++++++++++-- .../vertx/service/OperatorKeyService.java | 20 +++++++++++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/uid2/admin/vertx/service/AdminKeyService.java b/src/main/java/com/uid2/admin/vertx/service/AdminKeyService.java index d8302538..0ba86bae 100644 --- a/src/main/java/com/uid2/admin/vertx/service/AdminKeyService.java +++ b/src/main/java/com/uid2/admin/vertx/service/AdminKeyService.java @@ -1,6 +1,7 @@ package com.uid2.admin.vertx.service; import com.fasterxml.jackson.databind.ObjectWriter; +import com.google.cloud.Tuple; import com.uid2.admin.auth.AdminUser; import com.uid2.admin.auth.AdminUserProvider; import com.uid2.shared.secret.IKeyGenerator; @@ -165,6 +166,20 @@ private void handleAdminReveal(RoutingContext rc) { } } + private String[] generateRandomKey() throws Exception { + String key = (this.adminKeyPrefix != null ? this.adminKeyPrefix : "") + keyGenerator.generateFormattedKeyString(32); + String keyId = key.substring(0, "UID2-A-L-".length() + 5); + + // Check if keyId is duplicated + Optional existingAdminKeyId = this.adminUserProvider.getAll() + .stream().filter(a -> a.getKeyId().equals(keyId)) + .findFirst(); + if (existingAdminKeyId.isPresent()) { + return generateRandomKey(); + } + return new String[]{ key, keyId }; + } + private void handleAdminAdd(RoutingContext rc) { try { // refresh manually @@ -195,8 +210,10 @@ private void handleAdminAdd(RoutingContext rc) { .collect(Collectors.toList()); // create a random key - String key = (this.adminKeyPrefix != null ? this.adminKeyPrefix : "") + keyGenerator.generateFormattedKeyString(32); - String keyId = key.substring(0, "UID2-A-L-".length() + 5); + String[] randomKeySet = generateRandomKey(); + String key = randomKeySet[0]; + String keyId = randomKeySet[1]; + KeyHashResult khr = keyHasher.hashKey(key); // create new admin diff --git a/src/main/java/com/uid2/admin/vertx/service/ClientKeyService.java b/src/main/java/com/uid2/admin/vertx/service/ClientKeyService.java index b587d6c9..622bfa62 100644 --- a/src/main/java/com/uid2/admin/vertx/service/ClientKeyService.java +++ b/src/main/java/com/uid2/admin/vertx/service/ClientKeyService.java @@ -1,6 +1,7 @@ package com.uid2.admin.vertx.service; import com.fasterxml.jackson.databind.ObjectWriter; +import com.uid2.admin.auth.AdminUser; import com.uid2.admin.auth.RevealedKey; import com.uid2.admin.legacy.LegacyClientKey; import com.uid2.admin.legacy.LegacyClientKeyStoreWriter; @@ -184,6 +185,20 @@ private void handleClientReveal(RoutingContext rc) { } } + private String[] generateRandomKey(Site site) throws Exception { + String key = (this.clientKeyPrefix != null ? (this.clientKeyPrefix + site.getId() + "-") : "") + keyGenerator.generateFormattedKeyString(32); + String keyId = key.substring(0, String.format("UID2-C-L-%d-", site.getId()).length() + 5); + + // Check if keyId is duplicated + Optional existingClientKeyId = this.clientKeyProvider.getAll() + .stream().filter(c -> c.getKeyId().equals(keyId)) + .findFirst(); + if (existingClientKeyId.isPresent()) { + return generateRandomKey(site); + } + return new String[]{ key, keyId }; + } + private void handleClientAdd(RoutingContext rc) { try { // refresh manually @@ -212,11 +227,14 @@ private void handleClientAdd(RoutingContext rc) { List clients = getAllClientKeys(); // create random key and secret - String key = (this.clientKeyPrefix != null ? (this.clientKeyPrefix + site.getId() + "-") : "") + keyGenerator.generateFormattedKeyString(32); + String[] randomKeySet = generateRandomKey(site); + String key = randomKeySet[0]; + String keyId = randomKeySet[1]; + KeyHashResult khr = keyHasher.hashKey(key); String secret = keyGenerator.generateRandomKeyString(32); - String keyId = key.substring(0, String.format("UID2-C-L-%d-", site.getId()).length() + 5); + // add new client to array Instant created = Instant.now(); diff --git a/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java b/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java index d1ea7e3f..60a318ff 100644 --- a/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java +++ b/src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.uid2.admin.auth.RevealedKey; +import com.uid2.admin.legacy.LegacyClientKey; import com.uid2.shared.model.Site; import com.uid2.shared.secret.IKeyGenerator; import com.uid2.admin.store.writer.OperatorKeyStoreWriter; @@ -166,6 +167,20 @@ private void handleOperatorReveal(RoutingContext rc) { } } + private String[] generateRandomKey(Integer finalSiteId) throws Exception { + String key = (this.operatorKeyPrefix != null ? (this.operatorKeyPrefix + finalSiteId + "-") : "") + keyGenerator.generateFormattedKeyString(32); + String keyId = key.substring(0, String.format("UID2-O-L-%d-", finalSiteId).length() + 5); + + // Check if keyId is duplicated + Optional existingOperatorKeyId = this.operatorKeyProvider.getAll() + .stream().filter(o -> o.getKeyId().equals(keyId)) + .findFirst(); + if (existingOperatorKeyId.isPresent()) { + return generateRandomKey(finalSiteId); + } + return new String[]{ key, keyId }; + } + private void handleOperatorAdd(RoutingContext rc) { try { // refresh manually @@ -235,8 +250,9 @@ private void handleOperatorAdd(RoutingContext rc) { .collect(Collectors.toList()); // create a random key - String key = (this.operatorKeyPrefix != null ? (this.operatorKeyPrefix + finalSiteId + "-") : "") + keyGenerator.generateFormattedKeyString(32); - String keyId = key.substring(0, String.format("UID2-O-L-%d-", siteId).length() + 5); + String[] randomKeySet = generateRandomKey(finalSiteId); + String key = randomKeySet[0]; + String keyId = randomKeySet[1]; KeyHashResult khr = keyHasher.hashKey(key); // create new operator