forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OAuth2ClientManagerBuilder FactoryBean
Issue spring-projectsgh-13288
- Loading branch information
Showing
10 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...gframework/security/config/annotation/web/configuration/OAuth2RestOperationsSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2002-2023 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 | ||
* | ||
* https://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.config.annotation.web.configuration; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.springframework.web.client.RestOperations; | ||
|
||
/** | ||
* @author Steve Riesenberg | ||
*/ | ||
@FunctionalInterface | ||
public interface OAuth2RestOperationsSupplier extends Supplier<RestOperations> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...java/org/springframework/security/oauth2/client/OAuth2AuthorizedClientManagerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2002-2023 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 | ||
* | ||
* https://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.util.function.Consumer; | ||
|
||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.security.oauth2.client.endpoint.DefaultClientCredentialsTokenResponseClient; | ||
import org.springframework.security.oauth2.client.endpoint.DefaultPasswordTokenResponseClient; | ||
import org.springframework.security.oauth2.client.endpoint.DefaultRefreshTokenTokenResponseClient; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.client.RestOperations; | ||
|
||
/** | ||
* @author Steve Riesenberg | ||
*/ | ||
public final class OAuth2AuthorizedClientManagerBuilder implements FactoryBean<OAuth2AuthorizedClientManager> { | ||
|
||
private final ClientRegistrationRepository clientRegistrationRepository; | ||
|
||
private final OAuth2AuthorizedClientRepository authorizedClientRepository; | ||
|
||
// @formatter:off | ||
private OAuth2AuthorizedClientProviderBuilder authorizedClientProviderBuilder = | ||
OAuth2AuthorizedClientProviderBuilder.builder() | ||
.authorizationCode() | ||
.refreshToken() | ||
.clientCredentials() | ||
.password(); | ||
// @formatter:on | ||
|
||
private OAuth2AuthorizedClientManagerBuilder(ClientRegistrationRepository clientRegistrationRepository, | ||
OAuth2AuthorizedClientRepository authorizedClientRepository) { | ||
this.clientRegistrationRepository = clientRegistrationRepository; | ||
this.authorizedClientRepository = authorizedClientRepository; | ||
} | ||
|
||
public OAuth2AuthorizedClientManagerBuilder providers(OAuth2AuthorizedClientProviderBuilder authorizedClientProviderBuilder) { | ||
Assert.notNull(authorizedClientProviderBuilder, "authorizedClientProviderBuilder cannot be null"); | ||
this.authorizedClientProviderBuilder = authorizedClientProviderBuilder; | ||
return this; | ||
} | ||
|
||
public OAuth2AuthorizedClientManagerBuilder providers(Consumer<OAuth2AuthorizedClientProviderBuilder> builderConsumer) { | ||
Assert.notNull(builderConsumer, "consumer cannot be null"); | ||
builderConsumer.accept(this.authorizedClientProviderBuilder); | ||
return this; | ||
} | ||
|
||
public OAuth2AuthorizedClientManagerBuilder provider(OAuth2AuthorizedClientProvider authorizedClientProvider) { | ||
Assert.notNull(authorizedClientProvider, "authorizedClientProvider cannot be null"); | ||
this.authorizedClientProviderBuilder.provider(authorizedClientProvider); | ||
return this; | ||
} | ||
|
||
public OAuth2AuthorizedClientManagerBuilder restOperations(RestOperations restOperations) { | ||
Assert.notNull(restOperations, "restOperations cannot be null"); | ||
// @formatter:off | ||
this.authorizedClientProviderBuilder | ||
.refreshToken((configurer) -> configurer.accessTokenResponseClient( | ||
DefaultRefreshTokenTokenResponseClient.builder() | ||
.restOperations(restOperations) | ||
.build())) | ||
.clientCredentials((configurer) -> configurer.accessTokenResponseClient( | ||
DefaultClientCredentialsTokenResponseClient.builder() | ||
.restOperations(restOperations) | ||
.build())) | ||
.password((configurer) -> configurer.accessTokenResponseClient( | ||
DefaultPasswordTokenResponseClient.builder() | ||
.restOperations(restOperations) | ||
.build())); | ||
// @formatter:on | ||
return this; | ||
} | ||
|
||
public DefaultOAuth2AuthorizedClientManager build() { | ||
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( | ||
this.clientRegistrationRepository, this.authorizedClientRepository); | ||
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProviderBuilder.build()); | ||
return authorizedClientManager; | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizedClientManager getObject() { | ||
return build(); | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return OAuth2AuthorizedClientManager.class; | ||
} | ||
|
||
public static OAuth2AuthorizedClientManagerBuilder builder( | ||
ClientRegistrationRepository clientRegistrationRepository, | ||
OAuth2AuthorizedClientRepository authorizedClientRepository) { | ||
Assert.notNull(clientRegistrationRepository, "clientRegistrationRepository cannot be null"); | ||
Assert.notNull(authorizedClientRepository, "authorizedClientRepository cannot be null"); | ||
return new OAuth2AuthorizedClientManagerBuilder(clientRegistrationRepository, authorizedClientRepository); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...mework/security/oauth2/client/endpoint/DefaultOAuth2AccessTokenResponseClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2002-2023 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 | ||
* | ||
* https://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.endpoint; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.web.client.RestOperations; | ||
|
||
/** | ||
* @author Steve Riesenberg | ||
*/ | ||
final class DefaultOAuth2AccessTokenResponseClientBuilder<T extends AbstractOAuth2AuthorizationGrantRequest> | ||
implements OAuth2AccessTokenResponseClient.Builder<T> { | ||
|
||
private final OAuth2AccessTokenResponseClient<T> accessTokenResponseClient; | ||
|
||
private final Consumer<Converter<T, RequestEntity<?>>> requestEntityConverterSetter; | ||
|
||
private final Consumer<RestOperations> restOperationsSetter; | ||
|
||
DefaultOAuth2AccessTokenResponseClientBuilder(OAuth2AccessTokenResponseClient<T> accessTokenResponseClient, | ||
Consumer<Converter<T, RequestEntity<?>>> requestEntityConverterSetter, | ||
Consumer<RestOperations> restOperationsSetter) { | ||
this.accessTokenResponseClient = accessTokenResponseClient; | ||
this.requestEntityConverterSetter = requestEntityConverterSetter; | ||
this.restOperationsSetter = restOperationsSetter; | ||
} | ||
|
||
@Override | ||
public OAuth2AccessTokenResponseClient.Builder<T> requestEntityConverter(Converter<T, RequestEntity<?>> requestEntityConverter) { | ||
this.requestEntityConverterSetter.accept(requestEntityConverter); | ||
return this; | ||
} | ||
|
||
@Override | ||
public OAuth2AccessTokenResponseClient.Builder<T> restOperations(RestOperations restOperations) { | ||
this.restOperationsSetter.accept(restOperations); | ||
return this; | ||
} | ||
|
||
@Override | ||
public OAuth2AccessTokenResponseClient<T> build() { | ||
return this.accessTokenResponseClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters