From a319cd7e71b7402267198535c4f46b61c203b450 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Wed, 5 Apr 2023 00:20:53 +0200 Subject: [PATCH] fix: fix getting jwt signing keys with mongodb --- CHANGELOG.md | 4 ++++ .../signingkeys/JWTSigningKey.java | 4 ++-- .../supertokens/test/jwt/JWTCreateTest.java | 22 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e8db456..40d244e1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [unreleased] +### Fixes + +- Fixed creating JWTs using MongoDB if a key already exists + ### Breaking changes - Using an internal `SemVer` class to handle version numbers. This will make handling CDI version ranges easier. diff --git a/src/main/java/io/supertokens/signingkeys/JWTSigningKey.java b/src/main/java/io/supertokens/signingkeys/JWTSigningKey.java index 0091ac47c..bf8db4288 100644 --- a/src/main/java/io/supertokens/signingkeys/JWTSigningKey.java +++ b/src/main/java/io/supertokens/signingkeys/JWTSigningKey.java @@ -214,9 +214,9 @@ public JWTSigningKeyInfo getOrCreateAndGetKeyForAlgorithm(SupportedAlgorithms al // Retry with a new key id } } - - return keyInfo; } + + return keyInfo; } throw new QuitProgramException("Unsupported storage type detected"); diff --git a/src/test/java/io/supertokens/test/jwt/JWTCreateTest.java b/src/test/java/io/supertokens/test/jwt/JWTCreateTest.java index 1adcff9ba..e602b7da6 100644 --- a/src/test/java/io/supertokens/test/jwt/JWTCreateTest.java +++ b/src/test/java/io/supertokens/test/jwt/JWTCreateTest.java @@ -70,6 +70,28 @@ public void testNormalFunctioningOfCreateToken() throws Exception { assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); } + /** + * Call JWTSigningFunctions.createJWTToken with valid params twice and ensure that it does not throw any errors + */ + @Test + public void testCreateTokenWithAlreadyExistingKey() throws Exception { + String[] args = { "../" }; + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); + + String algorithm = "RS256"; + JsonObject payload = new JsonObject(); + payload.addProperty("customClaim", "customValue"); + String jwksDomain = "http://localhost"; + long validity = 3600; + + JWTSigningFunctions.createJWTToken(process.getProcess(), algorithm, payload, jwksDomain, validity, false); + JWTSigningFunctions.createJWTToken(process.getProcess(), algorithm, payload, jwksDomain, validity, false); + + process.kill(); + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); + } + /** * Call JWTSigningFunctions.createJWTToken with valid params and long validity and ensure that it does not throw any * errors