forked from quarkus-qe/quarkus-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor security/jwt in order to use JWT builder inside a Quarkus ap…
…plication
- Loading branch information
pablo gonzalez granados
committed
May 31, 2022
1 parent
03b5768
commit c90514b
Showing
5 changed files
with
120 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
security/jwt/src/main/java/io/quarkus/ts/security/jwt/GenerateJwtResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.quarkus.ts.security.jwt; | ||
|
||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.annotation.security.PermitAll; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import io.smallrye.jwt.build.Jwt; | ||
import io.smallrye.jwt.build.JwtClaimsBuilder; | ||
|
||
@Path("/login") | ||
public class GenerateJwtResource { | ||
|
||
public enum Invalidity { | ||
WRONG_ISSUER, | ||
WRONG_DATE, | ||
WRONG_KEY | ||
} | ||
|
||
private static final String DEFAULT_ISSUER = "https://my.auth.server/"; | ||
private static final int TEN = 10; | ||
|
||
@POST | ||
@Path("/jwt") | ||
@PermitAll | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String login(@QueryParam("invalidity") String invalidity, String body) throws NoSuchAlgorithmException { | ||
Date now = new Date(); | ||
Date expiration = new Date(TimeUnit.SECONDS.toMillis(TEN) + now.getTime()); | ||
String issuer = DEFAULT_ISSUER; | ||
if (invalidity.equalsIgnoreCase(Invalidity.WRONG_ISSUER.name())) { | ||
issuer = "https://wrong/"; | ||
} | ||
|
||
if (invalidity.equalsIgnoreCase(Invalidity.WRONG_DATE.name())) { | ||
now = new Date(now.getTime() - TimeUnit.DAYS.toMillis(TEN)); | ||
expiration = new Date(now.getTime() - TimeUnit.DAYS.toMillis(TEN)); | ||
} | ||
|
||
PrivateKey privateKey = null; | ||
if (invalidity.equalsIgnoreCase(Invalidity.WRONG_KEY.name())) { | ||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); | ||
KeyPair keyPair = keyPairGenerator.generateKeyPair(); | ||
privateKey = keyPair.getPrivate(); | ||
} | ||
|
||
JwtClaimsBuilder jwtbuilder = Jwt.issuer(issuer) | ||
.expiresAt(expiration.getTime()) | ||
.issuedAt(now.getTime()) | ||
.subject("test_subject_at_example_com") | ||
.groups(Set.of(body)) | ||
.claim("upn", "[email protected]") | ||
.claim("roleMappings", Collections.singletonMap("admin", "superuser")); | ||
|
||
if (!Objects.isNull(privateKey)) { | ||
return jwtbuilder.jws().sign(privateKey); | ||
} | ||
|
||
return jwtbuilder.sign(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.