remove class useless and reduce reponse http for token class

This commit is contained in:
Valentin CZERYBA 2022-05-12 22:45:54 +02:00
parent 4ea644e1d4
commit 77aa772a84
3 changed files with 15 additions and 44 deletions

View File

@ -1,38 +0,0 @@
package com.covas.Json;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Message {
public String name;
public Boolean status;
public String message;
public Message(){
name = "";
status = true;
message = "";
}
public Message(String name){
this.name = name;
status = true;
message = "";
}
public Message(String name, String message){
this.name = name;
this.message = message;
status = true;
}
public Message(String name, Boolean status, String message){
this.name = name;
this.status = status;
this.message = message;
}
}

View File

@ -16,7 +16,6 @@ import javax.ws.rs.core.Response;
import com.covas.Classes.Hash; import com.covas.Classes.Hash;
import com.covas.Entity.UsersEntity; import com.covas.Entity.UsersEntity;
import com.covas.Json.Message;
import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.JWTParser;
import io.smallrye.jwt.auth.principal.ParseException; import io.smallrye.jwt.auth.principal.ParseException;
@ -54,14 +53,14 @@ public class TokenRessource {
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(new HashSet<>(Arrays.asList(users.roles))).sign(); 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 // or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret); // Jwt.upn("alice").encryptWithSecret(secret);
return Response.status(Response.Status.CREATED).entity(new Message(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); return Response.status(Response.Status.CREATED).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else { } else {
return Response.status(Response.Status.FORBIDDEN).entity(new Message(name, false, "Password is incorrect")).build(); return Response.status(Response.Status.FORBIDDEN).build();
} }
} }
return Response.status(Response.Status.NOT_FOUND).entity(new Message(name, false, "User not found")).build(); return Response.status(Response.Status.NOT_FOUND).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.
@ -69,10 +68,10 @@ public class TokenRessource {
jwt = parser.parse(jwtCookie); jwt = parser.parse(jwtCookie);
} }
catch(ParseException p){ catch(ParseException p){
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Message(name, false, p.getMessage())).build(); return Response.status(Response.Status.UNAUTHORIZED).build();
} }
// or jwt = parser.decrypt(jwtCookie, secret); // or jwt = parser.decrypt(jwtCookie, secret);
return Response.status(Response.Status.OK).entity(new Message(jwt.getName(),"Token is still valid")).build(); return Response.status(Response.Status.OK).build();
} }
} }
} }

View File

@ -2,6 +2,7 @@ package com.covas.Resources;
import java.util.UUID; import java.util.UUID;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
@ -19,11 +20,13 @@ import org.jboss.logging.Logger;
public class UsersRessources { public class UsersRessources {
private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); private static final Logger LOGGER = Logger.getLogger(UsersRessources.class);
@GET @GET
@RolesAllowed({"Admin"})
public Response getUsers(){ public Response getUsers(){
return Response.ok(UsersEntity.listAll()).build(); return Response.ok(UsersEntity.listAll()).build();
} }
@GET @GET
@RolesAllowed({"Admin"})
@Path("{id}") @Path("{id}")
public Response getSingleUser(@PathParam("id") String id){ public Response getSingleUser(@PathParam("id") String id){
UUID uid = UUID.fromString(id); UUID uid = UUID.fromString(id);
@ -34,5 +37,12 @@ public class UsersRessources {
return Response.ok(users).build(); return Response.ok(users).build();
} }
@GET
@RolesAllowed({"User"})
@Path("info")
public Response getInfoUser(){
return Response.ok().build();
}
} }