Use Cognito access token or id token to do request to secure API endpoints?

0

Hi!

Right now my frontend is storing the cognito generated access token to do request to my API endpoints and is working good. The thing is that I customized an attribute in Cognito containing the userId and that custom attribute is in the id token. I need that attribute to get the user data from some internal DB flows. Since the id token contains sensible data, should I use the id token to do request agains my API or continue using the access token? I need to get the userId from the token and I saw that I can create a Lambda pre authorization trigger to clean the id token and modify the attributes.

Is this a good approach? Or should I continue using the access token, get from that token the username and then with that username internally get the user id for some flows?

Thanks!

2 個答案
2
已接受的答案

For starters, you should definitely stick to using the access token for you're authorization needs against the API gateway. This is a best practice. The good news is you can now customize the access token using a Pre token generation Lambda trigger. This would allow you to add the necessary information to the access token. This feature is available with Cognito's advanced security features.

Otherwise, you could explore creating a backend mapping (ex: using DynamoDB) between the userId attribute (what you're ultimately needing) and the SUB of the user, which is present in the access token. Maintain the userId as the immutable identifier for the user for your solution and have an additional backend lookup to obtain the userId.

profile pictureAWS
已回答 4 個月前
profile picture
專家
已審閱 1 個月前
  • I see! Thank you man!

0

The ID token is meant for authentication. It contains claims about the identity of the authenticated user, such as user ID, username, and any custom attributes. The access token is meant for authorization. It grants the client application access to specific resources on behalf of the user. It is used to make requests to protected resources, such as API endpoints.

profile picture
Jagan
已回答 4 個月前
  • I see!

    Thank you man!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南