Skip to content

Example test showing the minimalist response to an empty Authorizatio… #16976

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

Conversation

danielshiplett
Copy link

@danielshiplett danielshiplett commented Apr 21, 2025

…n header

relates to #16977

We recently had an application request fail to provide any bearer token in the Authorization header. They received a 401 as expected. However, the www-authenticate response header only had the word Bearer in the value. This made it difficult to debug as it implied that they were sending a bearer token when they weren't.

I suggest an improvement in Spring Security to add an error message such as missing_token or some other similar value to help identify the root problem.

This PR contains a test case that will pass when the additional error message is provided.

I am willing to provide a fix for this as well. However, I don't know where in Spring Security to implement such a fix. I know about BearerTokenAuthenticationEntryPoint where other bearer token related errors have their details added. However, that doesn't feel right in this case because we aren't 100% guaranteed the intent of the request was to provide a bearer token (some other form of Authorization may be supported along with OAuth2 Resource Server).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 21, 2025
@jzheaux
Copy link
Contributor

jzheaux commented Apr 21, 2025

Thanks for the idea, @danielshiplett, though this deviates from the RFC which states (emphasis mine):

If the request lacks any authentication information (e.g., the client
was unaware that authentication is necessary or attempted using an
unsupported authentication method), the resource server SHOULD NOT
include an error code or other error information
.

As such, I'm going to close this PR. Note that you can publish your own entry point that does as you describe:

class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private final BearerAuthenticationEntryPoint delegate = new BearerAuthenticationEntryPoint();

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) {
        if (ex instanceof InsufficientAuthenticationException insufficient) {
            this.delegate.commence(request, response, new OAuth2AuthenticationException("missing token"));
            return;
        }
        this.delegate.commence(request, response, ex);
    }
}

@jzheaux jzheaux closed this Apr 21, 2025
@jzheaux jzheaux self-assigned this Apr 21, 2025
@jzheaux jzheaux added type: enhancement A general enhancement status: declined A suggestion or change that we don't feel we should currently apply in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 21, 2025
@danielshiplett
Copy link
Author

Hey Josh. Great feedback. Thanks for the callout on the RFC and the example. I take it, like most of the Beans, if I create my own MyAuthenticationEntryPoint Bean, the standard one(s) will back off?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants