Skip to content

Commit

Permalink
Add duplication check for keyid
Browse files Browse the repository at this point in the history
  • Loading branch information
cYKatherine committed Oct 25, 2023
1 parent 65c656e commit 838d5ec
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/main/java/com/uid2/admin/vertx/service/AdminKeyService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<AdminUser> 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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/uid2/admin/vertx/service/ClientKeyService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LegacyClientKey> 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
Expand Down Expand Up @@ -212,11 +227,14 @@ private void handleClientAdd(RoutingContext rc) {
List<LegacyClientKey> 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();
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/uid2/admin/vertx/service/OperatorKeyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<OperatorKey> 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 838d5ec

Please sign in to comment.