-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Proposed fix for missing WWW-Authenticate header #1877
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,8 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter | |
|
||
private final AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource(); | ||
|
||
private final String realmName; | ||
|
||
private AuthenticationConverter authenticationConverter; | ||
|
||
private AuthenticationSuccessHandler authenticationSuccessHandler = this::onAuthenticationSuccess; | ||
|
@@ -104,10 +106,12 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter | |
* @param requestMatcher the {@link RequestMatcher} used for matching against the | ||
* {@code HttpServletRequest} | ||
*/ | ||
public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationManager, | ||
RequestMatcher requestMatcher) { | ||
public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationManager, RequestMatcher requestMatcher, | ||
String realmName) { | ||
this.realmName = realmName; | ||
Assert.notNull(authenticationManager, "authenticationManager cannot be null"); | ||
Assert.notNull(requestMatcher, "requestMatcher cannot be null"); | ||
Assert.notNull(realmName, "realmName cannot be null"); | ||
this.authenticationManager = authenticationManager; | ||
this.requestMatcher = requestMatcher; | ||
// @formatter:off | ||
|
@@ -140,9 +144,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse | |
validateClientIdentifier(authenticationRequest); | ||
Authentication authenticationResult = this.authenticationManager.authenticate(authenticationRequest); | ||
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, authenticationResult); | ||
filterChain.doFilter(request, response); | ||
} | ||
else { | ||
this.authenticationFailureHandler.onAuthenticationFailure(request, response, | ||
new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define |
||
} | ||
filterChain.doFilter(request, response); | ||
|
||
} | ||
catch (OAuth2AuthenticationException ex) { | ||
if (this.logger.isTraceEnabled()) { | ||
|
@@ -204,20 +211,11 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp | |
|
||
SecurityContextHolder.clearContext(); | ||
|
||
// TODO | ||
// The authorization server MAY return an HTTP 401 (Unauthorized) status code | ||
// to indicate which HTTP authentication schemes are supported. | ||
// If the client attempted to authenticate via the "Authorization" request header | ||
// field, | ||
// the authorization server MUST respond with an HTTP 401 (Unauthorized) status | ||
// code and | ||
// include the "WWW-Authenticate" response header field | ||
// matching the authentication scheme used by the client. | ||
|
||
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError(); | ||
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); | ||
if (OAuth2ErrorCodes.INVALID_CLIENT.equals(error.getErrorCode())) { | ||
httpResponse.setStatusCode(HttpStatus.UNAUTHORIZED); | ||
httpResponse.getHeaders().set("WWW-Authenticate", "Basic realm=\"" + this.realmName + "\""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only return |
||
} | ||
else { | ||
httpResponse.setStatusCode(HttpStatus.BAD_REQUEST); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove
realmName
as the framework would not make much use of it. Furthermore, the implicit "realm" where the client credentials are authenticated against is the single instance ofRegisteredClientRepository
.The "realm" parameter is mainly used for user-based authentication.