From 7e4cc9f425f79f5a5ce1883f70824fc321f1e4d3 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Fri, 24 Jan 2025 23:15:15 +0100 Subject: [PATCH] Refractor Authenticator -> was visibly not finished previously --- src/dev/peerat/framework/Client.java | 5 +--- src/dev/peerat/framework/Router.java | 12 +++++----- .../peerat/framework/auth/Authenticator.java | 6 ++--- .../framework/auth/JwtAuthenticator.java | 24 +++++++++++++++---- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/dev/peerat/framework/Client.java b/src/dev/peerat/framework/Client.java index c705c24..2fb141d 100644 --- a/src/dev/peerat/framework/Client.java +++ b/src/dev/peerat/framework/Client.java @@ -46,11 +46,8 @@ public class Client extends Thread{ } private User isLogin(RequestType type, HttpReader reader) throws AuthException{ - String auth = reader.getHeader("Authorization"); - if(auth == null) return null; - auth = auth.substring(7); try{ - return this.router.getUser(auth); + return this.router.getUser(type, reader); }catch(Exception e){ try{ writer.response(401, this.router.getDefaultHeaders(type)); diff --git a/src/dev/peerat/framework/Router.java b/src/dev/peerat/framework/Router.java index cda30a9..48057ea 100644 --- a/src/dev/peerat/framework/Router.java +++ b/src/dev/peerat/framework/Router.java @@ -161,18 +161,18 @@ public class Router{ return this; } - public U getUser(String token) throws Exception{ - return this.auth != null ? this.auth.getUser(token) : null; + public U getUser(RequestType type, HttpReader reader) throws Exception{ + return this.auth != null ? this.auth.getUser(type, reader) : null; + } + + public U getUser(String data) throws Exception{ + return this.auth != null ? this.auth.getUser(data) : null; } public String createAuthUser(U user) throws Exception{ return this.auth.createAuthUser(user); } - public User isLogin(RequestType type, HttpReader reader) throws Exception{ - return this.auth.isLogin(type, reader); - } - public Locker getLogger(){ return this.logger; } diff --git a/src/dev/peerat/framework/auth/Authenticator.java b/src/dev/peerat/framework/auth/Authenticator.java index 8709de0..fd10428 100644 --- a/src/dev/peerat/framework/auth/Authenticator.java +++ b/src/dev/peerat/framework/auth/Authenticator.java @@ -6,10 +6,10 @@ import dev.peerat.framework.User; public interface Authenticator{ - U getUser(String token) throws Exception; - String createAuthUser(U user) throws Exception; - User isLogin(RequestType type, HttpReader reader) throws Exception; + U getUser(RequestType type, HttpReader reader) throws Exception; + + U getUser(String data) throws Exception; } diff --git a/src/dev/peerat/framework/auth/JwtAuthenticator.java b/src/dev/peerat/framework/auth/JwtAuthenticator.java index c2a2211..e4c284e 100644 --- a/src/dev/peerat/framework/auth/JwtAuthenticator.java +++ b/src/dev/peerat/framework/auth/JwtAuthenticator.java @@ -15,9 +15,11 @@ import org.jose4j.jwt.JwtClaims; import org.jose4j.jwt.consumer.JwtConsumer; import org.jose4j.jwt.consumer.JwtConsumerBuilder; +import dev.peerat.framework.HttpReader; +import dev.peerat.framework.RequestType; import dev.peerat.framework.User; -public class JwtAuthenticator{ +public class JwtAuthenticator implements Authenticator{ private RsaJsonWebKey rsaJsonWebKey; private JwtConsumer jwtConsumer; @@ -58,10 +60,7 @@ public class JwtAuthenticator{ return this.rsaJsonWebKey.toParams(OutputControlLevel.INCLUDE_PRIVATE); } - public U getUser(String token) throws Exception{ - return this.userCreator.apply(this.jwtConsumer.processToClaims(token)); - } - + @Override public String createAuthUser(U user) throws Exception{ JwtClaims claims = new JwtClaims(); claims.setGeneratedJwtId(); // a unique identifier for the token @@ -78,4 +77,19 @@ public class JwtAuthenticator{ jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); return jws.getCompactSerialization(); } + + @Override + public U getUser(RequestType type, HttpReader reader) throws Exception{ + String auth = reader.getHeader("Authorization"); + if(auth == null) throw new AuthException("Header 'Authorization' notfound"); + auth = auth.substring(7); + return this.userCreator.apply(this.jwtConsumer.processToClaims(auth)); + } + + @Override + public U getUser(String data) throws Exception { + if(data == null) throw new AuthException("Null Token"); + return this.userCreator.apply(this.jwtConsumer.processToClaims(data)); + } + }