Skip to content

Expose getter for nameAttributeKey in OAuth2AuthenticatedPrincipal #16003

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

Closed
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,6 +99,23 @@ public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcI
this.userInfo = userInfo;
}

DefaultOidcUser(DefaultOidcUser user, Collection<GrantedAuthority> authorities) {
super(user, authorities);
this.idToken = user.idToken;
this.userInfo = user.userInfo;
}

/**
* Copy this {@code DefaultOAuth2User}, using the provided {@code authorities}
* @param authorities the authorities to use
* @return a new {@code DefaultOAuth2User}
* @since 6.5
*/
@Override
public DefaultOidcUser withAuthorities(Collection<GrantedAuthority> authorities) {
return new DefaultOidcUser(this, authorities);
}

@Override
public Map<String, Object> getClaims() {
return this.getAttributes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,6 +79,12 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
this.nameAttributeKey = nameAttributeKey;
}

protected DefaultOAuth2User(DefaultOAuth2User copy, Collection<GrantedAuthority> authorities) {
this.nameAttributeKey = copy.nameAttributeKey;
this.attributes = copy.attributes;
this.authorities = sortAuthorities(authorities);
}

@Override
public String getName() {
return this.getAttribute(this.nameAttributeKey).toString();
Expand All @@ -94,6 +100,16 @@ public Map<String, Object> getAttributes() {
return this.attributes;
}

/**
* Copy this {@code DefaultOAuth2User}, using the provided {@code authorities}
* @param authorities the authorities to use
* @return a new {@code DefaultOAuth2User}
* @since 6.5
*/
public DefaultOAuth2User withAuthorities(Collection<GrantedAuthority> authorities) {
return new DefaultOAuth2User(this, authorities);
}

private Set<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<>(
Comparator.comparing(GrantedAuthority::getAuthority));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
package org.springframework.security.oauth2.core.oidc.user;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -31,6 +33,7 @@
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.StandardClaimNames;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand Down Expand Up @@ -147,4 +150,15 @@ public void constructorWhenAllParametersProvidedAndValidThenCreated() {
StandardClaimNames.NAME, StandardClaimNames.EMAIL);
}

@Test
public void constructorWhenWithAuthoritiesThenReplaces() {
DefaultOidcUser user = new DefaultOidcUser(AUTHORITIES, ID_TOKEN, USER_INFO);
Collection<GrantedAuthority> additional = new ArrayList<>(AUTHORITIES);
additional.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
DefaultOAuth2User copy = user.withAuthorities(additional);
assertThat((Set<GrantedAuthority>) user.getAuthorities()).containsAll(AUTHORITIES);
assertThat((Set<GrantedAuthority>) copy.getAuthorities()).containsAll(additional);
assertThat(copy.getAttributes()).isEqualTo(user.getAttributes());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.security.oauth2.core.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -109,4 +111,15 @@ public void constructorWhenCreatedThenIsSerializable() {
SerializationUtils.serialize(user);
}

@Test
public void constructorWhenWithAuthoritiesThenReplaces() {
DefaultOAuth2User user = new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, ATTRIBUTE_NAME_KEY);
Collection<GrantedAuthority> additional = new ArrayList<>(AUTHORITIES);
additional.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
DefaultOAuth2User copy = user.withAuthorities(additional);
assertThat((Set<GrantedAuthority>) user.getAuthorities()).containsAll(AUTHORITIES);
assertThat((Set<GrantedAuthority>) copy.getAuthorities()).containsAll(additional);
assertThat(copy.getAttributes()).isEqualTo(user.getAttributes());
}

}