change class name and moving functions

This commit is contained in:
Valentin CZERYBA 2022-05-04 23:07:47 +02:00
parent 047a32a677
commit d00aacbada
3 changed files with 37 additions and 62 deletions

View File

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

View File

@ -4,6 +4,9 @@ import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -13,10 +16,15 @@ import javax.ws.rs.core.Response;
import com.covas.Json.Hello; import com.covas.Json.Hello;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/json") @Path("/json")
public class HelloRessource { public class HelloRessource {
@Inject
JsonWebToken jwt;
Set<Hello> hello = Collections.synchronizedSet(new LinkedHashSet<>()); Set<Hello> hello = Collections.synchronizedSet(new LinkedHashSet<>());
public HelloRessource(){ public HelloRessource(){
@ -24,7 +32,25 @@ public class HelloRessource {
} }
@GET @GET
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
public Response hello_json(){ public Response hello_json(){
return Response.ok(this.hello).build(); return Response.ok(this.hello).build();
} }
@GET
@Path("/user")
@RolesAllowed({"User"})
@Produces(MediaType.APPLICATION_JSON)
public Response hello_user(){
return Response.ok(new Hello(String.format("Hello %s", jwt.getName()))).build();
}
@GET
@Path("/admin")
@RolesAllowed({"Admin"})
@Produces(MediaType.APPLICATION_JSON)
public Response hello_admin(){
return Response.ok(new Hello(String.format("Hello admin %s", jwt.getName()))).build();
}
} }

View File

@ -16,12 +16,10 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import com.covas.Classes.Hash; import com.covas.Classes.Hash;
import com.covas.Entity.UsersEntity; import com.covas.Entity.UsersEntity;
import com.covas.Json.Jwt2; import com.covas.Json.Message;
import com.covas.Json.Token;
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;
@ -40,7 +38,6 @@ public class TokenRessource {
@Inject JWTParser parser; @Inject JWTParser parser;
@GET @GET
@Path("authentificate")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) { public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) {
String name = "anonymous"; String name = "anonymous";
@ -59,14 +56,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 Jwt2(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); return Response.status(Response.Status.CREATED).entity(new Message(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else { } else {
return Response.status(Response.Status.FORBIDDEN).entity(new Jwt2(name, false, "Password is incorrect")).build(); return Response.status(Response.Status.FORBIDDEN).entity(new Message(name, false, "Password is incorrect")).build();
} }
} }
return Response.status(Response.Status.NOT_FOUND).entity(new Jwt2(name, false, "User not found")).build(); return Response.status(Response.Status.NOT_FOUND).entity(new Message(name, false, "User 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.
@ -74,58 +71,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 Jwt2(name, false, p.getMessage())).build(); return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Message(name, false, p.getMessage())).build();
} }
// or jwt = parser.decrypt(jwtCookie, secret); // or jwt = parser.decrypt(jwtCookie, secret);
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),"Token is still valid")).build(); return Response.status(Response.Status.OK).entity(new Message(jwt.getName(),"Token is still valid")).build();
} }
} }
@GET
@Path("permit-all")
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
public Token hello(@Context SecurityContext ctx) {
return getResponseString(ctx);
}
@GET
@Path("roles-allowed")
@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({"User"})
@Produces(MediaType.APPLICATION_JSON)
public Token helloRolesUser(@Context SecurityContext ctx) {
Token token = getResponseString(ctx);
token.name = jwt.getName().toString();
token.role = "User";
return token;
}
private Token getResponseString(SecurityContext ctx) {
String name;
if (ctx.getUserPrincipal() == null) {
name = "anonymous";
} else if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) {
throw new InternalServerErrorException("Principal and JsonWebToken names do not match");
} else {
name = ctx.getUserPrincipal().getName();
}
return new Token(name, ctx.isSecure(), ctx.getAuthenticationScheme(), hasJwt());
}
private boolean hasJwt() {
return jwt.getClaimNames() != null;
}
} }