use of org.dcache.gplazma.util.JsonWebToken in project dcache by dCache.
the class QueryUserInfoEndpoint method identityProviders.
private Collection<IdentityProvider> identityProviders(String token) throws AuthenticationException {
if (JsonWebToken.isCompatibleFormat(token)) {
try {
JsonWebToken jwt = new JsonWebToken(token);
Optional<String> iss = jwt.getPayloadString("iss");
if (iss.isPresent()) {
try {
URI issuer = new URI(iss.get());
IdentityProvider ip = providersByIssuer.get(issuer);
checkAuthentication(ip != null, "JWT with unknown \"iss\" claim");
LOG.debug("Discovered token is JWT issued by {}", ip.getName());
return Collections.singleton(ip);
} catch (URISyntaxException e) {
LOG.debug("Bad \"iss\" claim \"{}\": {}", iss.get(), e.toString());
throw new AuthenticationException("Bad \"iss\" claim in JWT");
}
}
} catch (IOException e) {
LOG.debug("Failed to parse JWT: {}", e.toString());
throw new AuthenticationException("Bad JWT");
}
}
return providersByIssuer.values();
}
use of org.dcache.gplazma.util.JsonWebToken in project dcache by dCache.
the class JwtFactoryTest method shouldGenerateValidTokenWithAPayloadInstantClaim.
@Test
public void shouldGenerateValidTokenWithAPayloadInstantClaim() throws Exception {
// We work with seconds granularity.
Instant expiry = Instant.now().plus(5, MINUTES).truncatedTo(SECONDS);
given(aJwtFactory());
String jwt = factory.aJwt().withPayloadClaim("sub", "my-identity").withPayloadClaim("exp", expiry).build();
assertTrue(JsonWebToken.isCompatibleFormat(jwt));
JsonWebToken token = new JsonWebToken(jwt);
assertThat(token.getKeyIdentifier(), is(nullValue()));
assertThat(token.getPayloadString("sub"), isPresentAnd(equalTo("my-identity")));
assertThat(token.getPayloadInstant("exp"), isPresentAnd(equalTo(expiry)));
assertThat(token.getPayloadMap(), aMapWithSize(2));
assertThat(token.getPayloadMap(), hasEntry("sub", jsonString("my-identity")));
assertTrue(token.isSignedBy(factory.publicKey()));
}
use of org.dcache.gplazma.util.JsonWebToken in project dcache by dCache.
the class JwtFactoryTest method shouldGenerateValidTokenWithAPayloadArrayClaim.
@Test
public void shouldGenerateValidTokenWithAPayloadArrayClaim() throws Exception {
given(aJwtFactory());
String jwt = factory.aJwt().withPayloadClaim("sub", "my-identity").withPayloadClaim("groups", "group-1", "group-2").build();
assertTrue(JsonWebToken.isCompatibleFormat(jwt));
JsonWebToken token = new JsonWebToken(jwt);
assertThat(token.getKeyIdentifier(), is(nullValue()));
assertThat(token.getPayloadString("sub"), isPresentAnd(equalTo("my-identity")));
assertThat(token.getPayloadStringOrArray("groups"), contains("group-1", "group-2"));
assertThat(token.getPayloadMap(), aMapWithSize(2));
assertThat(token.getPayloadMap(), hasEntry("sub", jsonString("my-identity")));
assertThat(token.getPayloadMap(), hasEntry("groups", jsonArray("group-1", "group-2")));
assertTrue(token.isSignedBy(factory.publicKey()));
}
use of org.dcache.gplazma.util.JsonWebToken in project dcache by dCache.
the class JwtFactoryTest method shouldGenerateValidTokenWithAPayloadStringClaim.
@Test
public void shouldGenerateValidTokenWithAPayloadStringClaim() throws Exception {
given(aJwtFactory());
String jwt = factory.aJwt().withPayloadClaim("sub", "my-identity").build();
assertTrue(JsonWebToken.isCompatibleFormat(jwt));
JsonWebToken token = new JsonWebToken(jwt);
assertThat(token.getKeyIdentifier(), is(nullValue()));
assertThat(token.getPayloadString("sub"), isPresentAnd(equalTo("my-identity")));
assertThat(token.getPayloadMap(), aMapWithSize(1));
assertThat(token.getPayloadMap(), hasEntry("sub", jsonString("my-identity")));
assertTrue(token.isSignedBy(factory.publicKey()));
}
use of org.dcache.gplazma.util.JsonWebToken in project dcache by dCache.
the class JwtFactoryTest method shouldGenerateValidTokenWithoutPayloadClaims.
@Test
public void shouldGenerateValidTokenWithoutPayloadClaims() throws Exception {
given(aJwtFactory());
String jwt = factory.aJwt().build();
assertTrue(JsonWebToken.isCompatibleFormat(jwt));
JsonWebToken token = new JsonWebToken(jwt);
assertThat(token.getKeyIdentifier(), is(nullValue()));
assertThat(token.getPayloadMap(), is(anEmptyMap()));
assertTrue(token.isSignedBy(factory.publicKey()));
}
Aggregations