package be.jeffcheasey88.peeratcode.webserver; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import org.jose4j.jwk.RsaJsonWebKey; import org.jose4j.jwk.RsaJwkGenerator; import org.jose4j.jws.AlgorithmIdentifiers; import org.jose4j.jws.JsonWebSignature; import org.jose4j.jwt.JwtClaims; import org.jose4j.lang.JoseException; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; public class Router{ private List responses; private Response noFileFound; private RsaJsonWebKey rsaJsonWebKey; private DatabaseRepository repo; public Router(DatabaseRepository repo) throws Exception{ this.repo = repo; this.responses = new ArrayList<>(); this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048); } public DatabaseRepository getDataBase(){ return this.repo; } public void register(Response response){ this.responses.add(response); } public void setDefault(Response response){ this.noFileFound = response; } public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception { for(Response response : this.responses){ if(type.equals(response.getType())){ Matcher matcher = response.getPattern().matcher(path); if(matcher.matches()){ response.exec(matcher, user, reader, writer); return; } } } if(noFileFound != null) noFileFound.exec(null, user, reader, writer); } public RsaJsonWebKey getWebKey(){ return this.rsaJsonWebKey; } public String createAuthUser(int id) throws JoseException{ JwtClaims claims = new JwtClaims(); claims.setIssuer("Issuer"); // who creates the token and signs it claims.setAudience("Audience"); // to whom the token is intended to be sent claims.setExpirationTimeMinutesInTheFuture(10); // time when the token will expire (10 minutes from now) 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) claims.setClaim("id", id); JsonWebSignature jws = new JsonWebSignature(); jws.setPayload(claims.toJson()); jws.setKey(rsaJsonWebKey.getPrivateKey()); jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId()); jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); return jws.getCompactSerialization(); } }