Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenID Connect standard claims in ATs for WLCG JWT profile #651

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,62 @@ public JWTClaimsSet buildAccessToken(OAuth2AccessTokenEntity token,

builder.notBeforeTime(Date.from(issueTime));

addScopeClaim(builder, token);
addWlcgVerClaim(builder);

if (!isNull(userInfo)) {
Set<String> groupNames =
groupHelper.resolveGroupNames(token, ((UserInfoAdapter) userInfo).getUserinfo());
addWlcgGroupsScopeClaim(builder, groupNames);

if (token.getScope().contains(ATTR_SCOPE)) {
addAttributeScopeClaim(builder, userInfo);
}
if (properties.getAccessToken().isIncludeAuthnInfo()) {
addAuthnInfoClaims(builder, token.getScope(), userInfo);
}
}

addAudience(builder, authentication);

return builder.build();
}

private void addScopeClaim(Builder builder, OAuth2AccessTokenEntity token) {
if (!token.getScope().isEmpty()) {
builder.claim(SCOPE_CLAIM_NAME, token.getScope().stream().collect(joining(SPACE)));
}
}

private void addWlcgVerClaim(Builder builder) {
builder.claim(WLCG_VER_CLAIM, PROFILE_VERSION);
}

if (!isNull(userInfo)) {
Set<String> groupNames =
groupHelper.resolveGroupNames(token, ((UserInfoAdapter) userInfo).getUserinfo());
private void addWlcgGroupsScopeClaim(Builder builder, Set<String> groupNames) {
if (!groupNames.isEmpty()) {
builder.claim(WLCGGroupHelper.WLCG_GROUPS_SCOPE, groupNames);
}
}

if (!groupNames.isEmpty()) {
builder.claim(WLCGGroupHelper.WLCG_GROUPS_SCOPE, groupNames);
}
private void addAttributeScopeClaim(Builder builder, UserInfo userInfo) {
builder.claim(ATTR_SCOPE,
attributeHelper.getAttributeMapFromUserInfo(((UserInfoAdapter) userInfo).getUserinfo()));
}

if (token.getScope().contains(ATTR_SCOPE)) {
builder.claim(ATTR_SCOPE, attributeHelper
.getAttributeMapFromUserInfo(((UserInfoAdapter) userInfo).getUserinfo()));
}
private void addAuthnInfoClaims(Builder builder, Set<String> scopes, UserInfo userInfo) {
if (scopes.contains("email")) {
builder.claim("email", userInfo.getEmail());
}
if (scopes.contains("profile")) {
builder.claim("preferred_username", userInfo.getPreferredUsername());
builder.claim("name", userInfo.getName());
}
}

private void addAudience(Builder builder, OAuth2Authentication authentication) {
if (!hasAudienceRequest(authentication) && !hasRefreshTokenAudienceRequest(authentication)) {
builder.audience(ALL_AUDIENCES_VALUE);
}

return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
@TestPropertySource(properties = {
// @formatter:off
"iam.jwt-profile.default-profile=wlcg",
"iam.access_token.include_authn_info=true",
"scope.matchers[0].name=storage.read",
"scope.matchers[0].type=path",
"scope.matchers[0].prefix=storage.read",
Expand Down Expand Up @@ -736,7 +737,7 @@ public void attributesAreNotIncludedInAccessTokenWhenNotRequested() throws Excep
}

@Test
public void attributesAreIncludedInAccessTokenWhenNotRequested() throws Exception {
public void attributesAreIncludedInAccessTokenWhenRequested() throws Exception {
IamAccount testAccount =
repo.findByUsername(TEST_USER).orElseThrow(assertionError(EXPECTED_USER_NOT_FOUND));

Expand All @@ -762,4 +763,55 @@ public void attributesAreIncludedInAccessTokenWhenNotRequested() throws Exceptio
assertThat(claims.getJSONObjectClaim("attr").get("test"), is("test"));
}

@Test
public void additionalClaimsAreIncludedInAccessTokenWhenRequested() throws Exception {

String tokenResponseJson = mvc
.perform(post("/token").param("grant_type", "password")
.param("client_id", CLIENT_ID)
.param("client_secret", CLIENT_SECRET)
.param("username", "test")
.param("password", "password")
.param("scope", "openid profile email"))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();

JWTClaimsSet claims =
JWTParser.parse(mapper.readTree(tokenResponseJson).get("access_token").asText())
.getJWTClaimsSet();

assertThat(claims.getClaim("email"), notNullValue());
assertThat(claims.getClaim("email"), is("test@iam.test"));
assertThat(claims.getClaim("name"), notNullValue());
assertThat(claims.getClaim("name"), is("Test User"));
assertThat(claims.getClaim("preferred_username"), notNullValue());
assertThat(claims.getClaim("preferred_username"), is("test"));
}

@Test
public void additionalClaimsAreNotIncludedInAccessTokenWhenRNotequested() throws Exception {

String tokenResponseJson = mvc
.perform(post("/token").param("grant_type", "password")
.param("client_id", CLIENT_ID)
.param("client_secret", CLIENT_SECRET)
.param("username", "test")
.param("password", "password")
.param("scope", "openid address"))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();

JWTClaimsSet claims =
JWTParser.parse(mapper.readTree(tokenResponseJson).get("access_token").asText())
.getJWTClaimsSet();

assertThat(claims.getClaim("email"), nullValue());
assertThat(claims.getClaim("name"), nullValue());
assertThat(claims.getClaim("preferred_username"), nullValue());
}

}
Loading