diff --git a/src/main/java/com/covas/Token.java b/src/main/java/com/covas/Token.java index 0786ca3..fe6eec7 100644 --- a/src/main/java/com/covas/Token.java +++ b/src/main/java/com/covas/Token.java @@ -10,6 +10,7 @@ public class Token { public String authScheme; public Boolean hasJwt; public String birthday; + public String role = ""; public Token(){ this.name = "anonymous"; @@ -17,6 +18,7 @@ public class Token { this.authScheme = ""; this.hasJwt = false; this.birthday = ""; + this.role = ""; } public Token(String name, Boolean isHttps, String authScheme, Boolean hasJwt){ @@ -25,6 +27,7 @@ public class Token { this.authScheme = authScheme; this.hasJwt = hasJwt; this.birthday = ""; + this.role = ""; } diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index ea7f27b..31e68c9 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -10,6 +10,7 @@ import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -36,13 +37,13 @@ public class TokenRessource { private String secret = "AyM1SysPpbyDfgZld3umj1qzKObwVMko"; @GET - @Path("authentificate") + @Path("authentificate/{role}") @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) { // 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.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 // Jwt.upn("alice").encryptWithSecret(secret); @@ -71,11 +72,23 @@ public class TokenRessource { @GET @Path("roles-allowed") - @RolesAllowed({ "User", "Admin" }) + @RolesAllowed({"Admin" }) @Produces(MediaType.APPLICATION_JSON) public Token helloRolesAllowed(@Context SecurityContext ctx) { Token token = getResponseString(ctx); 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; }