Skip to content

Commit 0ac8618

Browse files
committed
Align DefaultOAuth2AuthorizedClientManager.DefaultContextAttributesMapper
Fixes gh-7350
1 parent dcd997e commit 0ac8618

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizedClientManager.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,6 @@ public Map<String, Object> apply(OAuth2AuthorizeRequest authorizeRequest) {
139139
contextAttributes.put(OAuth2AuthorizationContext.REQUEST_SCOPE_ATTRIBUTE_NAME,
140140
StringUtils.delimitedListToStringArray(scope, " "));
141141
}
142-
String username = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.USERNAME);
143-
String password = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.PASSWORD);
144-
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
145-
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
146-
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
147-
}
148-
149142
return contextAttributes;
150143
}
151144
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizedClientManagerTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import org.springframework.security.oauth2.core.TestOAuth2AccessTokens;
3232
import org.springframework.security.oauth2.core.TestOAuth2RefreshTokens;
3333
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
34+
import org.springframework.util.StringUtils;
3435

36+
import java.util.HashMap;
37+
import java.util.Map;
3538
import java.util.function.Function;
3639

3740
import static org.assertj.core.api.Assertions.assertThat;
@@ -206,9 +209,17 @@ public void authorizeWhenRequestParameterUsernamePasswordThenMappedToContext() {
206209

207210
when(this.authorizedClientProvider.authorize(any(OAuth2AuthorizationContext.class))).thenReturn(this.authorizedClient);
208211

209-
// Override the mock with the default
210-
this.authorizedClientManager.setContextAttributesMapper(
211-
new DefaultOAuth2AuthorizedClientManager.DefaultContextAttributesMapper());
212+
// Set custom contextAttributesMapper
213+
this.authorizedClientManager.setContextAttributesMapper(authorizeRequest -> {
214+
Map<String, Object> contextAttributes = new HashMap<>();
215+
String username = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.USERNAME);
216+
String password = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.PASSWORD);
217+
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
218+
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
219+
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
220+
}
221+
return contextAttributes;
222+
});
212223

213224
this.request.addParameter(OAuth2ParameterNames.USERNAME, "username");
214225
this.request.addParameter(OAuth2ParameterNames.PASSWORD, "password");

0 commit comments

Comments
 (0)