-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Allow in-memory authorized client services to be constructed with a map #5994
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2002-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* The identifier for {@link OAuth2AuthorizedClient}. | ||
* | ||
* @author Vedran Pavic | ||
* @since 5.2 | ||
* @see OAuth2AuthorizedClient | ||
* @see OAuth2AuthorizedClientService | ||
*/ | ||
public final class OAuth2AuthorizedClientId implements Serializable { | ||
|
||
private final String clientRegistrationId; | ||
|
||
private final String principalName; | ||
|
||
private OAuth2AuthorizedClientId(String clientRegistrationId, String principalName) { | ||
Assert.notNull(clientRegistrationId, "clientRegistrationId cannot be null"); | ||
Assert.notNull(principalName, "principalName cannot be null"); | ||
this.clientRegistrationId = clientRegistrationId; | ||
this.principalName = principalName; | ||
} | ||
|
||
/** | ||
* Factory method for creating new {@link OAuth2AuthorizedClientId} using | ||
* {@link ClientRegistration} and principal name. | ||
* @param clientRegistration the client registration | ||
* @param principalName the principal name | ||
* @return the new authorized client id | ||
*/ | ||
public static OAuth2AuthorizedClientId create(ClientRegistration clientRegistration, | ||
String principalName) { | ||
return new OAuth2AuthorizedClientId(clientRegistration.getRegistrationId(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the parameter I'm also curious on why this factory method is needed? Alternatively, we can make the constructor |
||
principalName); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
OAuth2AuthorizedClientId that = (OAuth2AuthorizedClientId) obj; | ||
return Objects.equals(this.clientRegistrationId, that.clientRegistrationId) | ||
&& Objects.equals(this.principalName, that.principalName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.clientRegistrationId, this.principalName); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful to implement |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2002-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client; | ||
|
||
import org.junit.Test; | ||
|
||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link OAuth2AuthorizedClientId}. | ||
* | ||
* @author Vedran Pavic | ||
*/ | ||
public class OAuth2AuthorizedClientIdTests { | ||
|
||
@Test | ||
public void equalsWhenSameRegistrationIdAndPrincipalThenShouldReturnTrue() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal"); | ||
assertThat(id1.equals(id2)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void equalsWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnFalse() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client1"), | ||
"test-principal"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client2"), | ||
"test-principal"); | ||
assertThat(id1.equals(id2)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void equalsWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnFalse() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal1"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal2"); | ||
assertThat(id1.equals(id2)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void hashCodeWhenSameRegistrationIdAndPrincipalThenShouldReturnSame() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal"); | ||
assertThat(id1.hashCode()).isEqualTo(id2.hashCode()); | ||
} | ||
|
||
@Test | ||
public void hashCodeWhenDifferentRegistrationIdAndSamePrincipalThenShouldNotReturnSame() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client1"), | ||
"test-principal"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client2"), | ||
"test-principal"); | ||
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode()); | ||
} | ||
|
||
@Test | ||
public void hashCodeWhenSameRegistrationIdAndDifferentPrincipalThenShouldNotReturnSame() { | ||
OAuth2AuthorizedClientId id1 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal1"); | ||
OAuth2AuthorizedClientId id2 = OAuth2AuthorizedClientId.create(testClientRegistration("test-client"), | ||
"test-principal2"); | ||
assertThat(id1.hashCode()).isNotEqualTo(id2.hashCode()); | ||
} | ||
|
||
private static ClientRegistration testClientRegistration(String registrationId) { | ||
return ClientRegistration.withRegistrationId(registrationId).clientId("id").clientSecret("secret") | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}") | ||
.authorizationUri("http://example.com/authorize").tokenUri("http://example.com/token").build(); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.