JWT -> import & export private key
This commit is contained in:
parent
c2e9e9e5bf
commit
600de755ca
1 changed files with 31 additions and 14 deletions
|
@ -3,8 +3,10 @@ package dev.peerat.framework;
|
|||
import java.lang.reflect.Method;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.security.Key;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -15,6 +17,7 @@ import javax.net.ssl.SSLServerSocketFactory;
|
|||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||
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;
|
||||
|
@ -41,22 +44,33 @@ public class Router<U extends User>{
|
|||
private String[][] headers;
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
public Router() throws Exception{
|
||||
public Router(){
|
||||
this.logger = new Locker<>();
|
||||
this.exceptions = new Locker<>();
|
||||
int types = RequestType.values().length;
|
||||
this.mappers = new RouteMapper[types];
|
||||
this.interceptors = new ArrayList<>();
|
||||
for(RequestType type : RequestType.values()) this.mappers[type.ordinal()] = new RouteMapper<>(this);
|
||||
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||
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()
|
||||
.setRequireExpirationTime()
|
||||
.setAllowedClockSkewInSeconds(30)
|
||||
.setVerificationKey(rsaJsonWebKey.getKey())
|
||||
.setVerificationKey(key)
|
||||
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256);
|
||||
|
||||
consumer.accept(builder);
|
||||
|
@ -64,7 +78,10 @@ public class Router<U extends User>{
|
|||
this.jwtConsumer = builder.build();
|
||||
this.claims = claims;
|
||||
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){
|
||||
|
|
Loading…
Add table
Reference in a new issue