Skip to content

Commit 8472482

Browse files
committed
Polish authorization consent
Issue spring-projectsgh-340 spring-projectsgh-280
1 parent 98187c8 commit 8472482

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationServerConfigurer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
158158
*
159159
* <ul>
160160
* <li>{@code client_id} - the client identifier</li>
161-
* <li>{@code scope} - the space separated list of scopes present in the authorization request</li>
161+
* <li>{@code scope} - a space-delimited list of scopes present in the authorization request</li>
162162
* <li>{@code state} - a CSRF protection token</li>
163163
* </ul>
164164
*
@@ -172,11 +172,9 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
172172
* <li>It must include the received {@code state} as an HTTP parameter</li>
173173
* <li>It must include the list of {@code scope}s the {@code Resource Owner}
174174
* consented to as an HTTP parameter</li>
175-
* <li>It must include the {@code consent_action} parameter, with a value either
176-
* {@code approve} or {@code cancel} as an HTTP parameter</li>
177175
* </ul>
178176
*
179-
* @param consentPage the consent page to redirect to if consent is required (e.g. "/oauth2/consent")
177+
* @param consentPage the URI of the custom consent page to redirect to if consent is required (e.g. "/oauth2/consent")
180178
* @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
181179
*/
182180
public OAuth2AuthorizationServerConfigurer<B> consentPage(String consentPage) {
@@ -296,7 +294,7 @@ public void configure(B builder) {
296294
authenticationManager,
297295
providerSettings.authorizationEndpoint());
298296
if (StringUtils.hasText(this.consentPage)) {
299-
authorizationEndpointFilter.setUserConsentUri(this.consentPage);
297+
authorizationEndpointFilter.setConsentPage(this.consentPage);
300298
}
301299
builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
302300

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2AuthorizationEndpointFilter.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class OAuth2AuthorizationEndpointFilter extends OncePerRequestFilter {
9090
private final RequestMatcher authorizationEndpointMatcher;
9191
private final AuthenticationConverter authenticationConverter;
9292
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
93-
private String userConsentUri;
93+
private String consentPage;
9494

9595
/**
9696
* Constructs an {@code OAuth2AuthorizationEndpointFilter} using the provided parameters.
@@ -168,11 +168,11 @@ private static RequestMatcher createDefaultRequestMatcher(String authorizationEn
168168
* Specify the URI to redirect Resource Owners to if consent is required. A default consent
169169
* page will be generated when this attribute is not specified.
170170
*
171-
* @param userConsentUri the URI of the custom consent page to redirect to if consent is required (e.g. "/oauth2/consent")
171+
* @param consentPage the URI of the custom consent page to redirect to if consent is required (e.g. "/oauth2/consent")
172172
* @see org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer#consentPage(String)
173173
*/
174-
public final void setUserConsentUri(String userConsentUri) {
175-
this.userConsentUri = userConsentUri;
174+
public final void setConsentPage(String consentPage) {
175+
this.consentPage = consentPage;
176176
}
177177

178178
@Override
@@ -230,24 +230,24 @@ private void sendAuthorizationConsent(HttpServletRequest request, HttpServletRes
230230
.toUriString();
231231
this.redirectStrategy.sendRedirect(request, response, redirectUri);
232232
} else {
233-
UserConsentPage.displayConsent(request, response, clientId, principal, requestedScopes, authorizedScopes, state);
233+
DefaultConsentPage.displayConsent(request, response, clientId, principal, requestedScopes, authorizedScopes, state);
234234
}
235235
}
236236

237237
private boolean hasConsentUri() {
238-
return StringUtils.hasText(this.userConsentUri);
238+
return StringUtils.hasText(this.consentPage);
239239
}
240240

241241
private String resolveConsentUri(HttpServletRequest request) {
242-
if (UrlUtils.isAbsoluteUrl(this.userConsentUri)) {
243-
return this.userConsentUri;
242+
if (UrlUtils.isAbsoluteUrl(this.consentPage)) {
243+
return this.consentPage;
244244
}
245245
RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();
246246
urlBuilder.setScheme(request.getScheme());
247247
urlBuilder.setServerName(request.getServerName());
248248
urlBuilder.setPort(request.getServerPort());
249249
urlBuilder.setContextPath(request.getContextPath());
250-
urlBuilder.setPathInfo(this.userConsentUri);
250+
urlBuilder.setPathInfo(this.consentPage);
251251
return urlBuilder.getUrl();
252252
}
253253

@@ -427,7 +427,7 @@ private static void throwError(String errorCode, String parameterName, String er
427427
/**
428428
* For internal use only.
429429
*/
430-
private static class UserConsentPage {
430+
private static class DefaultConsentPage {
431431
private static final MediaType TEXT_HTML_UTF8 = new MediaType("text", "html", StandardCharsets.UTF_8);
432432

433433
private static void displayConsent(HttpServletRequest request, HttpServletResponse response,
@@ -485,7 +485,7 @@ private static String generateConsentPage(HttpServletRequest request,
485485

486486
for (String scope : scopesToAuthorize) {
487487
builder.append(" <div class=\"form-group form-check py-1\">");
488-
builder.append(" <input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"" + scope + "\" id=\"" + scope + "\" checked>");
488+
builder.append(" <input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"" + scope + "\" id=\"" + scope + "\">");
489489
builder.append(" <label class=\"form-check-label\" for=\"" + scope + "\">" + scope + "</label>");
490490
builder.append(" </div>");
491491
}

oauth2-authorization-server/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationCodeGrantTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ private static String getAuthorizationHeader(RegisteredClient registeredClient)
501501

502502
private static String scopeCheckbox(String scope) {
503503
return MessageFormat.format(
504-
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"{0}\" id=\"{0}\" checked>",
504+
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"{0}\" id=\"{0}\">",
505505
scope
506506
);
507507
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/web/OAuth2AuthorizationEndpointFilterTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void doFilterWhenAuthorizationRequestConsentRequiredWithCustomConsentUriT
285285
MockHttpServletResponse response = new MockHttpServletResponse();
286286
FilterChain filterChain = mock(FilterChain.class);
287287

288-
this.filter.setUserConsentUri("/oauth2/custom-consent");
288+
this.filter.setConsentPage("/oauth2/custom-consent");
289289
this.filter.doFilter(request, response, filterChain);
290290

291291
verify(this.authenticationManager).authenticate(any());
@@ -471,7 +471,7 @@ private static OAuth2AuthorizationCodeRequestAuthenticationToken.Builder authori
471471

472472
private static String scopeCheckbox(String scope) {
473473
return MessageFormat.format(
474-
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"{0}\" id=\"{0}\" checked>",
474+
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"{0}\" id=\"{0}\">",
475475
scope
476476
);
477477
}

samples/boot/oauth2-integration/authorizationserver-custom-consent-page/src/main/resources/templates/consent.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ <h1 class="text-center text-primary">App permissions</h1>
4242
type="checkbox"
4343
name="scope"
4444
th:value="${scope.scope}"
45-
th:id="${scope.scope}"
46-
checked>
45+
th:id="${scope.scope}">
4746
<label class="form-check-label font-weight-bold" th:for="${scope.scope}" th:text="${scope.scope}"></label>
4847
<p class="text-primary" th:text="${scope.description}"></p>
4948
</div>
@@ -60,12 +59,12 @@ <h1 class="text-center text-primary">App permissions</h1>
6059
</div>
6160

6261
<div class="form-group pt-3">
63-
<button class="btn btn-primary btn-lg" type="submit" name="consent_action" value="approve">
62+
<button class="btn btn-primary btn-lg" type="submit">
6463
Submit Consent
6564
</button>
6665
</div>
6766
<div class="form-group">
68-
<button class="btn btn-link regular" type="submit" name="consent_action" value="cancel">
67+
<button class="btn btn-link regular" type="submit">
6968
Cancel
7069
</button>
7170
</div>

0 commit comments

Comments
 (0)