use of org.springframework.security.oauth2.core.oidc.user.OidcUser in project dhis2-core by dhis2.
the class DhisOidcUserService method loadUser.
@Override
public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
ClientRegistration clientRegistration = userRequest.getClientRegistration();
DhisOidcClientRegistration oidcClientRegistration = clientRegistrationRepository.getDhisOidcClientRegistration(clientRegistration.getRegistrationId());
String mappingClaimKey = oidcClientRegistration.getMappingClaimKey();
OidcUser oidcUser = super.loadUser(userRequest);
OidcUserInfo userInfo = oidcUser.getUserInfo();
Map<String, Object> attributes = oidcUser.getAttributes();
Object claimValue = attributes.get(mappingClaimKey);
if (claimValue == null && userInfo != null) {
claimValue = userInfo.getClaim(mappingClaimKey);
}
if (log.isDebugEnabled()) {
log.debug(String.format("Trying to look up DHIS2 user with OidcUser mapping mappingClaimKey='%s', claim value='%s'", mappingClaimKey, claimValue));
}
if (claimValue != null) {
User user = userService.getUserByOpenId((String) claimValue);
if (user != null) {
return new DhisOidcUser(user, attributes, IdTokenClaimNames.SUB, oidcUser.getIdToken());
}
}
String errorMessage = String.format("Failed to look up DHIS2 user with OidcUser mapping mappingClaimKey='%s', claim value='%s'", mappingClaimKey, claimValue);
if (log.isDebugEnabled()) {
log.debug(errorMessage);
}
OAuth2Error oauth2Error = new OAuth2Error("could_not_map_oidc_user_to_dhis2_user", errorMessage, null);
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
}
use of org.springframework.security.oauth2.core.oidc.user.OidcUser in project spring-security by spring-projects.
the class SecurityMockMvcRequestPostProcessorsOidcLoginTests method oidcLoginWhenNameSpecifiedThenUserHasName.
@Test
public void oidcLoginWhenNameSpecifiedThenUserHasName() throws Exception {
OidcUser oidcUser = new DefaultOidcUser(AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_read"), OidcIdToken.withTokenValue("id-token").claim("custom-attribute", "test-subject").build(), "custom-attribute");
this.mvc.perform(get("/id-token/custom-attribute").with(oidcLogin().oidcUser(oidcUser))).andExpect(content().string("test-subject"));
this.mvc.perform(get("/name").with(oidcLogin().oidcUser(oidcUser))).andExpect(content().string("test-subject"));
this.mvc.perform(get("/client-name").with(oidcLogin().oidcUser(oidcUser))).andExpect(content().string("test-subject"));
}
use of org.springframework.security.oauth2.core.oidc.user.OidcUser in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOidcLoginTests method oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence.
// gh-7794
@Test
public void oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
OidcUser oidcUser = new DefaultOidcUser(AuthorityUtils.createAuthorityList("SCOPE_read"), TestOidcIdTokens.idToken().build());
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin().idToken((i) -> i.subject("foo")).oidcUser(oidcUser)).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "subject");
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin().oidcUser(oidcUser).idToken((i) -> i.subject("bar"))).get().uri("/token").exchange().expectStatus().isOk();
token = this.controller.token;
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "bar");
}
use of org.springframework.security.oauth2.core.oidc.user.OidcUser in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOidcLoginTests method oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication.
@Test
public void oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication() {
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin()).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token).isNotNull();
assertThat(token.getAuthorizedClientRegistrationId()).isEqualTo("test");
assertThat(token.getPrincipal()).isInstanceOf(OidcUser.class);
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "user");
assertThat((Collection<GrantedAuthority>) token.getPrincipal().getAuthorities()).contains(new SimpleGrantedAuthority("SCOPE_read"));
assertThat(((OidcUser) token.getPrincipal()).getIdToken().getTokenValue()).isEqualTo("id-token");
}
use of org.springframework.security.oauth2.core.oidc.user.OidcUser in project spring-security by spring-projects.
the class OidcUserService method loadUser.
@Override
public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
Assert.notNull(userRequest, "userRequest cannot be null");
OidcUserInfo userInfo = null;
if (this.shouldRetrieveUserInfo(userRequest)) {
OAuth2User oauth2User = this.oauth2UserService.loadUser(userRequest);
Map<String, Object> claims = getClaims(userRequest, oauth2User);
userInfo = new OidcUserInfo(claims);
// 1) The sub (subject) Claim MUST always be returned in the UserInfo Response
if (userInfo.getSubject() == null) {
OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE);
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
}
// the UserInfo Response values MUST NOT be used.
if (!userInfo.getSubject().equals(userRequest.getIdToken().getSubject())) {
OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE);
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
}
}
Set<GrantedAuthority> authorities = new LinkedHashSet<>();
authorities.add(new OidcUserAuthority(userRequest.getIdToken(), userInfo));
OAuth2AccessToken token = userRequest.getAccessToken();
for (String authority : token.getScopes()) {
authorities.add(new SimpleGrantedAuthority("SCOPE_" + authority));
}
return getUser(userRequest, userInfo, authorities);
}
Aggregations