forked from quarkusio/quarkus
-
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.
Showing
5 changed files
with
103 additions
and
1 deletion.
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
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
35 changes: 35 additions & 0 deletions
35
extensions/smallrye-jwt/deployment/src/test/java/io/quarkus/jwt/test/JwtParserEndpoint.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,35 @@ | ||
package io.quarkus.jwt.test; | ||
|
||
import java.security.PublicKey; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.Path; | ||
|
||
import io.smallrye.jwt.auth.principal.JWTParser; | ||
import io.smallrye.jwt.util.KeyUtils; | ||
|
||
@Path("/parser") | ||
@ApplicationScoped | ||
public class JwtParserEndpoint { | ||
|
||
@Inject | ||
JWTParser parser; | ||
|
||
@GET | ||
@Path("/name") | ||
public String tokenWithoutIssuedAt(@HeaderParam("Authorization") String authorization) throws Exception { | ||
String rawToken = authorization.split(" ")[1].trim(); | ||
return parser.parse(rawToken).getName(); | ||
} | ||
|
||
@GET | ||
@Path("/name-with-key") | ||
public String tokenWithoutIssuedAtWithKey(@HeaderParam("Authorization") String authorization) throws Exception { | ||
String rawToken = authorization.split(" ")[1].trim(); | ||
PublicKey key = KeyUtils.readPublicKey("publicKey.pem"); | ||
return parser.verify(rawToken, key).getName(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
extensions/smallrye-jwt/deployment/src/test/java/io/quarkus/jwt/test/JwtParserUnitTest.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,61 @@ | ||
package io.quarkus.jwt.test; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.security.PrivateKey; | ||
|
||
import org.jose4j.jws.AlgorithmIdentifiers; | ||
import org.jose4j.jws.JsonWebSignature; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.smallrye.jwt.util.KeyUtils; | ||
|
||
public class JwtParserUnitTest { | ||
private static Class<?>[] testClasses = { | ||
JwtParserEndpoint.class | ||
}; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(testClasses) | ||
.addAsResource("publicKey.pem") | ||
.addAsResource("privateKey.pem") | ||
.addAsResource("applicationJwtParser.properties", "application.properties")); | ||
|
||
@Test | ||
public void verifyTokenWithoutIssuedAt() throws Exception { | ||
RestAssured.given().auth() | ||
.oauth2(generateTokenWithoutIssuedAt()) | ||
.get("/parser/name") | ||
.then().assertThat().statusCode(200) | ||
.body(equalTo("alice")); | ||
} | ||
|
||
@Test | ||
public void verifyTokenWithoutIssuedAtWithKey() throws Exception { | ||
RestAssured.given().auth() | ||
.oauth2(generateTokenWithoutIssuedAt()) | ||
.get("/parser/name-with-key") | ||
.then().assertThat().statusCode(200) | ||
.body(equalTo("alice")); | ||
} | ||
|
||
private String generateTokenWithoutIssuedAt() throws Exception { | ||
String payload = "{" | ||
+ "\"sub\":\"alice\"," | ||
+ "\"iss\":\"https://server.example.com\"," | ||
+ "\"exp\":" + (System.currentTimeMillis() / 1000 + 5) + "," | ||
+ "}"; | ||
|
||
JsonWebSignature jws = new JsonWebSignature(); | ||
jws.setPayload(payload); | ||
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); | ||
PrivateKey privateKey = KeyUtils.readPrivateKey("privateKey.pem"); | ||
jws.setKey(privateKey); | ||
return jws.getCompactSerialization(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
extensions/smallrye-jwt/deployment/src/test/resources/applicationJwtParser.properties
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,4 @@ | ||
mp.jwt.verify.publickey.location=/publicKey.pem | ||
smallrye.jwt.sign.key.location=/privateKey.pem | ||
smallrye.jwt.time-to-live=-1 | ||
mp.jwt.verify.issuer=https://server.example.com |