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.net.Socket;
import java.util.Arrays; import java.util.Arrays;
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType; public class Client<U extends User> extends Thread{
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{
private HttpReader reader; private HttpReader reader;
private HttpWriter writer; 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.reader = new HttpReader(socket);
this.writer = new HttpWriter(socket); this.writer = new HttpWriter(socket);
this.router = router; this.router = router;
@ -41,8 +35,7 @@ public class Client extends Thread{
if(auth == null) return null; if(auth == null) return null;
auth = auth.substring(7); auth = auth.substring(7);
try{ try{
JwtClaims jwtClaims = this.router.getJwtConsumer().processToClaims(auth); return this.router.getUser(auth);
return new User(jwtClaims);
}catch(Exception e){ }catch(Exception e){
writer.response(401, "Access-Control-Allow-Origin: *"); writer.response(401, "Access-Control-Allow-Origin: *");
writer.flush(); writer.flush();

View file

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

View file

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