use of io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal in project trellis by trellis-ldp.
the class WebIdPrincipalTest method testNoSubPrincipal.
@Test
void testNoSubPrincipal() {
final String iss = "https://example.com/idp/";
final JwtClaims claims = new JwtClaims();
claims.setIssuer(iss);
final JsonWebToken principal = new WebIdPrincipal(new DefaultJWTCallerPrincipal(claims));
assertNull(principal.getName());
}
use of io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal in project trellis by trellis-ldp.
the class WebIdSecurityContextTest method testAdminRoles.
@Test
void testAdminRoles() {
final SecurityContext mockDelegate = mock(SecurityContext.class);
final String iss = "https://example.com/idp/";
final String sub = "acoburn";
final JwtClaims claims = new JwtClaims();
claims.setSubject(sub);
claims.setIssuer(iss);
claims.setClaim("groups", List.of("testers"));
final JsonWebToken principal = new DefaultJWTCallerPrincipal(claims);
final SecurityContext ctx = new WebIdSecurityContext(mockDelegate, principal, singleton(iss + sub));
assertTrue(ctx.isUserInRole(TrellisRoles.ADMIN));
assertTrue(ctx.isUserInRole(TrellisRoles.USER));
assertFalse(ctx.isUserInRole("other-role"));
assertTrue(ctx.isUserInRole("testers"));
}
use of io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal in project quarkus by quarkusio.
the class JwtCallerPrincipalUnitTest method testAllClaims.
@Test
public void testAllClaims() throws InvalidJwtException {
InputStream is = getClass().getResourceAsStream("/Token1.json");
JsonObject content = Json.createReader(is).readObject();
JwtClaims jwtClaims = JwtClaims.parse(content.toString());
DefaultJWTCallerPrincipal principal = new DefaultJWTCallerPrincipal(jwtClaims);
String iss = principal.getIssuer();
Assertions.assertEquals("https://server.example.com", iss);
String jti = principal.getTokenID();
Assertions.assertEquals("a-123", jti);
String name = principal.getName();
Assertions.assertEquals("jdoe@example.com", name);
String upn = principal.getClaim(Claims.upn.name());
Assertions.assertEquals("jdoe@example.com", upn);
Set<String> aud = principal.getAudience();
Assertions.assertEquals(new HashSet<>(Arrays.asList("s6BhdRkqt3")), aud);
Long exp = principal.getExpirationTime();
Assertions.assertEquals(1311281970l, exp.longValue());
Long iat = principal.getIssuedAtTime();
Assertions.assertEquals(1311280970l, iat.longValue());
String sub = principal.getSubject();
Assertions.assertEquals("24400320", sub);
Set<String> groups = principal.getGroups();
String[] expectedGroups = { "Echoer", "Tester", "group1", "group2" };
Assertions.assertEquals(new HashSet<String>(Arrays.asList(expectedGroups)), groups);
/*
* "customDoubleArray": [0.1, 1.1, 2.2, 3.3, 4.4],
*/
JsonArray customDoubleArray = principal.getClaim("customDoubleArray");
Assertions.assertEquals(5, customDoubleArray.size());
Assertions.assertEquals(Json.createValue(0.1), customDoubleArray.getJsonNumber(0));
Assertions.assertEquals(Json.createValue(1.1), customDoubleArray.getJsonNumber(1));
Assertions.assertEquals(Json.createValue(2.2), customDoubleArray.getJsonNumber(2));
Assertions.assertEquals(Json.createValue(3.3), customDoubleArray.getJsonNumber(3));
Assertions.assertEquals(Json.createValue(4.4), customDoubleArray.getJsonNumber(4));
// "customString": "customStringValue",
Assertions.assertEquals("customStringValue", principal.getClaim("customString"));
// "customInteger": 123456789,
JsonNumber customInteger = principal.getClaim("customInteger");
Assertions.assertEquals(Json.createValue(123456789), customInteger);
// "customDouble": 3.141592653589793,
JsonNumber customDouble = principal.getClaim("customDouble");
Assertions.assertEquals(Json.createValue(3.141592653589793), customDouble);
/*
* "customStringArray": ["value0", "value1", "value2" ],
*/
JsonArray customStringArray = principal.getClaim("customStringArray");
Assertions.assertEquals(3, customStringArray.size());
Assertions.assertEquals(Json.createValue("value0"), customStringArray.getJsonString(0));
Assertions.assertEquals(Json.createValue("value1"), customStringArray.getJsonString(1));
Assertions.assertEquals(Json.createValue("value2"), customStringArray.getJsonString(2));
/* "customIntegerArray": [0,1,2,3] */
JsonArray customIntegerArray = principal.getClaim("customIntegerArray");
Assertions.assertEquals(4, customIntegerArray.size());
Assertions.assertEquals(Json.createValue(0), customIntegerArray.getJsonNumber(0));
Assertions.assertEquals(Json.createValue(1), customIntegerArray.getJsonNumber(1));
Assertions.assertEquals(Json.createValue(2), customIntegerArray.getJsonNumber(2));
Assertions.assertEquals(Json.createValue(3), customIntegerArray.getJsonNumber(3));
/*
* "customObject": {
* "my-service": {
* "groups": [
* "group1",
* "group2"
* ],
* "roles": [
* "role-in-my-service"
* ]
* },
* "service-B": {
* "roles": [
* "role-in-B"
* ]
* },
* "service-C": {
* "groups": [
* "groupC",
* "web-tier"
* ]
* }
* }
*/
JsonObject customObject = principal.getClaim("customObject");
String[] keys = { "my-service", "service-B", "service-C" };
Assertions.assertEquals(new HashSet<>(Arrays.asList(keys)), customObject.keySet());
}
use of io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal in project smallrye-jwt by smallrye.
the class ClaimInjectionTest method jwt.
@Produces
@RequestScoped
private static JsonWebToken jwt() throws Exception {
String jwt = Jwt.claims("/token-claims.json").sign("/privateKey.pem");
JsonWebSignature jws = new JsonWebSignature();
jws.setKey(KeyUtils.readPublicKey("/publicKey.pem"));
jws.setCompactSerialization(jwt);
JwtClaims claims = JwtClaims.parse(jws.getPayload());
return new DefaultJWTCallerPrincipal(jwt, claims);
}
use of io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal in project trellis by trellis-ldp.
the class JwtAuthFilterTest method testJwtAuthFilter.
@Test
void testJwtAuthFilter() {
final ContainerRequestContext mockContext = mock(ContainerRequestContext.class);
assertNotNull(filter);
assertNotNull(producer);
final String iss = "https://example.com/idp/";
final String sub = "acoburn";
final JwtClaims claims = new JwtClaims();
claims.setSubject(sub);
claims.setIssuer(iss);
producer.setJsonWebToken(new DefaultJWTCallerPrincipal(claims));
assertDoesNotThrow(() -> filter.filter(mockContext));
verify(mockContext).setSecurityContext(securityArgument.capture());
assertEquals(iss + sub, securityArgument.getValue().getUserPrincipal().getName());
}
Aggregations