Refractor Authenticator -> was visibly not finished previously

This commit is contained in:
jeffcheasey88 2025-01-24 23:15:15 +01:00
parent 8a6b744519
commit 7e4cc9f425
4 changed files with 29 additions and 18 deletions

View file

@ -46,11 +46,8 @@ public class Client<U extends User> extends Thread{
} }
private User isLogin(RequestType type, HttpReader reader) throws AuthException{ private User isLogin(RequestType type, HttpReader reader) throws AuthException{
String auth = reader.getHeader("Authorization");
if(auth == null) return null;
auth = auth.substring(7);
try{ try{
return this.router.getUser(auth); return this.router.getUser(type, reader);
}catch(Exception e){ }catch(Exception e){
try{ try{
writer.response(401, this.router.getDefaultHeaders(type)); writer.response(401, this.router.getDefaultHeaders(type));

View file

@ -161,18 +161,18 @@ public class Router<U extends User>{
return this; return this;
} }
public U getUser(String token) throws Exception{ public U getUser(RequestType type, HttpReader reader) throws Exception{
return this.auth != null ? this.auth.getUser(token) : null; 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{ public String createAuthUser(U user) throws Exception{
return this.auth.createAuthUser(user); return this.auth.createAuthUser(user);
} }
public User isLogin(RequestType type, HttpReader reader) throws Exception{
return this.auth.isLogin(type, reader);
}
public Locker<Context> getLogger(){ public Locker<Context> getLogger(){
return this.logger; return this.logger;
} }

View file

@ -6,10 +6,10 @@ import dev.peerat.framework.User;
public interface Authenticator<U extends User>{ public interface Authenticator<U extends User>{
U getUser(String token) throws Exception;
String createAuthUser(U user) 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;
} }

View file

@ -15,9 +15,11 @@ import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.JwtConsumer; import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder; import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.User; import dev.peerat.framework.User;
public class JwtAuthenticator<U extends User>{ public class JwtAuthenticator<U extends User> implements Authenticator<U>{
private RsaJsonWebKey rsaJsonWebKey; private RsaJsonWebKey rsaJsonWebKey;
private JwtConsumer jwtConsumer; private JwtConsumer jwtConsumer;
@ -58,10 +60,7 @@ public class JwtAuthenticator<U extends User>{
return this.rsaJsonWebKey.toParams(OutputControlLevel.INCLUDE_PRIVATE); return this.rsaJsonWebKey.toParams(OutputControlLevel.INCLUDE_PRIVATE);
} }
public U getUser(String token) throws Exception{ @Override
return this.userCreator.apply(this.jwtConsumer.processToClaims(token));
}
public String createAuthUser(U user) throws Exception{ public String createAuthUser(U user) throws Exception{
JwtClaims claims = new JwtClaims(); JwtClaims claims = new JwtClaims();
claims.setGeneratedJwtId(); // a unique identifier for the token claims.setGeneratedJwtId(); // a unique identifier for the token
@ -78,4 +77,19 @@ public class JwtAuthenticator<U extends User>{
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
return jws.getCompactSerialization(); 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));
}
} }