Skip to content

Add WWW-Authenticate header.#47

Closed
meiXXI wants to merge 3 commits intoauth0:masterfrom
meiXXI:master
Closed

Add WWW-Authenticate header.#47
meiXXI wants to merge 3 commits intoauth0:masterfrom
meiXXI:master

Conversation

@meiXXI
Copy link
Copy Markdown

@meiXXI meiXXI commented May 18, 2020

This change adds the WWW-Authenticate header in case of a 401 response, as the JWT standard requires it.

@meiXXI meiXXI requested a review from a team May 18, 2020 16:12
@jimmyjames jimmyjames requested review from jimmyjames and removed request for a team May 21, 2020 15:28
Copy link
Copy Markdown
Contributor

@jimmyjames jimmyjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I think this is a good start towards adding the WWW-Authenticate header. Aside from the suggestions mentioned, we should also handle the case where the token is valid but has insufficient privileges (e.g., a 403). I think we'd need to implement the AccessDeniedHandler or possibly extend the AccessDeniedHandlerImpl, but I think it's fine for that to be separate from this PR unless you want to add it here.

"WWW-Authenticate",
String.format("Bearer realm=\"%s\", authorization_uri=\"%soauth/token\"", this.audience, this.issuer)
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use HttpHeaders.WWW-Authenticate instead of hard-coding the String literal.

For the header value, the authorization_uri you included here is really the token endpoint; the authorization uri would be /authorize. As the specification does not require the authorization_uri, I think it would be ok to not include it unless you have a use case you are thinking of.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Jimmy, thanks for your feedback.
The replacement of the headers key by the constant makes absolutely sense.

Regarding to the authorization_uri, the removal of this attribute would change the constructor to JwtAuthenticationEntryPoint(String audience). What do you think of keeping the current constructor JwtAuthenticationEntryPoint(String audience, String issuer) as a secondary one and making authorization_uri depending on the issuer so that authorization_uri is only written when the issuer is set?
I don't have such a deep understanding of the standard, as this is not my normal business. So feel free to reject my suggestions in case you see the standard violated here ;)
...and of course I will change the authroization_uri to [issuer]/authorize

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making authroization_uri dependent on the issuer was a fallacy. I will provide another solution soon. thx.

Copy link
Copy Markdown
Contributor

@jimmyjames jimmyjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update @meiXXI, and apologies for the delay on my end. I've added some more feedback. If you wish, we could also create an issue and I think we can try and take this on in the next week. Or, happy to continue to collaborate with you here on this PR. Thanks!

final String audience;
final String issuer;

public JwtAuthenticationEntryPoint(String audience, String issuer) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to provide a default no-arg constructor, to ensure that existing usages of this class aren't broken with this change.


@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
String modifiedIssuer = this.issuer.endsWith("/") ? this.issuer.substring(0, this.issuer.length() - 1) : this.issuer;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to ensure that the header value won't have double "/"? We will need to account for potential null values (related to the above comment regarding needing a default constructor, in which audience and issuer would be null). Also would be good to make sure all cases are covered by tests, that might be why the code coverage check is failing.

@jimmyjames
Copy link
Copy Markdown
Contributor

I've created #50 to track the issue. I'll follow with a separate PR shortly based on the conversation here to make sure we get this addressed.

@jimmyjames
Copy link
Copy Markdown
Contributor

Fixed with #51

@jimmyjames jimmyjames closed this Jun 17, 2020
@jimmyjames jimmyjames modified the milestone: v1-Next Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants