From 768602baeb1998f7ce1c330dba655dd1a72067bf Mon Sep 17 00:00:00 2001 From: GrmpfNarf Date: Fri, 28 Mar 2025 14:16:03 +0100 Subject: [PATCH 1/3] Add Equals and HashCode methods for better comparison. Closes gh-16394 Signed-off-by: Maximilian Klose --- .../endpoint/OAuth2AuthorizationRequest.java | 67 ++++++++++++++++++- .../OAuth2AuthorizationRequestTests.java | 45 ++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java index ff40a307858..2e85ccd4329 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java @@ -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. @@ -188,6 +188,71 @@ public static Builder authorizationCode() { return new Builder(AuthorizationGrantType.AUTHORIZATION_CODE); } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || this.getClass() != obj.getClass()) { + return false; + } + OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj; + + if (!this.authorizationUri.equals(that.authorizationUri)) { + return false; + } + + if (!this.authorizationGrantType.equals(that.authorizationGrantType)) { + return false; + } + + if (this.responseType != that.responseType) { + return false; + } + + if (!this.clientId.equals(that.clientId)) { + return false; + } + + if (!this.redirectUri.equals(that.redirectUri)) { + return false; + } + + if (!this.scopes.equals(that.scopes)) { + return false; + } + + if (!this.state.equals(that.state)) { + return false; + } + + if (!this.additionalParameters.equals(that.additionalParameters)) { + return false; + } + + if (!this.authorizationRequestUri.equals(that.authorizationRequestUri)) { + return false; + } + + return this.attributes.equals(that.attributes); + } + + @Override + public int hashCode() { + int result = this.authorizationUri.hashCode(); + result = 31 * result + this.clientId.hashCode(); + result = 31 * result + ((this.authorizationGrantType == null) ? 0 : this.authorizationGrantType.hashCode()); + result = 31 * result + ((this.responseType == null) ? 0 : this.responseType.hashCode()); + result = 31 * result + ((this.redirectUri == null) ? 0 : this.redirectUri.hashCode()); + result = 31 * result + ((this.scopes == null) ? 0 : this.scopes.hashCode()); + result = 31 * result + ((this.state == null) ? 0 : this.state.hashCode()); + result = 31 * result + ((this.additionalParameters == null) ? 0 : this.additionalParameters.hashCode()); + result = 31 * result + ((this.authorizationRequestUri == null) ? 0 : this.authorizationRequestUri.hashCode()); + result = 31 * result + ((this.attributes == null) ? 0 : this.attributes.hashCode()); + + return result; + } + /** * Returns a new {@link Builder}, initialized with the values from the provided * {@code authorizationRequest}. diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java index 1c4365560d8..27b093cfd0a 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java @@ -18,6 +18,7 @@ import java.net.URI; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -28,8 +29,7 @@ import org.springframework.security.oauth2.core.AuthorizationGrantType; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.*; /** * Tests for {@link OAuth2AuthorizationRequest}. @@ -365,4 +365,45 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri + "item1=null&item2=value2"); } + @Test + public void equalsTrueTest() { + OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() + .authorizationRequestUri("http://example.com") + .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); + + OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() + .authorizationRequestUri("http://example.com") + .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); + + assertThat(authorizationRequest1).isEqualTo(authorizationRequest2); + } + + @Test + public void hashCodeTest() { + OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() + .authorizationRequestUri("http://example.com") + .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); + + OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() + .authorizationRequestUri("http://example.com") + .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); + + int authorizationRequest1HashCode = authorizationRequest1.hashCode(); + int authorizationRequest2HashCode = authorizationRequest2.hashCode(); + + assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode); + } + } From 0ba7b866f661eed3c4ff3444dddebc64a6b9d088 Mon Sep 17 00:00:00 2001 From: Maximilian Klose Date: Fri, 25 Apr 2025 11:49:02 +0200 Subject: [PATCH 2/3] Add Equals and HashCode methods for better comparison. Closes gh-16394 Signed-off-by: Maximilian Klose --- .../endpoint/OAuth2AuthorizationRequest.java | 63 +++++-------------- .../OAuth2AuthorizationRequestTests.java | 44 +++++++------ 2 files changed, 38 insertions(+), 69 deletions(-) diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java index 2e85ccd4329..0147b0f4f44 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java @@ -24,6 +24,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -198,59 +199,23 @@ public boolean equals(Object obj) { } OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj; - if (!this.authorizationUri.equals(that.authorizationUri)) { - return false; - } - - if (!this.authorizationGrantType.equals(that.authorizationGrantType)) { - return false; - } - - if (this.responseType != that.responseType) { - return false; - } - - if (!this.clientId.equals(that.clientId)) { - return false; - } - - if (!this.redirectUri.equals(that.redirectUri)) { - return false; - } - - if (!this.scopes.equals(that.scopes)) { - return false; - } - - if (!this.state.equals(that.state)) { - return false; - } - - if (!this.additionalParameters.equals(that.additionalParameters)) { - return false; - } - - if (!this.authorizationRequestUri.equals(that.authorizationRequestUri)) { - return false; - } - - return this.attributes.equals(that.attributes); + return Objects.equals(this.authorizationUri, that.authorizationUri) + && Objects.equals(this.authorizationGrantType, that.authorizationGrantType) + && Objects.equals(this.responseType, that.responseType) && Objects.equals(this.clientId, that.clientId) + && Objects.equals(this.redirectUri, that.redirectUri) && Objects.equals(this.scopes, that.scopes) + && Objects.equals(this.state, that.state) + && Objects.equals(this.additionalParameters, that.additionalParameters) + && Objects.equals(this.authorizationRequestUri, that.authorizationRequestUri) + && Objects.equals(this.attributes, that.attributes); } @Override public int hashCode() { - int result = this.authorizationUri.hashCode(); - result = 31 * result + this.clientId.hashCode(); - result = 31 * result + ((this.authorizationGrantType == null) ? 0 : this.authorizationGrantType.hashCode()); - result = 31 * result + ((this.responseType == null) ? 0 : this.responseType.hashCode()); - result = 31 * result + ((this.redirectUri == null) ? 0 : this.redirectUri.hashCode()); - result = 31 * result + ((this.scopes == null) ? 0 : this.scopes.hashCode()); - result = 31 * result + ((this.state == null) ? 0 : this.state.hashCode()); - result = 31 * result + ((this.additionalParameters == null) ? 0 : this.additionalParameters.hashCode()); - result = 31 * result + ((this.authorizationRequestUri == null) ? 0 : this.authorizationRequestUri.hashCode()); - result = 31 * result + ((this.attributes == null) ? 0 : this.attributes.hashCode()); - - return result; + return Objects.hashCode(this.authorizationUri) + Objects.hashCode(this.clientId) + + Objects.hashCode(this.authorizationGrantType) + Objects.hashCode(this.responseType) + + Objects.hashCode(this.redirectUri) + Objects.hashCode(this.scopes) + Objects.hashCode(this.state) + + Objects.hashCode(this.additionalParameters) + Objects.hashCode(this.authorizationRequestUri) + + Objects.hashCode(this.attributes); } /** diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java index 27b093cfd0a..106c57c0b1a 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java @@ -368,18 +368,20 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri @Test public void equalsTrueTest() { OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); assertThat(authorizationRequest1).isEqualTo(authorizationRequest2); } @@ -387,18 +389,20 @@ public void equalsTrueTest() { @Test public void hashCodeTest() { OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); int authorizationRequest1HashCode = authorizationRequest1.hashCode(); int authorizationRequest2HashCode = authorizationRequest2.hashCode(); From b15a8311d454e06209e3aca06846e0be107a355c Mon Sep 17 00:00:00 2001 From: Maximilian Klose Date: Fri, 25 Apr 2025 11:49:02 +0200 Subject: [PATCH 3/3] Add Equals and HashCode methods for better comparison. Closes gh-16394 Signed-off-by: GrmpfNarf --- .../endpoint/OAuth2AuthorizationRequest.java | 63 +++++-------------- .../OAuth2AuthorizationRequestTests.java | 44 +++++++------ 2 files changed, 38 insertions(+), 69 deletions(-) diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java index 2e85ccd4329..0147b0f4f44 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java @@ -24,6 +24,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -198,59 +199,23 @@ public boolean equals(Object obj) { } OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj; - if (!this.authorizationUri.equals(that.authorizationUri)) { - return false; - } - - if (!this.authorizationGrantType.equals(that.authorizationGrantType)) { - return false; - } - - if (this.responseType != that.responseType) { - return false; - } - - if (!this.clientId.equals(that.clientId)) { - return false; - } - - if (!this.redirectUri.equals(that.redirectUri)) { - return false; - } - - if (!this.scopes.equals(that.scopes)) { - return false; - } - - if (!this.state.equals(that.state)) { - return false; - } - - if (!this.additionalParameters.equals(that.additionalParameters)) { - return false; - } - - if (!this.authorizationRequestUri.equals(that.authorizationRequestUri)) { - return false; - } - - return this.attributes.equals(that.attributes); + return Objects.equals(this.authorizationUri, that.authorizationUri) + && Objects.equals(this.authorizationGrantType, that.authorizationGrantType) + && Objects.equals(this.responseType, that.responseType) && Objects.equals(this.clientId, that.clientId) + && Objects.equals(this.redirectUri, that.redirectUri) && Objects.equals(this.scopes, that.scopes) + && Objects.equals(this.state, that.state) + && Objects.equals(this.additionalParameters, that.additionalParameters) + && Objects.equals(this.authorizationRequestUri, that.authorizationRequestUri) + && Objects.equals(this.attributes, that.attributes); } @Override public int hashCode() { - int result = this.authorizationUri.hashCode(); - result = 31 * result + this.clientId.hashCode(); - result = 31 * result + ((this.authorizationGrantType == null) ? 0 : this.authorizationGrantType.hashCode()); - result = 31 * result + ((this.responseType == null) ? 0 : this.responseType.hashCode()); - result = 31 * result + ((this.redirectUri == null) ? 0 : this.redirectUri.hashCode()); - result = 31 * result + ((this.scopes == null) ? 0 : this.scopes.hashCode()); - result = 31 * result + ((this.state == null) ? 0 : this.state.hashCode()); - result = 31 * result + ((this.additionalParameters == null) ? 0 : this.additionalParameters.hashCode()); - result = 31 * result + ((this.authorizationRequestUri == null) ? 0 : this.authorizationRequestUri.hashCode()); - result = 31 * result + ((this.attributes == null) ? 0 : this.attributes.hashCode()); - - return result; + return Objects.hashCode(this.authorizationUri) + Objects.hashCode(this.clientId) + + Objects.hashCode(this.authorizationGrantType) + Objects.hashCode(this.responseType) + + Objects.hashCode(this.redirectUri) + Objects.hashCode(this.scopes) + Objects.hashCode(this.state) + + Objects.hashCode(this.additionalParameters) + Objects.hashCode(this.authorizationRequestUri) + + Objects.hashCode(this.attributes); } /** diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java index 27b093cfd0a..106c57c0b1a 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java @@ -368,18 +368,20 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri @Test public void equalsTrueTest() { OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); assertThat(authorizationRequest1).isEqualTo(authorizationRequest2); } @@ -387,18 +389,20 @@ public void equalsTrueTest() { @Test public void hashCodeTest() { OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request() - .authorizationRequestUri("http://example.com") - .additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) - .parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue")) - .scope("someScope") - .build(); + .authorizationRequestUri("https://example.com") + .additionalParameters( + Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope") + .build(); int authorizationRequest1HashCode = authorizationRequest1.hashCode(); int authorizationRequest2HashCode = authorizationRequest2.hashCode();