OpenID Connect, which is built on top of OAuth 2.0, uses two types of tokens: an access_token and an id_token. Understanding the purpose of these two tokens can help you decide which claims or attributes to include in each.
Access Token: The access_token is intended to be used when making requests to the resource server (your API). It is meant to authorize requests, not to convey user identity information. That being said, it can contain scopes or roles (i.e., permissions) to help the resource server understand what the client (or the user on behalf of whom the client is acting) is allowed to do. For instance, you might include the role claim in the access token.
ID Token: The id_token is intended to convey user identity information to the client. It's provided to the client application by the authorization server, and it provides proof that the user has authenticated, along with basic profile information about the user. This is where you'd typically include claims like firstName, lastName, age, and gender.
So, following this logic, in your case:
- firstName, lastName, age, and gender should be included in the id_token because they're primarily related to user identity.
- role should be included in the access_token because it relates to what the client is allowed to do when making requests to your API on behalf of the user.
Of course, there might be exceptions based on specific requirements of your application. If, for example, your resource server (API) needs to know the age or gender of the user for some reason, you might include those claims in the access_token too.
Remember, one of the important principles of using these tokens is to minimize the amount of sensitive user information they carry. Always include only what is necessary for the operation of your application.