From 7412902fd43f6931b808e2ac9471f88145fb261c Mon Sep 17 00:00:00 2001 From: Christophe Maillard Date: Tue, 21 May 2024 15:36:09 +0200 Subject: [PATCH 1/2] feat(encryption): allow custom loading of JWK sets --- .../clientsdk/util/HyperwalletEncryption.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java b/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java index 732b3d2c6..d5c953c77 100644 --- a/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java +++ b/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java @@ -106,8 +106,8 @@ public HyperwalletEncryption(JWEAlgorithm encryptionAlgorithm, JWSAlgorithm sign public String encrypt(String body) throws JOSEException, IOException, ParseException { - JWK clientPrivateKey = getKeyByAlgorithm(loadKeySet(clientPrivateKeySetLocation), signAlgorithm); - JWK hyperwalletPublicKey = getKeyByAlgorithm(loadKeySet(hyperwalletKeySetLocation), encryptionAlgorithm); + JWK clientPrivateKey = getKeyByAlgorithm(loadClientPrivateKeySet(), signAlgorithm); + JWK hyperwalletPublicKey = getKeyByAlgorithm(loadHyperwalletKeySet(), encryptionAlgorithm); JWSSigner jwsSigner = getJWSSigner(clientPrivateKey); JWEEncrypter jweEncrypter = getJWEEncrypter(hyperwalletPublicKey); @@ -132,8 +132,8 @@ public String encrypt(String body) throws JOSEException, IOException, ParseExcep public String decrypt(String body) throws ParseException, IOException, JOSEException { - JWK privateKeyToDecrypt = getKeyByAlgorithm(loadKeySet(clientPrivateKeySetLocation), encryptionAlgorithm); - JWK publicKeyToSign = getKeyByAlgorithm(loadKeySet(hyperwalletKeySetLocation), signAlgorithm); + JWK privateKeyToDecrypt = getKeyByAlgorithm(loadClientPrivateKeySet(), encryptionAlgorithm); + JWK publicKeyToSign = getKeyByAlgorithm(loadHyperwalletKeySet(), signAlgorithm); JWEDecrypter jweDecrypter = getJWEDecrypter(privateKeyToDecrypt); JWSVerifier jwsVerifier = getJWSVerifier(publicKeyToSign); @@ -148,6 +148,14 @@ public String decrypt(String body) throws ParseException, IOException, JOSEExcep return jwsObject.getPayload().toString(); } + protected JWKSet loadClientPrivateKeySet() throws IOException, ParseException { + return loadKeySet(clientPrivateKeySetLocation); + } + + protected JWKSet loadHyperwalletKeySet() throws IOException, ParseException { + return loadKeySet(hyperwalletKeySetLocation); + } + public void verifySignatureExpirationDate(Object signatureExpirationDate) { if (signatureExpirationDate == null) { throw new HyperwalletException("exp JWS header param was null"); From e9c69190e07a03ef4d96d1dd86fe2eea72ed1e1c Mon Sep 17 00:00:00 2001 From: Christophe Maillard Date: Wed, 22 May 2024 11:04:43 +0200 Subject: [PATCH 2/2] docs --- .../hyperwallet/clientsdk/util/HyperwalletEncryption.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java b/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java index d5c953c77..98cd0bb65 100644 --- a/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java +++ b/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletEncryption.java @@ -148,10 +148,16 @@ public String decrypt(String body) throws ParseException, IOException, JOSEExcep return jwsObject.getPayload().toString(); } + /** + * Allows clients to implement a custom loading of their private JWK set. + */ protected JWKSet loadClientPrivateKeySet() throws IOException, ParseException { return loadKeySet(clientPrivateKeySetLocation); } + /** + * Allows clients to implement a custom loading of Hyperwallet public JWK set. + */ protected JWKSet loadHyperwalletKeySet() throws IOException, ParseException { return loadKeySet(hyperwalletKeySetLocation); } @@ -397,4 +403,4 @@ public HyperwalletEncryption build() { return hyperwalletEncryption; } } -} \ No newline at end of file +}