-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
ef11d56
to
4d16cfe
Compare
a2dd707
to
18e7cc5
Compare
18e7cc5
to
82141a9
Compare
|
||
@NonNull | ||
private Set<String> unauthenticatedPaths; | ||
|
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.
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!
82141a9
to
12dd7d6
Compare
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) { |
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.
if (unauthenticatedPaths.toJavaStream().noneMatch(path -> antPathMatcher.match(path, pathToCheck)))
@@ -15,40 +15,64 @@ | |||
*/ |
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.
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",""); |
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.
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 ")) { |
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.
Could you introduce a local variable/method to describe what this means? noToken
?
replaced by #18 |
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, aJWTPrincipal.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).