|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import org.springframework.util.MultiValueMap;
|
24 | 24 | import org.springframework.util.StringUtils;
|
25 | 25 | import org.springframework.web.util.UriComponentsBuilder;
|
| 26 | +import org.springframework.web.util.UriUtils; |
26 | 27 |
|
27 | 28 | import java.io.Serializable;
|
28 | 29 | import java.nio.charset.StandardCharsets;
|
@@ -376,29 +377,34 @@ public OAuth2AuthorizationRequest build() {
|
376 | 377 |
|
377 | 378 | private String buildAuthorizationRequestUri() {
|
378 | 379 | MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
379 |
| - parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, this.responseType.getValue()); |
380 |
| - parameters.set(OAuth2ParameterNames.CLIENT_ID, this.clientId); |
| 380 | + parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, encodeQueryParam(this.responseType.getValue())); |
| 381 | + parameters.set(OAuth2ParameterNames.CLIENT_ID, encodeQueryParam(this.clientId)); |
381 | 382 | if (!CollectionUtils.isEmpty(this.scopes)) {
|
382 | 383 | parameters.set(OAuth2ParameterNames.SCOPE,
|
383 |
| - StringUtils.collectionToDelimitedString(this.scopes, " ")); |
| 384 | + encodeQueryParam(StringUtils.collectionToDelimitedString(this.scopes, " "))); |
384 | 385 | }
|
385 | 386 | if (this.state != null) {
|
386 |
| - parameters.set(OAuth2ParameterNames.STATE, this.state); |
| 387 | + parameters.set(OAuth2ParameterNames.STATE, encodeQueryParam(this.state)); |
387 | 388 | }
|
388 | 389 | if (this.redirectUri != null) {
|
389 |
| - parameters.set(OAuth2ParameterNames.REDIRECT_URI, this.redirectUri); |
| 390 | + parameters.set(OAuth2ParameterNames.REDIRECT_URI, encodeQueryParam(this.redirectUri)); |
390 | 391 | }
|
391 | 392 | if (!CollectionUtils.isEmpty(this.additionalParameters)) {
|
392 |
| - this.additionalParameters.forEach((k, v) -> parameters.set(k, v.toString())); |
| 393 | + this.additionalParameters.forEach((k, v) -> |
| 394 | + parameters.set(encodeQueryParam(k), encodeQueryParam(v.toString()))); |
393 | 395 | }
|
394 | 396 |
|
395 | 397 | return UriComponentsBuilder.fromHttpUrl(this.authorizationUri)
|
396 | 398 | .queryParams(parameters)
|
397 |
| - .encode(StandardCharsets.UTF_8) |
398 | 399 | .build()
|
399 | 400 | .toUriString();
|
400 | 401 | }
|
401 | 402 |
|
| 403 | + // Encode query parameter value according to RFC 3986 |
| 404 | + private static String encodeQueryParam(String value) { |
| 405 | + return UriUtils.encodeQueryParam(value, StandardCharsets.UTF_8); |
| 406 | + } |
| 407 | + |
402 | 408 | private LinkedHashSet<String> toLinkedHashSet(String... scope) {
|
403 | 409 | LinkedHashSet<String> result = new LinkedHashSet<>();
|
404 | 410 | Collections.addAll(result, scope);
|
|
0 commit comments