Merge pull request 'token' (#1) from token into master

Reviewed-on: #1
This commit is contained in:
v4l3n71n 2022-04-11 20:40:09 +00:00
commit 367715dd32
8 changed files with 259 additions and 0 deletions

View File

@ -40,6 +40,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt-build</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>

View File

@ -0,0 +1,36 @@
package com.covas;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Jwt2 {
public String name;
public Boolean status;
public String message;
public Jwt2(){
name = "";
status = true;
message = "";
}
public Jwt2(String name){
this.name = name;
status = true;
message = "";
}
public Jwt2(String name, String message){
this.name = name;
this.message = message;
status = true;
}
public Jwt2(String name, Boolean status, String message){
this.name = name;
this.status = status;
this.message = message;
}
}

View File

@ -0,0 +1,36 @@
package com.covas;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Token {
public String name;
public Boolean isHttps;
public String authScheme;
public Boolean hasJwt;
public String birthday;
public String role = "";
public Token(){
this.name = "anonymous";
this.isHttps = false;
this.authScheme = "";
this.hasJwt = false;
this.birthday = "";
this.role = "";
}
public Token(String name, Boolean isHttps, String authScheme, Boolean hasJwt){
this.name = name;
this.isHttps = isHttps;
this.authScheme = authScheme;
this.hasJwt = hasJwt;
this.birthday = "";
this.role = "";
}
}

View File

@ -0,0 +1,109 @@
package com.covas;
import java.util.Arrays;
import java.util.HashSet;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
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;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import io.smallrye.jwt.auth.principal.JWTParser;
import io.smallrye.jwt.auth.principal.ParseException;
import io.smallrye.jwt.build.Jwt;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Path("/token")
public class TokenRessource {
@Inject
JsonWebToken jwt;
@Inject JWTParser parser;
@GET
@Path("authentificate")
@Produces(MediaType.APPLICATION_JSON)
public Response getUserName(@CookieParam("jwt") String jwtCookie) {
if (jwtCookie == null) {
// Create a JWT token signed using the 'HS256' algorithm
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn("Alice").groups(new HashSet<>(Arrays.asList("User"))).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("Alice")).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else {
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
try {
jwt = parser.parse(jwtCookie);
}
catch(ParseException p){
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2("Alice", false, p.getMessage())).build();
}
// or jwt = parser.decrypt(jwtCookie, secret);
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName())).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({"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;
}
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;
}
}

View File

@ -0,0 +1,6 @@
smallrye.jwt.sign.key.location=privateKey.pem
mp.jwt.verify.publickey.location=publicKey.pem
mp.jwt.verify.issuer=https://example.com/issuer

View File

@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDpjthUAWnz5l6d
Y5UTjW8zXx4IGwU0BK2HN7oPeZs1LTA6UDbHQtNK3rBlI/ycxFcmbGWm0v5folan
rRoCkpZjbvlddyATc1puVXn8vtKUBuXfJNQL28riSzIp3ifflKEVkwtCvyeLHTWN
XTATKArmQhWEBwQozwqITMhKxHcBfKJOBv3Hwd0S8elnHLyRzzVd60ulDAtV0Glv
09B3/j1DV+5NQU9iBq7I997RX4uVekkk3vjk/71ego5WhRhMTB6WTZ3zySCndkUY
6NNygPcL1i0CVKs8nLpgreTYxeObY/yXD7SlJQHM0jO20UNt6O313BzajI/E/b0s
UiXEaJWXAgMBAAECggEAf8+D4rTDXo16yXZtZj3H1L0Amj7vIgBNVenREBbkML2B
3ud2kqSnLBmhPjfrTX7EkmpPVnOwcHVOJoQ775x4YAGixNsj2oENw8NS9lUWt5ru
wKZOxA6LgDrWQMMp3thfLodBak1hPmfFno6YeGM5U7hmPdH5HLvwb5ZVJVVOKqkV
+a5vIqxNfsk/e2WPXI1LkSE/PlXvAS/6+CCNUXekis8ug12BRUM2qxBqpZuqUty+
IuoLHf8RZ1J0rTksbgV/Z7BYxr8nEuQ0zm2CnejGJ4v5hLID23jD9QNbvv+qrp/b
ULoo4Dmct/ju/Y6JJPxQcy17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