File tree Expand file tree Collapse file tree
main/java/org/springframework/security/oauth2/core/oidc/user
test/java/org/springframework/security/oauth2/core/oidc/user Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919import java .io .Serial ;
2020import java .util .Collection ;
2121import java .util .Map ;
22+ import java .util .Objects ;
2223
2324import org .springframework .security .core .GrantedAuthority ;
2425import org .springframework .security .oauth2 .core .oidc .IdTokenClaimNames ;
@@ -114,4 +115,38 @@ public OidcUserInfo getUserInfo() {
114115 return this .userInfo ;
115116 }
116117
118+ @ Override
119+ public boolean equals (Object obj ) {
120+ if (this == obj ) {
121+ return true ;
122+ }
123+ if (obj == null || this .getClass () != obj .getClass ()) {
124+ return false ;
125+ }
126+ DefaultOidcUser that = (DefaultOidcUser ) obj ;
127+ if (!this .getName ().equals (that .getName ())) {
128+ return false ;
129+ }
130+ if (!this .getAuthorities ().equals (that .getAuthorities ())) {
131+ return false ;
132+ }
133+ if (this .getIdToken ().getIssuer () == null || that .getIdToken ().getIssuer () == null ) {
134+ return false ;
135+ }
136+ return Objects .equals (this .getIdToken ().getIssuer ().toExternalForm (),
137+ that .getIdToken ().getIssuer ().toExternalForm ())
138+ && Objects .equals (this .getIdToken ().getSubject (), that .getIdToken ().getSubject ());
139+ }
140+
141+ @ Override
142+ public int hashCode () {
143+ int result = this .getName ().hashCode ();
144+ result = 31 * result + this .getAuthorities ().hashCode ();
145+ result = 31 * result + ((this .getIdToken ().getIssuer () != null )
146+ ? this .getIdToken ().getIssuer ().toExternalForm ().hashCode () : 0 );
147+ result = 31 * result
148+ + ((this .getIdToken ().getSubject () != null ) ? this .getIdToken ().getSubject ().hashCode () : 0 );
149+ return result ;
150+ }
151+
117152}
Original file line number Diff line number Diff line change 1717package org .springframework .security .oauth2 .core .oidc .user ;
1818
1919import java .time .Instant ;
20+ import java .time .temporal .ChronoUnit ;
2021import java .util .Collections ;
2122import java .util .HashMap ;
2223import java .util .Map ;
@@ -147,4 +148,31 @@ public void constructorWhenAllParametersProvidedAndValidThenCreated() {
147148 StandardClaimNames .NAME , StandardClaimNames .EMAIL );
148149 }
149150
151+ // gh-18622
152+ @ Test
153+ public void equalsWhenOidcUserPrincipalSameThenTrue () {
154+ String issuer = "https://example.com" ;
155+ String subject = "subject-1" ;
156+
157+ // @formatter:off
158+ OidcIdToken idToken1 = OidcIdToken .withTokenValue ("id-token-value-1" )
159+ .issuer (issuer )
160+ .subject (subject )
161+ .issuedAt (Instant .now ())
162+ .expiresAt (Instant .now ().plus (30 , ChronoUnit .MINUTES ))
163+ .build ();
164+
165+ OidcIdToken idToken2 = OidcIdToken .withTokenValue ("id-token-value-2" )
166+ .issuer (issuer )
167+ .subject (subject )
168+ .issuedAt (Instant .now ())
169+ .expiresAt (Instant .now ().plus (30 , ChronoUnit .MINUTES ))
170+ .build ();
171+ // @formatter:on
172+
173+ DefaultOidcUser user1 = new DefaultOidcUser (AUTHORITIES , idToken1 , USER_INFO );
174+ DefaultOidcUser user2 = new DefaultOidcUser (AUTHORITIES , idToken2 , USER_INFO );
175+ assertThat (user1 ).isEqualTo (user2 );
176+ }
177+
150178}
You can’t perform that action at this time.
0 commit comments