Skip to content

Commit 1a97d07

Browse files
committed
Merge branch '6.2.x' into 6.3.x
Closes gh-15829
2 parents dfce3a2 + 551c483 commit 1a97d07

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -440,7 +440,21 @@ private String buildAuthorizationRequestUri() {
440440
Map<String, Object> parameters = getParameters(); // Not encoded
441441
this.parametersConsumer.accept(parameters);
442442
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
443-
parameters.forEach((k, v) -> queryParams.set(encodeQueryParam(k), encodeQueryParam(String.valueOf(v)))); // Encoded
443+
parameters.forEach((k, v) -> {
444+
String key = encodeQueryParam(k);
445+
if (v instanceof Iterable) {
446+
((Iterable<?>) v).forEach((value) -> queryParams.add(key, encodeQueryParam(String.valueOf(value))));
447+
}
448+
else if (v != null && v.getClass().isArray()) {
449+
Object[] values = (Object[]) v;
450+
for (Object value : values) {
451+
queryParams.add(key, encodeQueryParam(String.valueOf(value)));
452+
}
453+
}
454+
else {
455+
queryParams.set(key, encodeQueryParam(String.valueOf(v)));
456+
}
457+
});
444458
UriBuilder uriBuilder = this.uriBuilderFactory.uriString(this.authorizationUri).queryParams(queryParams);
445459
return this.authorizationRequestUriFunction.apply(uriBuilder).toString();
446460
}

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.util.Arrays;
2121
import java.util.HashMap;
22+
import java.util.LinkedHashMap;
2223
import java.util.LinkedHashSet;
2324
import java.util.Map;
2425
import java.util.Set;
@@ -319,4 +320,49 @@ public void buildWhenNonAsciiAdditionalParametersThenProperlyEncoded() {
319320
+ "item%20amount=19.95%E2%82%AC&%C3%A2ge=4%C2%BD&item%20name=H%C3%85M%C3%96");
320321
}
321322

323+
@Test
324+
public void buildWhenAdditionalParametersContainsArrayThenProperlyEncoded() {
325+
Map<String, Object> additionalParameters = new LinkedHashMap<>();
326+
additionalParameters.put("item1", new String[] { "1", "2" });
327+
additionalParameters.put("item2", "value2");
328+
OAuth2AuthorizationRequest authorizationRequest = TestOAuth2AuthorizationRequests.request()
329+
.additionalParameters(additionalParameters)
330+
.build();
331+
assertThat(authorizationRequest.getAuthorizationRequestUri()).isNotNull();
332+
assertThat(authorizationRequest.getAuthorizationRequestUri())
333+
.isEqualTo("https://example.com/#/oauth/authorize?response_type=code&client_id=client-id&state=state&"
334+
+ "redirect_uri=https://example.com/authorize/oauth2/code/registration-id&"
335+
+ "item1=1&item1=2&item2=value2");
336+
}
337+
338+
@Test
339+
public void buildWhenAdditionalParametersContainsIterableThenProperlyEncoded() {
340+
Map<String, Object> additionalParameters = new LinkedHashMap<>();
341+
additionalParameters.put("item1", Arrays.asList("1", "2"));
342+
additionalParameters.put("item2", "value2");
343+
OAuth2AuthorizationRequest authorizationRequest = TestOAuth2AuthorizationRequests.request()
344+
.additionalParameters(additionalParameters)
345+
.build();
346+
assertThat(authorizationRequest.getAuthorizationRequestUri()).isNotNull();
347+
assertThat(authorizationRequest.getAuthorizationRequestUri())
348+
.isEqualTo("https://example.com/#/oauth/authorize?response_type=code&client_id=client-id&state=state&"
349+
+ "redirect_uri=https://example.com/authorize/oauth2/code/registration-id&"
350+
+ "item1=1&item1=2&item2=value2");
351+
}
352+
353+
@Test
354+
public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUriContainsNull() {
355+
Map<String, Object> additionalParameters = new LinkedHashMap<>();
356+
additionalParameters.put("item1", null);
357+
additionalParameters.put("item2", "value2");
358+
OAuth2AuthorizationRequest authorizationRequest = TestOAuth2AuthorizationRequests.request()
359+
.additionalParameters(additionalParameters)
360+
.build();
361+
assertThat(authorizationRequest.getAuthorizationRequestUri()).isNotNull();
362+
assertThat(authorizationRequest.getAuthorizationRequestUri())
363+
.isEqualTo("https://example.com/#/oauth/authorize?response_type=code&client_id=client-id&state=state&"
364+
+ "redirect_uri=https://example.com/authorize/oauth2/code/registration-id&"
365+
+ "item1=null&item2=value2");
366+
}
367+
322368
}

0 commit comments

Comments
 (0)