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 ff40a30785..37e323b5e4 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. @@ -24,6 +24,7 @@ import java.util.Collections; 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; @@ -188,6 +189,33 @@ public final class OAuth2AuthorizationRequest implements Serializable { 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; + + 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() { + return Objects.hash(this.authorizationUri, this.clientId, this.authorizationGrantType, this.responseType, + this.redirectUri, this.scopes, this.state, this.additionalParameters, this.authorizationRequestUri, + this.attributes); + } + /** * 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 1c4365560d..9d4b089f1e 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 @@ -365,4 +365,27 @@ public class OAuth2AuthorizationRequestTests { + "item1=null&item2=value2"); } + @Test + public void equalsWhenAllFieldsEqualEqualsThenTrue() { + OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build(); + + OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build(); + + assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2); + assertThat(authorizationRequest1).isEqualTo(authorizationRequest2); + } + + @Test + public void hashCodeWhenAllFieldsEqualThenHashCodesAreEqual() { + OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build(); + + OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build(); + + int authorizationRequest1HashCode = authorizationRequest1.hashCode(); + int authorizationRequest2HashCode = authorizationRequest2.hashCode(); + + assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2); + assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode); + } + } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java index eaf559a28d..c95f0771b3 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java @@ -47,4 +47,15 @@ public final class TestOAuth2AuthorizationRequests { return request().scope("openid"); } + public static OAuth2AuthorizationRequest.Builder allFields() { + // @formatter:off + return request() + .authorizationRequestUri("https://example.com") + .additionalParameters(Map.of("someAdditionalParameterKey", "someAdditionalParameterValue")) + .parameters((parametersMap) -> + parametersMap.put("someParameterKey", "someParameterValue")) + .scope("someScope"); + // @formatter:on + } + }