remove token

This commit is contained in:
Valentin CZERYBA 2022-07-30 00:20:04 +02:00
parent 948e35ae12
commit d6939de924
2 changed files with 15 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import java.time.Duration;
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.CookieParam; import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
@ -66,11 +67,11 @@ public class TokenRessource {
return Response.status(Response.Status.FORBIDDEN).build(); return Response.status(Response.Status.FORBIDDEN).build();
} }
// Create a JWT token signed using the 'HS256' algorithm // Create a JWT token signed using the 'HS256' algorithm
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(users.roles).claim(Claims.kid, users.id.toString()).expiresIn(Duration.ofMinutes(1)).sign(); String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(users.roles).claim(Claims.kid, users.id.toString()).expiresIn(Duration.ofMinutes(5)).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);
String nameEncoded = Base64.toBase64String(name.getBytes(StandardCharsets.UTF_8)); String nameEncoded = Base64.toBase64String(name.getBytes(StandardCharsets.UTF_8));
return Response.status(Response.Status.CREATED).cookie(new NewCookie(new Cookie("jwt", newJwtCookie), "Token JWT", 60, false), new NewCookie(new Cookie("user", nameEncoded), "Username", 60, false)).build(); return Response.status(Response.Status.CREATED).cookie(new NewCookie(new Cookie("jwt", newJwtCookie), "Token JWT", 300, false), new NewCookie(new Cookie("user", nameEncoded), "Username", 60, false)).build();
} }
// 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 {
@ -86,5 +87,16 @@ public class TokenRessource {
} }
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} }
@DELETE
public Response deleteToken(@CookieParam("jwt") String jwtCookie) {
if(jwtCookie == null){
return Response.status(Response.Status.BAD_REQUEST).build();
}
return Response.ok().cookie(new NewCookie(new Cookie("jwt", null), "", 0, false)).build();
}
} }

View File

@ -29,6 +29,6 @@ quarkus.redis.hosts=redis://redis:6379
quarkus.http.cors=true quarkus.http.cors=true
quarkus.http.origins=http://localhost:8084 quarkus.http.origins=http://localhost:8084
quarkus.http.cors.methods=GET,PUT,POST quarkus.http.cors.methods=GET,PUT,POST,DELETE
quarkus.http.cors.headers=accept,authorization,content-type,x-requested-with,x-foobar quarkus.http.cors.headers=accept,authorization,content-type,x-requested-with,x-foobar
quarkus.http.cors.access-control-allow-credentials=true quarkus.http.cors.access-control-allow-credentials=true