Skip to content

Commit

Permalink
Add Token Encoder Interface (#529)
Browse files Browse the repository at this point in the history
* extract interface

* adhere to naming convention

---------

Co-authored-by: Tobias Koch <[email protected]>
Co-authored-by: Steffengreiner <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 3db69f8 commit 67ee7d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <b>Password encryption policy</b>
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class PersonalAccessToken {
private Instant creationDate;
private Duration duration;

private static final TokenEncoder TOKEN_ENCODER = PasswordEncryptionPolicy.instance();

protected PersonalAccessToken() {
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 67ee7d2

Please sign in to comment.