From 67ee7d24d0540e0a47705c798b39f395ba05667e Mon Sep 17 00:00:00 2001 From: Tobias Koch Date: Fri, 22 Mar 2024 10:34:55 +0100 Subject: [PATCH] Add Token Encoder Interface (#529) * extract interface * adhere to naming convention --------- Co-authored-by: Tobias Koch Co-authored-by: Steffengreiner --- .../domain/model/PasswordEncryptionPolicy.java | 13 ++++++++++++- .../domain/model/token/PersonalAccessToken.java | 9 +++++---- .../identity/domain/model/token/TokenEncoder.java | 11 +++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 identity/src/main/java/life/qbic/identity/domain/model/token/TokenEncoder.java diff --git a/identity/src/main/java/life/qbic/identity/domain/model/PasswordEncryptionPolicy.java b/identity/src/main/java/life/qbic/identity/domain/model/PasswordEncryptionPolicy.java index cb6a245a0e..329ae64de8 100644 --- a/identity/src/main/java/life/qbic/identity/domain/model/PasswordEncryptionPolicy.java +++ b/identity/src/main/java/life/qbic/identity/domain/model/PasswordEncryptionPolicy.java @@ -8,6 +8,7 @@ import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; +import life.qbic.identity.domain.model.token.TokenEncoder; /** * Password encryption policy @@ -17,7 +18,7 @@ * * @since 1.0.0 */ -public class PasswordEncryptionPolicy { +public class PasswordEncryptionPolicy implements TokenEncoder { private static final int ITERATION_INDEX = 0; // the index of the iteration count in the encoded password String @@ -154,6 +155,16 @@ private static SecretKey createSecretKey(SecretKeyFactory factory, KeySpec keySp } } + @Override + public String encode(char[] token) { + return encrypt(token); + } + + @Override + public boolean matches(char[] token, String encodedToken) { + return doPasswordsMatch(token, encodedToken); + } + static class EncryptionException extends RuntimeException { EncryptionException(String reason, Exception cause) { diff --git a/identity/src/main/java/life/qbic/identity/domain/model/token/PersonalAccessToken.java b/identity/src/main/java/life/qbic/identity/domain/model/token/PersonalAccessToken.java index 0b51995371..9e673fbc86 100644 --- a/identity/src/main/java/life/qbic/identity/domain/model/token/PersonalAccessToken.java +++ b/identity/src/main/java/life/qbic/identity/domain/model/token/PersonalAccessToken.java @@ -38,6 +38,8 @@ public class PersonalAccessToken { private Instant creationDate; private Duration duration; + private static final TokenEncoder TOKEN_ENCODER = PasswordEncryptionPolicy.instance(); + protected PersonalAccessToken() { } @@ -54,9 +56,8 @@ private PersonalAccessToken(String userId, String description, Duration duration public static PersonalAccessToken create(String userId, String description, Duration duration, String secret) { - return new PersonalAccessToken(userId, description, duration, - PasswordEncryptionPolicy.instance().encrypt( - secret.toCharArray())); + return new PersonalAccessToken(userId, description, duration, TOKEN_ENCODER.encode( + secret.toCharArray())); } public String description() { @@ -109,7 +110,7 @@ public int hashCode() { } public boolean matches(String rawToken) { - return PasswordEncryptionPolicy.instance().doPasswordsMatch(rawToken.toCharArray(), this.tokenValueEncrypted); + return TOKEN_ENCODER.matches(rawToken.toCharArray(), this.tokenValueEncrypted); } } diff --git a/identity/src/main/java/life/qbic/identity/domain/model/token/TokenEncoder.java b/identity/src/main/java/life/qbic/identity/domain/model/token/TokenEncoder.java new file mode 100644 index 0000000000..5646d9e930 --- /dev/null +++ b/identity/src/main/java/life/qbic/identity/domain/model/token/TokenEncoder.java @@ -0,0 +1,11 @@ +package life.qbic.identity.domain.model.token; + +/** + * Encodes Access Tokens. + */ +public interface TokenEncoder { + + String encode(char[] token); + + boolean matches(char[] token, String encodedToken); +}