-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Added OAuth2TokenAttributes to wrap attributes #7014
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
Added OAuth2TokenAttributes to wrap attributes #7014
Conversation
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.
@clementkng thanks for this! I've left a few pieces of feedback inline to align the PR with the ticket description as well as team standards.
public class OAuth2TokenAttributes { | ||
private Map<String, Object> attributes; | ||
|
||
public OAuth2TokenAttributes() { |
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.
It doesn't really make sense to have one constructor that allows for empty attributes and another constructor that doesn't.
For now, let's leave the default constructor out - we can add it in later if it's needed.
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 added the default constructor because constructorWhenAttributesAreNullOrEmptyThenThrowsException()
tests that an empty attributes map throws an illegal argument exception, but the setup for all the tests originally set attributes
to an empty map (which is OK because it doesn't go through the constructor for OAuth2IntrospectionAuthenticationToken
). One potential alternative is to have the ``OAuth2IntrospectionAuthenticationToken.attributesmethod do the checking that the map is not empty and the
OAuth2TokenAttributes` are not null. Would this be better?
...auth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2TokenAttributes.java
Show resolved
Hide resolved
/** | ||
* | ||
* @author Clement Ng | ||
* |
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 add @since 5.2
. Also, I'm sure you are already planning on adding a description of the class, but just calling it out here.
return attributes; | ||
} | ||
|
||
public void putAttributes(String key, String value) { |
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.
Let's please leave the class immutable.
|
||
public OAuth2TokenAttributes(Map<String, Object> attributes) { | ||
Assert.notEmpty(attributes, "attributes cannot be empty"); | ||
this.attributes = attributes; |
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 make a copy of the attributes and make it an immutable copy.
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 we make the attributes
map immutable via Collections.unmodifiableMap
, I believe the setUp()
method in the OAuth2IntrospectionAuthenticationTokenTests
will have to be collapsed into the creation of the instance variables (since right now it puts values into an OAuth2.0 attributes map, but now we need the map to be prepopulated w/ data before constructing the OAuth2.0TokenAttributes
). Would this be OK?
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.
Yes, that is fine. We do want to be careful when modifying tests, but it is better than breaking encapsulation.
* @author Clement Ng | ||
* | ||
*/ | ||
public class OAuth2TokenAttributes { |
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.
It'd be great to have a test class for OAuth2TokenAttributes
.
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 was considering it, but wasn't sure what would be tested w/i the test class that wasn't already covered in OAuth2IntrospectionAuthenticationTokenTests
. Should I pull some attribute-specific tests from OAuth2IntrospectionAuthenticationTokenTests
?
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.
Fair point, we can add tests for this class if and when it becomes more sophisticated.
148cd99
to
e58b959
Compare
Thanks @jzheaux I've addressed the feedback above. Let me know if you have any additional comments! |
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.
@clementkng Thanks for the revisions, they look good! I've left some more feedback inline.
* | ||
* @author Clement Ng | ||
*/ | ||
public class OAuth2TokenAttributesTest { |
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 think this is a stray file, let's remove it.
* @since 5.2 | ||
*/ | ||
public final class OAuth2TokenAttributes { | ||
private Map<String, Object> attributes; |
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.
Since it's intended for this to be immutable, let's make it final
.
import java.util.Map; | ||
|
||
/** | ||
* A domain object that wraps around the attributes in an {@link org.springframework.security.core.Authentication} |
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.
Let's mention OAuth 2.0 in this description. Maybe:
* A domain object that wraps around the attributes in an {@link org.springframework.security.core.Authentication} | |
* A domain object that wraps the attributes of an OAuth 2.0 token |
...auth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2TokenAttributes.java
Show resolved
Hide resolved
this.attributes.put(CLIENT_ID, "client_id"); | ||
this.attributes.put(USERNAME, "username"); | ||
} | ||
private Map<String, Object> attributesMap = new HashMap<String, Object>() {{ |
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.
While this is a clever construct for initializing a Map
, it's not used across the codebase. So, just for consistency, let's please still use the setUp
method.
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.
Sounds good, I didn't know @before would allow setUp
to populate the attributesMap
before the OAuth2TokenAttributes attributes
was made.
@@ -76,7 +75,7 @@ public void getNameWhenHasNoSubjectThenReturnsNull() { | |||
public void getNameWhenTokenHasUsernameThenReturnsUsernameAttribute() { | |||
OAuth2IntrospectionAuthenticationToken authenticated = | |||
new OAuth2IntrospectionAuthenticationToken(this.token, this.attributes, Collections.emptyList()); | |||
assertThat(authenticated.getName()).isEqualTo(this.attributes.get(SUBJECT)); | |||
assertThat(authenticated.getName()).isEqualTo(this.attributes.getAttributes().get(SUBJECT)); |
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 think you could call this.attributes.getAttribute(SUBJECT)
, right?
To simplify access to OAuth 2.0 token attributes Fixes spring-projectsgh-6498
e58b959
to
b4cf8d8
Compare
@jzheaux Thanks for the feedback, I've incorporated it into the PR. |
Looks great, @clementkng. The ticket outlines that the sample should also be updated - would you mind rolling that into this PR so that we can close out the ticket all at once? Placing that in a separate commit probably makes sense, just with the annotation "Issue: gh-6498" instead of "Fixes: gh-6498". |
@jzheaux I'm not sure how to update a ticket by rolling that into the PR. I assume you mean check off the ticket items, but how do I connect that to the PR via a commit? |
@clementkng, sorry, what I mean is just add a commit to this PR that updates the git add samples/
git commit
git push origin gh-6498-OAuth2TokenAttributes-class Where your commit message would be something like: Update Opaque Token Sample
Issue: gh-6948 |
@jzheaux Sorry, I think I misunderstood the item on that ticket. I've looked through the |
@clementkng no problem, sorry that's not so clear, let's see if I can help. So, we want to change this here: - public String index(@AuthenticationPrincipal(expression="['sub']") String subject) {
+ public String index(@AuthenticationPrincipal OAuth2TokenAttributes attributes) { |
@jzheaux Just to check my understanding, once we add the |
Correct, @clementkng |
@jzheaux Got it, there's currently an |
@jzheaux is there a way to |
Actually, I was able to figure out how to run the individual test. I've been able to narrow the issue down to the fact that the OAuth2TokenAttributes |
When you use Since that endpoint requires authentication, we know that it's because the principal is not actually of type So, it's probably best to check the |
@jzheaux Thanks for explaining! My understanding was that changing the constructor of Another thing I noticed was that the tests still pass w/ the old |
@clementkng, if you take a look at the The reason that the |
@jzheaux I see, thanks for pointing that out to me. I was able to update the sample. Please let me know if you have any other feedback! |
Thanks, @clementkng, this is now merged into |
@jzheaux Thanks for your help as well! When I first started out on this ticket, I had no idea how much the |
To simplify access to OAuth 2.0 token attributes
Fixes gh-6498