change class name and moving functions
This commit is contained in:
parent
047a32a677
commit
d00aacbada
@ -3,19 +3,19 @@ package com.covas.Json;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class Jwt2 {
|
||||
public class Message {
|
||||
|
||||
public String name;
|
||||
public Boolean status;
|
||||
public String message;
|
||||
|
||||
public Jwt2(){
|
||||
public Message(){
|
||||
name = "";
|
||||
status = true;
|
||||
message = "";
|
||||
}
|
||||
|
||||
public Jwt2(String name){
|
||||
public Message(String name){
|
||||
this.name = name;
|
||||
status = true;
|
||||
message = "";
|
||||
@ -23,13 +23,13 @@ public class Jwt2 {
|
||||
|
||||
|
||||
|
||||
public Jwt2(String name, String message){
|
||||
public Message(String name, String message){
|
||||
this.name = name;
|
||||
this.message = message;
|
||||
status = true;
|
||||
}
|
||||
|
||||
public Jwt2(String name, Boolean status, String message){
|
||||
public Message(String name, Boolean status, String message){
|
||||
this.name = name;
|
||||
this.status = status;
|
||||
this.message = message;
|
@ -4,6 +4,9 @@ import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
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.GET;
|
||||
import javax.ws.rs.Path;
|
||||
@ -13,10 +16,15 @@ import javax.ws.rs.core.Response;
|
||||
|
||||
import com.covas.Json.Hello;
|
||||
|
||||
import org.eclipse.microprofile.jwt.JsonWebToken;
|
||||
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/json")
|
||||
public class HelloRessource {
|
||||
@Inject
|
||||
JsonWebToken jwt;
|
||||
|
||||
Set<Hello> hello = Collections.synchronizedSet(new LinkedHashSet<>());
|
||||
|
||||
public HelloRessource(){
|
||||
@ -24,7 +32,25 @@ public class HelloRessource {
|
||||
}
|
||||
|
||||
@GET
|
||||
@PermitAll
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response hello_json(){
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,10 @@ import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.NewCookie;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
|
||||
import com.covas.Classes.Hash;
|
||||
import com.covas.Entity.UsersEntity;
|
||||
import com.covas.Json.Jwt2;
|
||||
import com.covas.Json.Token;
|
||||
import com.covas.Json.Message;
|
||||
|
||||
import io.smallrye.jwt.auth.principal.JWTParser;
|
||||
import io.smallrye.jwt.auth.principal.ParseException;
|
||||
@ -40,7 +38,6 @@ public class TokenRessource {
|
||||
@Inject JWTParser parser;
|
||||
|
||||
@GET
|
||||
@Path("authentificate")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) {
|
||||
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();
|
||||
// or create a JWT token encrypted using the 'A256KW' algorithm
|
||||
// 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 {
|
||||
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 {
|
||||
// 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);
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user