generate token with cookie and json response

This commit is contained in:
Valentin CZERYBA 2022-04-05 22:34:54 +02:00
parent 402a3100d0
commit 19c99ade07
2 changed files with 47 additions and 5 deletions

View File

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

View File

@ -36,20 +36,26 @@ public class TokenRessource {
@GET
@Path("generate")
@Produces("text/plain")
public Response getUserName(@CookieParam("jwt") String jwtCookie) throws ParseException {
@Produces(MediaType.APPLICATION_JSON)
public Response getUserName(@CookieParam("jwt") String jwtCookie) {
Response response = null;
if (jwtCookie == null) {
// Create a JWT token signed using the 'HS256' algorithm
String newJwtCookie = Jwt.upn("Alice").signWithSecret(secret);
// or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret);
return Response.ok("Alice").cookie(new NewCookie("jwt", newJwtCookie)).build();
return Response.status(Response.Status.CREATED).entity(new Jwt2("Alice")).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else {
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
JsonWebToken jwt = parser.verify(jwtCookie, secret);
try {
JsonWebToken jwt = parser.verify(jwtCookie, secret);
}
catch(ParseException p){
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2("Alice", false, p.getMessage())).build();
}
// or jwt = parser.decrypt(jwtCookie, secret);
return Response.ok(jwt.getName()).build();
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName())).build();
}
}