Skip to content

Commit

Permalink
Remore Code Refactor in RotatingClientKeyProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
zaiweidu committed Sep 19, 2023
1 parent 1858468 commit d434c01
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import com.uid2.shared.auth.IAuthorizable;
import com.uid2.shared.cloud.DownloadCloudStorage;
import com.uid2.shared.store.CloudPath;
import com.uid2.shared.store.ScopedStoreReader;
import com.uid2.shared.store.IClientKeyProvider;
import com.uid2.shared.store.ScopedStoreReader;
import com.uid2.shared.store.parser.ClientParser;
import com.uid2.shared.store.scope.StoreScope;
import io.vertx.core.json.JsonObject;

import java.util.Collection;
import java.util.List;
import java.util.Comparator;

/*
1. metadata.json format
Expand Down Expand Up @@ -93,4 +93,12 @@ public CloudPath getMetadataPath() {
public IAuthorizable get(String key) {
return getClientKey(key);
}

public ClientKey getOldestClientKey(int siteId) {
return this.reader.getSnapshot().stream()
.filter(k -> k.getSiteId() == siteId) // filter by site id
.sorted(Comparator.comparing(ClientKey::getCreated)) // sort by key creation timestamp ascending
.findFirst() // return the oldest key
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.uid2.shared.auth;

import com.uid2.shared.cloud.EmbeddedResourceStorage;
import com.uid2.shared.store.CloudPath;
import com.uid2.shared.store.reader.RotatingClientKeyProvider;
import com.uid2.shared.store.scope.GlobalScope;
import io.vertx.core.json.JsonObject;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;

public class RotatingClientKeyProviderTest {
@Test
public void testGetOldestClientKeyBySiteId() throws Exception {
RotatingClientKeyProvider provider = new RotatingClientKeyProvider(
new EmbeddedResourceStorage(RotatingClientKeyProviderTest.class),
new GlobalScope(new CloudPath("/com.uid2.shared/test/clients/metadata.json")));

JsonObject metadata = provider.getMetadata();
provider.loadContent(metadata);
assertEquals(1, provider.getVersion(metadata));

// a site that has multiple client keys
ClientKey expected = provider.getClientKey("trusted-partner-key");
assertNotNull(expected);
ClientKey actual = provider.getOldestClientKey(expected.getSiteId());
assertNotNull(actual);
assertThat(actual).isEqualTo(expected);

// a site that doesn't have client keys
actual = provider.getOldestClientKey(1234567890);
assertNull(actual);
}
}
22 changes: 22 additions & 0 deletions src/test/resources/com.uid2.shared/test/clients/clients.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,27 @@
"created": 1617149276,
"roles": [ "ID_READER", "MAPPER", "GENERATOR" ],
"site_id": 6
},
{
"key": "trusted-partner-key-latest1",
"key_hash": "JgdHSbec8S5qhVQeCwy0FVEwYknBVtccxccHHUobRFYVvzTmj0i2EQMhZ6BCBzSpI7I3Vd1RxMd3P2IOLGt+tA==",
"key_salt": "RSn7ZU+fjFtku6zDKJ1cpmeWqqxihF1FmrPRv4+78+M=",
"secret": "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=",
"name": "[email protected]",
"contact": "[email protected]",
"created": 1654596485,
"roles": [ "ID_READER", "MAPPER", "GENERATOR" ],
"site_id": 6
},
{
"key": "trusted-partner-key-latest2",
"key_hash": "JgdHSbec8S6qhVQeCwy0FVEwYknBVtccxccHHUobRFYVvzTmj0i2EQMhZ6BCBzSpI7I3Vd1RxMd3P2IOLGt+tA==",
"key_salt": "RSn7ZU+fjFtku6zDKJ1cpmeWqqxihF1FmrPRv4+78+M=",
"secret": "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=",
"name": "[email protected]",
"contact": "[email protected]",
"created": 1694596485,
"roles": [ "ID_READER", "MAPPER", "GENERATOR" ],
"site_id": 6
}
]

0 comments on commit d434c01

Please sign in to comment.