Skip to content

Commit

Permalink
Add copy constructor for DefaultOidcUser
Browse files Browse the repository at this point in the history
  • Loading branch information
andreblanke committed Dec 8, 2024
1 parent 5b2212b commit 6b815d2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@
@JsonIgnoreProperties(value = { "attributes" }, ignoreUnknown = true)
abstract class DefaultOidcUserMixin {

@Deprecated
@JsonCreator
DefaultOidcUserMixin(@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities,
@JsonProperty("idToken") OidcIdToken idToken, @JsonProperty("userInfo") OidcUserInfo userInfo,
@JsonProperty("nameAttributeKey") String nameAttributeKey) {
}

@JsonCreator
DefaultOidcUserMixin(@JsonProperty("name") String name,
@JsonProperty("idToken") OidcIdToken idToken, @JsonProperty("userInfo") OidcUserInfo userInfo,
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ static OidcUser getUser(OidcUserRequest userRequest, OidcUserInfo userInfo) {
authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
}
if (StringUtils.hasText(userNameAttributeName)) {
// TODO: Get name from OidcUserAuthority.collectClaims.
return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo, userNameAttributeName);
}
return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo);
return new DefaultOidcUser(userRequest.getIdToken(), userInfo, authorities);
}

private OidcUserRequestUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,68 @@ public class DefaultOidcUser extends DefaultOAuth2User implements OidcUser {
* @param authorities the authorities granted to the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
*/
@Deprecated
public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken) {
this(authorities, idToken, IdTokenClaimNames.SUB);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param authorities the authorities granted to the user
*/
public DefaultOidcUser(OidcIdToken idToken, Collection<? extends GrantedAuthority> authorities) {
this(null, idToken, authorities);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param authorities the authorities granted to the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param nameAttributeKey the key used to access the user's &quot;name&quot; from
* {@link #getAttributes()}
*/
@Deprecated
public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken,
String nameAttributeKey) {
this(authorities, idToken, null, nameAttributeKey);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param name the name of the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param authorities the authorities granted to the user
*/
public DefaultOidcUser(String name, OidcIdToken idToken, Collection<? extends GrantedAuthority> authorities) {
this(name, idToken, null, authorities);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param authorities the authorities granted to the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
* may be {@code null}
*/
@Deprecated
public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken,
OidcUserInfo userInfo) {
this(authorities, idToken, userInfo, IdTokenClaimNames.SUB);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param authorities the authorities granted to the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
* may be {@code null}
*/
public DefaultOidcUser(OidcIdToken idToken, OidcUserInfo userInfo,
Collection<? extends GrantedAuthority> authorities) {
this(null, idToken, userInfo, authorities);
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param authorities the authorities granted to the user
Expand All @@ -88,13 +122,29 @@ public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcI
* @param nameAttributeKey the key used to access the user's &quot;name&quot; from
* {@link #getAttributes()}
*/
@Deprecated
public DefaultOidcUser(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken,
OidcUserInfo userInfo, String nameAttributeKey) {
super(authorities, OidcUserAuthority.collectClaims(idToken, userInfo), nameAttributeKey);
this.idToken = idToken;
this.userInfo = userInfo;
}

/**
* Constructs a {@code DefaultOidcUser} using the provided parameters.
* @param name the name of the user
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
* may be {@code null}
* @param authorities the authorities granted to the user
*/
public DefaultOidcUser(String name, OidcIdToken idToken, OidcUserInfo userInfo,
Collection<? extends GrantedAuthority> authorities) {
super(name, OidcUserAuthority.collectClaims(idToken, userInfo), authorities);
this.idToken = idToken;
this.userInfo = userInfo;
}

@Override
public Map<String, Object> getClaims() {
return this.getAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ private OidcUserInfo getOidcUserInfo() {
}

private OidcUser defaultPrincipal() {
return new DefaultOidcUser(getAuthorities(), getOidcIdToken(), this.userInfo);
return new DefaultOidcUser(getOidcIdToken(), this.userInfo, getAuthorities());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ private OidcUserInfo getOidcUserInfo() {
}

private OidcUser defaultPrincipal() {
return new DefaultOidcUser(getAuthorities(), getOidcIdToken(), this.userInfo);
return new DefaultOidcUser(getOidcIdToken(), this.userInfo, getAuthorities());
}

}
Expand Down

0 comments on commit 6b815d2

Please sign in to comment.