Define user

This commit is contained in:
Valentin CZERYBA 2022-04-10 11:06:53 +02:00
parent ad04426bec
commit dd081ae82a
2 changed files with 20 additions and 4 deletions

View File

@ -10,6 +10,7 @@ public class Token {
public String authScheme; public String authScheme;
public Boolean hasJwt; public Boolean hasJwt;
public String birthday; public String birthday;
public String role = "";
public Token(){ public Token(){
this.name = "anonymous"; this.name = "anonymous";
@ -17,6 +18,7 @@ public class Token {
this.authScheme = ""; this.authScheme = "";
this.hasJwt = false; this.hasJwt = false;
this.birthday = ""; this.birthday = "";
this.role = "";
} }
public Token(String name, Boolean isHttps, String authScheme, Boolean hasJwt){ public Token(String name, Boolean isHttps, String authScheme, Boolean hasJwt){
@ -25,6 +27,7 @@ public class Token {
this.authScheme = authScheme; this.authScheme = authScheme;
this.hasJwt = hasJwt; this.hasJwt = hasJwt;
this.birthday = ""; this.birthday = "";
this.role = "";
} }

View File

@ -10,6 +10,7 @@ import javax.ws.rs.CookieParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -36,13 +37,13 @@ public class TokenRessource {
private String secret = "AyM1SysPpbyDfgZld3umj1qzKObwVMko"; private String secret = "AyM1SysPpbyDfgZld3umj1qzKObwVMko";
@GET @GET
@Path("authentificate") @Path("authentificate/{role}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getUserName(@CookieParam("jwt") String jwtCookie) { public Response getUserName(@CookieParam("jwt") String jwtCookie, @PathParam("role") String role) {
if (jwtCookie == null) { if (jwtCookie == null) {
// Create a JWT token signed using the 'HS256' algorithm // Create a JWT token signed using the 'HS256' algorithm
// String newJwtCookie = Jwt.upn("Alice").groups(new HashSet<>(Arrays.asList("User", "Admin"))).signWithSecret(secret); // String newJwtCookie = Jwt.upn("Alice").groups(new HashSet<>(Arrays.asList("User", "Admin"))).signWithSecret(secret);
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn("Alice").groups(new HashSet<>(Arrays.asList("User", "Admin"))).sign(); String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn("Alice").groups(new HashSet<>(Arrays.asList("User", role))).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);
@ -71,11 +72,23 @@ public class TokenRessource {
@GET @GET
@Path("roles-allowed") @Path("roles-allowed")
@RolesAllowed({ "User", "Admin" }) @RolesAllowed({"Admin" })
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Token helloRolesAllowed(@Context SecurityContext ctx) { public Token helloRolesAllowed(@Context SecurityContext ctx) {
Token token = getResponseString(ctx); Token token = getResponseString(ctx);
token.name = jwt.getName().toString(); token.name = jwt.getName().toString();
token.role = "Admin";
return token;
}
@GET
@Path("roles-user")
@RolesAllowed({"Toto"})
@Produces(MediaType.APPLICATION_JSON)
public Token helloRolesUser(@Context SecurityContext ctx) {
Token token = getResponseString(ctx);
token.name = jwt.getName().toString();
token.role = "User";
return token; return token;
} }