Function generate token finish

This commit is contained in:
Valentin CZERYBA 2022-05-04 22:42:35 +02:00
parent 77e473442e
commit 047a32a677
2 changed files with 19 additions and 25 deletions

View File

@ -8,7 +8,6 @@ public class Jwt2 {
public String name; public String name;
public Boolean status; public Boolean status;
public String message; public String message;
public String password;
public Jwt2(){ public Jwt2(){
name = ""; name = "";
@ -22,18 +21,12 @@ public class Jwt2 {
message = ""; message = "";
} }
public Jwt2(String name, String password){
this.name = name;
status = true;
this.password = password;
message = "";
}
public Jwt2(String name, String password, String message){
public Jwt2(String name, String message){
this.name = name; this.name = name;
this.message = message; this.message = message;
status = true; status = true;
this.password = password;
} }
public Jwt2(String name, Boolean status, String message){ public Jwt2(String name, Boolean status, String message){

View File

@ -30,13 +30,9 @@ import io.smallrye.jwt.build.Jwt;
import org.eclipse.microprofile.jwt.JsonWebToken; import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jboss.resteasy.annotations.jaxrs.HeaderParam; import org.jboss.resteasy.annotations.jaxrs.HeaderParam;
import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64;
import org.jboss.logging.Logger;
@Path("/token") @Path("/token")
public class TokenRessource { public class TokenRessource {
private static final Logger LOGGER = Logger.getLogger(TokenRessource.class);
@Inject @Inject
JsonWebToken jwt; JsonWebToken jwt;
@ -52,21 +48,26 @@ public class TokenRessource {
if (jwtCookie == null) { if (jwtCookie == null) {
String[] hash = new String(Base64.decode(auth.split(" ")[1]), StandardCharsets.UTF_8).split(":"); String[] hash = new String(Base64.decode(auth.split(" ")[1]), StandardCharsets.UTF_8).split(":");
String pseudo = hash[0]; name = hash[0];
password = Hash.encryptSHA512(hash[1]); password = Hash.encryptSHA512(hash[1]);
UsersEntity users = UsersEntity.findByPseudo(pseudo); UsersEntity users = UsersEntity.findByPseudo(name);
if(users != null){ if(users != null){
if(password.equals(users.password)){
// Create a JWT token signed using the 'HS256' algorithm
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(new HashSet<>(Arrays.asList(users.roles))).sign();
// or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret);
return Response.status(Response.Status.CREATED).entity(new Jwt2(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity(new Jwt2(name, false, "Password is incorrect")).build();
} }
// Create a JWT token signed using the 'HS256' algorithm }
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn("Alice").groups(new HashSet<>(Arrays.asList("User"))).sign(); return Response.status(Response.Status.NOT_FOUND).entity(new Jwt2(name, false, "User not found")).build();
// or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret);
return Response.status(Response.Status.CREATED).entity(new Jwt2(name, password)).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else { } else {
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized. // All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
try { try {
@ -76,7 +77,7 @@ public class TokenRessource {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build(); return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build();
} }
// or jwt = parser.decrypt(jwtCookie, secret); // or jwt = parser.decrypt(jwtCookie, secret);
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),password)).build(); return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),"Token is still valid")).build();
} }
} }
@ -101,7 +102,7 @@ public class TokenRessource {
@GET @GET
@Path("roles-user") @Path("roles-user")
@RolesAllowed({"Toto"}) @RolesAllowed({"User"})
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Token helloRolesUser(@Context SecurityContext ctx) { public Token helloRolesUser(@Context SecurityContext ctx) {
Token token = getResponseString(ctx); Token token = getResponseString(ctx);