diff --git a/src/dev/peerat/framework/User.java b/src/dev/peerat/framework/User.java index f9979a0..d273e2c 100644 --- a/src/dev/peerat/framework/User.java +++ b/src/dev/peerat/framework/User.java @@ -1,9 +1,3 @@ package dev.peerat.framework; -import org.jose4j.jwt.JwtClaims; - -public abstract class User{ - - public abstract void write(JwtClaims claims); - -} \ No newline at end of file +public abstract class User{} \ No newline at end of file diff --git a/src/dev/peerat/framework/auth/JwtAuthenticator.java b/src/dev/peerat/framework/auth/JwtAuthenticator.java index 850b66f..fa3a66f 100644 --- a/src/dev/peerat/framework/auth/JwtAuthenticator.java +++ b/src/dev/peerat/framework/auth/JwtAuthenticator.java @@ -2,13 +2,14 @@ package dev.peerat.framework.auth; import java.security.Key; import java.util.Map; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import org.jose4j.jwa.AlgorithmConstraints.ConstraintType; +import org.jose4j.jwk.JsonWebKey.OutputControlLevel; import org.jose4j.jwk.RsaJsonWebKey; import org.jose4j.jwk.RsaJwkGenerator; -import org.jose4j.jwk.JsonWebKey.OutputControlLevel; import org.jose4j.jws.AlgorithmIdentifiers; import org.jose4j.jws.JsonWebSignature; import org.jose4j.jwt.JwtClaims; @@ -23,24 +24,24 @@ public class JwtAuthenticator implements Authenticator{ private RsaJsonWebKey rsaJsonWebKey; private JwtConsumer jwtConsumer; - private Consumer claims; + private BiConsumer claims; private Function userCreator; public JwtAuthenticator(){} - public JwtAuthenticator configure(Consumer consumer, Consumer claims, Function userCreator) throws Exception{ + public JwtAuthenticator configure(Consumer consumer, BiConsumer claims, Function userCreator) throws Exception{ this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048); configureWithKey(consumer, claims, userCreator, this.rsaJsonWebKey.getKey()); return this; } - public JwtAuthenticator configure(Consumer consumer, Consumer claims, Function userCreator, Map keyParams) throws Exception{ + public JwtAuthenticator configure(Consumer consumer, BiConsumer claims, Function userCreator, Map keyParams) throws Exception{ this.rsaJsonWebKey = new RsaJsonWebKey(keyParams); configureWithKey(consumer, claims, userCreator, this.rsaJsonWebKey.getKey()); return this; } - private JwtAuthenticator configureWithKey(Consumer consumer, Consumer claims, Function userCreator, Key key) throws Exception{ + private JwtAuthenticator configureWithKey(Consumer consumer, BiConsumer claims, Function userCreator, Key key) throws Exception{ JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRequireExpirationTime() .setAllowedClockSkewInSeconds(30) @@ -66,9 +67,7 @@ public class JwtAuthenticator implements Authenticator{ claims.setGeneratedJwtId(); // a unique identifier for the token claims.setIssuedAtToNow(); // when the token was issued/created (now) claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago) - this.claims.accept(claims); - - user.write(claims); + this.claims.accept(claims, user); JsonWebSignature jws = new JsonWebSignature(); jws.setPayload(claims.toJson());