I am going through this api doc (source) of Spring OAuth2 ClientDetails Interface. And I am not sure how this i.e clientDetails.getAuthorities() works or helps in the authorization flow. The API docs describes it as below:
Collection<org.springframework.security.core.GrantedAuthority> getAuthorities()
Returns the authorities that are granted to the OAuth client. Cannot return null. Note that these are NOT the authorities that are granted to the user with an authorized access token. Instead, these authorities are inherent to the client itself.
Returns:
the authorities (never null)
And I understand the above is different from the userDetails.getAuthorities() of UserDetails Interface. The API docs says as below:
java.util.Collection<? extends GrantedAuthority> getAuthorities()
Returns the authorities granted to the user. Cannot return null.
Returns:
the authorities, sorted by natural key (never null)
I understand that, in ClientDetails we are dealing with granting permission to the OAuth2 Client, while in UserDetails we are granting the permission to User i.e Principal.
But how is the former different from later and when to use the former.
A little elaboration -
When dealing with OAuth, we are dealing with tokens issued by Auth Server. The tokens are typically jwt tokens with information about the authorities granted to the User. A typical base64 decoded token's payload might look like below:
{
"app-userId": "c54a-4140-9fa0-0f39",
"user_name": "abc@xyz.com",
"scope": [
"all"
],
"exp": 1656929583,
"authorities": [
"app1_viewer",
"app1_modifier",
"app2_viewer",
"app2_blog_creator],
"client_id": "client_A"
...
}
The authorities above are the authorities of the User who is logged-in and using the app.
- When the user accesses app1 - it checks the above jwt token to make sure the user has the required permissions/authorities.
- If app1 has to call app2, then app1 passes the above jwt which is specific to current user (as Authorization header) to app2. App2 processes this jwt token received in headers and checks if the user has permissions to access/edit the resource. And returns 200 or 403 accordingly.
As you can see, the process is dealing with userDetails.getAuthorities(). So, how and where or in which case does clientDetails.getAuthorities() help. What are the use cases/examples of it.
Could some one explain. Thanks for any answers.