Skip to content
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

Support anonymous token #16

Closed
wants to merge 5 commits into from
Closed

Support anonymous token #16

wants to merge 5 commits into from

Conversation

Mortinke
Copy link

Use Case
The root endpoint of an API should deliver different links depending on the presence of a valid JWT token.
If a valid JWT exists, the links for the private sector should be delivered. If JWT doesn't exists, the links for the public sector should be delivered.
To implement this, we added the root endpoint as addAnonymousPaths. Unfortunately, a JWTPrincipal.fromContext() will result afterwards in a NPE as spring-security will be ignored for unauthenticated paths (configured here).

Behavior after this pull request:
An AnonymousAuthenticationToken is returned, if no JWT is used for an anonymous path. If a JWT is used for an anonymous path, the old behavior should still to be used (e.g. JWT is expired).

@Mortinke Mortinke force-pushed the supportAnonymousToken branch 2 times, most recently from a2dd707 to 18e7cc5 Compare April 24, 2019 04:32
@Mortinke
Copy link
Author

The commit 18e7cc5 relates to # #17


@NonNull
private Set<String> unauthenticatedPaths;

Copy link
Collaborator

@thorsten-p-weber thorsten-p-weber Apr 24, 2019

Choose a reason for hiding this comment

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

Bad API design. The token filter gets a set of strings. These given strings have a special meaning because they bypass the normal jwt-token handling.
Better: use a builder pattern or use a special method like addUnauthenticatedPaths. You must make the exceptional cases explicitly!

log.warn("no JWT token found {}{} ({})", request.getServletPath(), pathInfo != null ? pathInfo : "",
header);
throw new InvalidTokenException("no token");
if (unauthenticatedPaths.toJavaStream().filter(path -> antPathMatcher.match(path, pathToCheck)).count() == 0) {
Copy link

Choose a reason for hiding this comment

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

if (unauthenticatedPaths.toJavaStream().noneMatch(path -> antPathMatcher.match(path, pathToCheck)))

@@ -15,40 +15,64 @@
*/
Copy link

Choose a reason for hiding this comment

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

That's not JavaDoc (use * instead of **)

String tokenHeader = request.getHeader(TOKEN_HEADER);

if (tokenHeader == null || !tokenHeader.startsWith("Bearer ")) {
final String pathInfo = String.valueOf(request.getPathInfo()).replace("null","");
Copy link

Choose a reason for hiding this comment

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

I'd prefer a dedicated method for this large block (getAuthenticationWithoutToken(request, tokenHeader)).

String header = request.getHeader(TOKEN_HEADER);
String tokenHeader = request.getHeader(TOKEN_HEADER);

if (tokenHeader == null || !tokenHeader.startsWith("Bearer ")) {
Copy link

Choose a reason for hiding this comment

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

Could you introduce a local variable/method to describe what this means? noToken?

@Mortinke
Copy link
Author

replaced by #18

@Mortinke Mortinke closed this May 10, 2019
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.

3 participants