-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Add Jwt Client Authentication support #9520
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
Closed
jgrandja
wants to merge
4
commits into
spring-projects:master
from
jgrandja:gh-8175-jwt-client-authn
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
156 changes: 156 additions & 0 deletions
156
...curity/oauth2/client/endpoint/AbstractOAuth2AuthorizationGrantRequestEntityConverter.java
This file contains hidden or 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,156 @@ | ||
| /* | ||
| * Copyright 2002-2021 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.net.URI; | ||
|
|
||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.http.RequestEntity; | ||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import org.springframework.util.MultiValueMap; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
|
|
||
| /** | ||
| * Base implementation of a {@link Converter} that converts the provided | ||
| * {@link AbstractOAuth2AuthorizationGrantRequest} instance to a {@link RequestEntity} | ||
| * representation of an OAuth 2.0 Access Token Request for the Authorization Grant. | ||
| * | ||
| * @param <T> the type of {@link AbstractOAuth2AuthorizationGrantRequest} | ||
| * @author Joe Grandja | ||
| * @since 5.5 | ||
| * @see Converter | ||
| * @see AbstractOAuth2AuthorizationGrantRequest | ||
| * @see RequestEntity | ||
| */ | ||
| public abstract class AbstractOAuth2AuthorizationGrantRequestEntityConverter<T extends AbstractOAuth2AuthorizationGrantRequest> | ||
| implements Converter<T, RequestEntity<?>> { | ||
|
|
||
| // @formatter:off | ||
| protected Converter<T, HttpHeaders> headersConverter = | ||
jgrandja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (authorizationGrantRequest) -> OAuth2AuthorizationGrantRequestEntityUtils | ||
| .getTokenRequestHeaders(authorizationGrantRequest.getClientRegistration()); | ||
| // @formatter:on | ||
|
|
||
| protected Converter<T, MultiValueMap<String, String>> parametersConverter = this::createParameters; | ||
jgrandja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Sub-class constructor. | ||
| */ | ||
| protected AbstractOAuth2AuthorizationGrantRequestEntityConverter() { | ||
| } | ||
|
|
||
| @Override | ||
| public RequestEntity<?> convert(T authorizationGrantRequest) { | ||
| HttpHeaders headers = this.headersConverter.convert(authorizationGrantRequest); | ||
| MultiValueMap<String, String> parameters = this.parametersConverter.convert(authorizationGrantRequest); | ||
| URI uri = UriComponentsBuilder | ||
| .fromUriString(authorizationGrantRequest.getClientRegistration().getProviderDetails().getTokenUri()) | ||
| .build().toUri(); | ||
| return new RequestEntity<>(parameters, headers, HttpMethod.POST, uri); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a {@link MultiValueMap} of the parameters used in the OAuth 2.0 Access | ||
| * Token Request body. | ||
| * @param authorizationGrantRequest the authorization grant request | ||
| * @return a {@link MultiValueMap} of the parameters used in the OAuth 2.0 Access | ||
| * Token Request body | ||
| */ | ||
| protected abstract MultiValueMap<String, String> createParameters(T authorizationGrantRequest); | ||
jgrandja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Sets the {@link Converter} used for converting the | ||
| * {@link AbstractOAuth2AuthorizationGrantRequest} instance to a {@link HttpHeaders} | ||
| * used in the OAuth 2.0 Access Token Request headers. | ||
| * @param headersConverter the {@link Converter} used for converting the | ||
| * {@link OAuth2AuthorizationCodeGrantRequest} to {@link HttpHeaders} | ||
| */ | ||
| public final void setHeadersConverter(Converter<T, HttpHeaders> headersConverter) { | ||
| Assert.notNull(headersConverter, "headersConverter cannot be null"); | ||
| this.headersConverter = headersConverter; | ||
| } | ||
|
|
||
| /** | ||
| * Add (compose) the provided {@code headersConverter} to the current | ||
| * {@link Converter} used for converting the | ||
| * {@link AbstractOAuth2AuthorizationGrantRequest} instance to a {@link HttpHeaders} | ||
| * used in the OAuth 2.0 Access Token Request headers. | ||
| * @param headersConverter the {@link Converter} to add (compose) to the current | ||
| * {@link Converter} used for converting the | ||
| * {@link OAuth2AuthorizationCodeGrantRequest} to a {@link HttpHeaders} | ||
| */ | ||
| public final void addHeadersConverter(Converter<T, HttpHeaders> headersConverter) { | ||
| Assert.notNull(headersConverter, "headersConverter cannot be null"); | ||
| Converter<T, HttpHeaders> currentHeadersConverter = this.headersConverter; | ||
| this.headersConverter = (authorizationGrantRequest) -> { | ||
| // Append headers using a Composite Converter | ||
| HttpHeaders headers = currentHeadersConverter.convert(authorizationGrantRequest); | ||
| if (headers == null) { | ||
| headers = new HttpHeaders(); | ||
| } | ||
| HttpHeaders headersToAdd = headersConverter.convert(authorizationGrantRequest); | ||
| if (headersToAdd != null) { | ||
| headers.addAll(headersToAdd); | ||
| } | ||
| return headers; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the {@link Converter} used for converting the | ||
| * {@link AbstractOAuth2AuthorizationGrantRequest} instance to a {@link MultiValueMap} | ||
| * of the parameters used in the OAuth 2.0 Access Token Request body. | ||
| * @param parametersConverter the {@link Converter} used for converting the | ||
| * {@link OAuth2AuthorizationCodeGrantRequest} to a {@link MultiValueMap} of the | ||
| * parameters | ||
| */ | ||
| public final void setParametersConverter(Converter<T, MultiValueMap<String, String>> parametersConverter) { | ||
| Assert.notNull(parametersConverter, "parametersConverter cannot be null"); | ||
| this.parametersConverter = parametersConverter; | ||
| } | ||
|
|
||
| /** | ||
| * Add (compose) the provided {@code parametersConverter} to the current | ||
| * {@link Converter} used for converting the | ||
| * {@link AbstractOAuth2AuthorizationGrantRequest} instance to a {@link MultiValueMap} | ||
| * of the parameters used in the OAuth 2.0 Access Token Request body. | ||
| * @param parametersConverter the {@link Converter} to add (compose) to the current | ||
| * {@link Converter} used for converting the | ||
| * {@link OAuth2AuthorizationCodeGrantRequest} to a {@link MultiValueMap} of the | ||
| * parameters | ||
| */ | ||
| public final void addParametersConverter(Converter<T, MultiValueMap<String, String>> parametersConverter) { | ||
| Assert.notNull(parametersConverter, "parametersConverter cannot be null"); | ||
| Converter<T, MultiValueMap<String, String>> currentParametersConverter = this.parametersConverter; | ||
| this.parametersConverter = (authorizationGrantRequest) -> { | ||
| // Append parameters using a Composite Converter | ||
| MultiValueMap<String, String> parameters = currentParametersConverter.convert(authorizationGrantRequest); | ||
| if (parameters == null) { | ||
| parameters = new LinkedMultiValueMap<>(); | ||
| } | ||
| MultiValueMap<String, String> parametersToAdd = parametersConverter.convert(authorizationGrantRequest); | ||
| if (parametersToAdd != null) { | ||
| parameters.addAll(parametersToAdd); | ||
| } | ||
| return parameters; | ||
| }; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.