JWT -> import & export private key

This commit is contained in:
jeffcheasey88 2024-04-16 11:51:22 +02:00
parent c2e9e9e5bf
commit 600de755ca

View file

@ -3,8 +3,10 @@ package dev.peerat.framework;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.security.Key;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -15,6 +17,7 @@ import javax.net.ssl.SSLServerSocketFactory;
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType; import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
import org.jose4j.jwk.RsaJsonWebKey; import org.jose4j.jwk.RsaJsonWebKey;
import org.jose4j.jwk.RsaJwkGenerator; import org.jose4j.jwk.RsaJwkGenerator;
import org.jose4j.jwk.JsonWebKey.OutputControlLevel;
import org.jose4j.jws.AlgorithmIdentifiers; import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature; import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims; import org.jose4j.jwt.JwtClaims;
@ -41,22 +44,33 @@ public class Router<U extends User>{
private String[][] headers; private String[][] headers;
private ServerSocket serverSocket; private ServerSocket serverSocket;
public Router() throws Exception{ public Router(){
this.logger = new Locker<>(); this.logger = new Locker<>();
this.exceptions = new Locker<>(); this.exceptions = new Locker<>();
int types = RequestType.values().length; int types = RequestType.values().length;
this.mappers = new RouteMapper[types]; this.mappers = new RouteMapper[types];
this.interceptors = new ArrayList<>(); this.interceptors = new ArrayList<>();
for(RequestType type : RequestType.values()) this.mappers[type.ordinal()] = new RouteMapper<>(this); for(RequestType type : RequestType.values()) this.mappers[type.ordinal()] = new RouteMapper<>(this);
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
this.headers = new String[types][0]; this.headers = new String[types][0];
} }
public Router<U> configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator){ public Router<U> configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator) throws Exception{
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
configureJwtWithKey(consumer, claims, userCreator, this.rsaJsonWebKey.getKey());
return this;
}
public Router<U> configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator, Map<String, Object> keyParams) throws Exception{
this.rsaJsonWebKey = new RsaJsonWebKey(keyParams);
configureJwtWithKey(consumer, claims, userCreator, this.rsaJsonWebKey.getKey());
return this;
}
private void configureJwtWithKey(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator, Key key) throws Exception{
JwtConsumerBuilder builder = new JwtConsumerBuilder() JwtConsumerBuilder builder = new JwtConsumerBuilder()
.setRequireExpirationTime() .setRequireExpirationTime()
.setAllowedClockSkewInSeconds(30) .setAllowedClockSkewInSeconds(30)
.setVerificationKey(rsaJsonWebKey.getKey()) .setVerificationKey(key)
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256); .setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256);
consumer.accept(builder); consumer.accept(builder);
@ -64,7 +78,10 @@ public class Router<U extends User>{
this.jwtConsumer = builder.build(); this.jwtConsumer = builder.build();
this.claims = claims; this.claims = claims;
this.userCreator = userCreator; this.userCreator = userCreator;
return this; }
public Map<String, Object> exportJwtKey(){
return this.rsaJsonWebKey.toParams(OutputControlLevel.INCLUDE_PRIVATE);
} }
public Router<U> addDefaultHeaders(RequestType type, String... headers){ public Router<U> addDefaultHeaders(RequestType type, String... headers){