Make User customizable

This commit is contained in:
jeffcheasey88 2023-07-26 16:01:07 +02:00
parent bd89572dc5
commit fb442f075b
3 changed files with 18 additions and 30 deletions

View file

@ -3,19 +3,13 @@ package be.jeffcheasey88.peeratcode.framework;
import java.net.Socket;
import java.util.Arrays;
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
public class Client extends Thread{
public class Client<U extends User> extends Thread{
private HttpReader reader;
private HttpWriter writer;
private Router router;
private Router<U> router;
public Client(Socket socket, Router router) throws Exception{
public Client(Socket socket, Router<U> router) throws Exception{
this.reader = new HttpReader(socket);
this.writer = new HttpWriter(socket);
this.router = router;
@ -41,8 +35,7 @@ public class Client extends Thread{
if(auth == null) return null;
auth = auth.substring(7);
try{
JwtClaims jwtClaims = this.router.getJwtConsumer().processToClaims(auth);
return new User(jwtClaims);
return this.router.getUser(auth);
}catch(Exception e){
writer.response(401, "Access-Control-Allow-Origin: *");
writer.flush();

View file

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -24,7 +25,7 @@ import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;
public class Router{
public class Router<U extends User>{
private Map<RequestType, Map<Response, Route>> responses;
private Map<Response, Pattern> patterns;
@ -32,16 +33,16 @@ public class Router{
private RsaJsonWebKey rsaJsonWebKey;
private JwtConsumer jwtConsumer;
private Consumer<JwtClaims> claims;
private Function<JwtClaims, U> userCreator;
public Router() throws Exception{
this.responses = new HashMap<>();
for(RequestType type : RequestType.values()) this.responses.put(type, new HashMap<>());
this.patterns = new HashMap<>();
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
}
public Router configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims){
public Router<U> configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator){
JwtConsumerBuilder builder = new JwtConsumerBuilder()
.setRequireExpirationTime()
.setAllowedClockSkewInSeconds(30)
@ -52,6 +53,7 @@ public class Router{
this.jwtConsumer = builder.build();
this.claims = claims;
this.userCreator = userCreator;
return this;
}
@ -64,7 +66,7 @@ public class Router{
while (!server.isClosed()) {
Socket socket = server.accept();
Client client = new Client(socket, this);
Client<U> client = new Client<>(socket, this);
client.start();
}
} catch (Exception e) {
@ -78,7 +80,7 @@ public class Router{
try (ServerSocket server = new ServerSocket(port)) {
while (!server.isClosed()) {
Socket socket = server.accept();
Client client = new Client(socket, this);
Client<U> client = new Client<>(socket, this);
client.start();
}
} catch (Exception e) {
@ -130,18 +132,18 @@ public class Router{
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
}
public JwtConsumer getJwtConsumer(){
return this.jwtConsumer;
public U getUser(String token) throws Exception{
return this.userCreator.apply(this.jwtConsumer.processToClaims(token));
}
public String createAuthUser(int id) throws JoseException{
public String createAuthUser(U user) throws JoseException{
JwtClaims claims = new JwtClaims();
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);
claims.setClaim("id", id);
user.write(claims);
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());

View file

@ -2,15 +2,8 @@ package be.jeffcheasey88.peeratcode.framework;
import org.jose4j.jwt.JwtClaims;
public class User{
private int id;
public User(JwtClaims jwtClaims){
this.id = ((Long) jwtClaims.getClaimValue("id")).intValue();
}
public abstract class User{
public abstract void write(JwtClaims claims);
public int getId(){
return this.id;
}
}