Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to diable CSTG key #559

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public enum ResponseStatus {
PayloadHasNoBody,
/* End of CSTG-related Status */
Unknown,
NoActiveKey
NoActiveKey,
UNAUTHORIZED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The others are PascalCase

}

public static void record(ISiteStore siteStore, Integer siteId, Endpoint endpoint, TokenVersion advertisingTokenVersion, ResponseStatus responseStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ private void handleClientSideTokenGenerateImpl(RoutingContext rc) throws NoSuchA
return;
}

if(clientSideKeypair.isDisabled()) {
SendClientErrorResponseAndRecordStats(ResponseStatus.Unauthorized, 401, rc, "Unauthorized",
null, TokenResponseStatsCollector.Endpoint.ClientSideTokenGenerateV2, TokenResponseStatsCollector.ResponseStatus.UNAUTHORIZED, siteProvider);
return;
}

if (!hasValidOriginOrAppName(rc, request, clientSideKeypair)) {
return;
}
Expand Down
50 changes: 49 additions & 1 deletion src/test/java/com/uid2/operator/UIDOperatorVerticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -3094,6 +3093,55 @@ void cstgLogsInvalidAppName(String appName, Vertx vertx, VertxTestContext testCo
});
}

@Test
void catalogsDisabledAsUnauthorized(Vertx vertx, VertxTestContext testContext) throws NoSuchAlgorithmException, InvalidKeyException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be prefixed with "cstg" to make the tests easier to search and group?

ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(UIDOperatorVerticle.class)).addAppender(logWatcher);
this.uidOperatorVerticle.setLastInvalidOriginProcessTime(Instant.now().minusSeconds(3600));

setupCstgBackend();
String subscriptionID = "PpRrE5YY84";
ClientSideKeypair keypairDisabled = new ClientSideKeypair(subscriptionID, clientSideTokenGeneratePublicKey, clientSideTokenGeneratePrivateKey, clientSideTokenGenerateSiteId, "", Instant.now(), true, "");
when(clientSideKeypairProvider.getSnapshot()).thenReturn(clientSideKeypairSnapshot);
when(clientSideKeypairSnapshot.getKeypair(subscriptionID)).thenReturn(keypairDisabled);

final KeyFactory kf = KeyFactory.getInstance("EC");
final PublicKey serverPublicKey = ClientSideTokenGenerateTestUtil.stringToPublicKey(clientSideTokenGeneratePublicKey, kf);
final PrivateKey clientPrivateKey = ClientSideTokenGenerateTestUtil.stringToPrivateKey("MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCDsqxZicsGytVqN2HZqNDHtV422Lxio8m1vlflq4Jb47Q==", kf);
final SecretKey secretKey = ClientSideTokenGenerateTestUtil.deriveKey(serverPublicKey, clientPrivateKey);

final byte[] iv = Random.getBytes(12);
final long timestamp = Instant.now().toEpochMilli();
final JsonArray aad = JsonArray.of(timestamp);
String rawId = "[email protected]";

JsonObject identityPayload = new JsonObject();
identityPayload.put("email_hash", getSha256(rawId));
byte[] payloadBytes = ClientSideTokenGenerateTestUtil.encrypt(identityPayload.toString().getBytes(), secretKey.getEncoded(), iv, aad.toBuffer().getBytes());
final String payload = EncodingUtils.toBase64String(payloadBytes);

JsonObject requestJson = new JsonObject();
requestJson.put("payload", payload);
requestJson.put("iv", EncodingUtils.toBase64String(iv));
requestJson.put("public_key", serverPublicKey.toString());
requestJson.put("timestamp", timestamp);
requestJson.put("subscription_id", subscriptionID);

Tuple.Tuple2<JsonObject, SecretKey> data = createClientSideTokenGenerateRequest(IdentityType.Email, "[email protected]", Instant.now().toEpochMilli(), false, null);
sendCstg(vertx,
"v2/token/client-generate",
null,
requestJson,
secretKey,
401,
testContext,
respJson -> {
assertEquals("Unauthorized", respJson.getString("message"));
testContext.completeNow();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can also assertTokenStatusMetrics here

}

@ParameterizedTest
@CsvSource({
"true,http://gototest.com",
Expand Down