I am working on implementing oauth2 to secure web app that will call REST API as well as give access to other potential clients to access the same rest API. I would like to use role based access to control the data returned from the API.
I will use Keycloak as the Authorization server as well as for user / group management.
The use case is that
- I will create keycloak realm with public client (SPA) and confidential possibly bearer only client (REST API) and also groups and users who will be part of those groups
- User will login to the SPA via authorization flow and will receive an access token.
- The SPA will make the request (XHR) to the REST service passing the token as a Bearer token and retrieves the data or perform an operation that is permitted based on the group that the user is in.
I am trying to understand / best practice where would I should store the list of groups that the user is part of. Is it in the access token or in the ID token that can be retrieved and passed by the SPA and/or REST service would have to retrieve that data from the Authorization server using the access token and the userinfo endpoint. It seems that keycloak uses JWT for both access and ID token and roles / groups can be included in both. I read mixed suggestions that access token should not be read by the REST service and only used to prove that user is authenticated but then I see that it is used to pass user groups.
Another question that I have is that if I want to allow an automated client to access the REST API which won't be able to use authorization flow is it in the best practice to use client flow and on board that client in keycloak and provide the client clientId and secret to be able to retrieve access token use it to authenticate to the REST service (Bearer authentication header)
UPDATE
I have few more follow up questions to hopefully make it all clear.
As far as ID Token, I am thinking that the ID token is only should be used by the application (SPA) that is authenticating the user and will get information about the user (username, email, and few other things) based on the claims and user approving permissions. Possibly to display those things in the app. The ID token should not (never) be sent to the REST API to retrieve the data.
On the other hand Access Token should not be read the the application (SPA) but used in every request to the API server (Bearer $AUTH_TOKEN) with API server validating the token and then retrieving users's groups information and return allowed response.
What is still not clear is that if an application received an authorization token doesn't that mean the user is authenticated. Why do we need ID Token.
Also, if access token does not always carry information and could be just a random string then how would you know user's permissions. I was reading that there are two types of tokens “identifier type” and “self-contained type”. I am guessing that if the token is an identifier type then REST Service will have to send a request to the authorization server to get that information (groups/permissions) via retrospect api.
Found two good articles on this:
https://darutk.medium.com/oauth-access-token-implementation-30c2e8b90ff0.
https://darutk.medium.com/api-protection-by-id-token-3123481e96f2