aws-amplify / aws-amplify/amplify-data
Access token passed to appsync resolver causes missing email claim
- Dominant language
- TypeScript
- Stars
- 18
- Forks
- 23
- Avg merge
- 26m
- Merged PRs (30d)
- 1
Description
This is half bug, half feature request, but I have decided that it fits better as a "bug" due to reasons listed below.
**Describe the bug**
The issue is described [here](https://stackoverflow.com/questions/57127211/appsync-resolver-no-email-in-claim), but I will summarize it below.
When using Appsync with a cognito user pool with email enabled, $ctx.identity.claims.email is missing in the resolver for a mutation if that mutation is called using the amplify-js library. The issue does NOT exist when called using the query editor in Appsync itself.
This causes problems when using a cognito user pool where users sign up by email address rather than specifying a custom username. Since the email address is the human-readable identifying factor in an account, it is desired that it be stored on the profile level. However, in this use case, the $ctx.identity.username field is identical to the $ctx.identity.sub field (the account's UUID), so to obtain a user's email address without obtaining the entire profile, it must be passed via claims.
**To Reproduce**
Steps to reproduce the behavior:
1. Signin using cognito user pools via amplify-js
2. Call the generated graphql mutation function
3. In the resolver, check if $context.identity.claims.email is null (using $util.isNullOrEmpty())
4. Find that $context.identity.claims.email is null because is does not exist
**Expected behavior**
$context.identity.claims.email should be populated with the cognito user's email address
**Cause of issue**
The issue is caused by the passing of the Access Token as authorization, while as claims are supposed to be obtained from the ID Token. See [here](https://github.com/aws-amplify/amplify-js/blob/799a1cfe59f7244f1cc51bdc0220a6c4ebf81455/packages/api/src/API.ts#L301) (pointed out by cy6581 in the above stack overflow thread).
Using the Access token for claims is not in line with the cognito user pool documentation, which is why I classify this as a "bug". See [here](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html):
> The ID Token contains claims about the identity of the authenticated user such as name, email, and phone_number.
> The Access Token grants access to authorized resources.
Contributor guide
Research direction
Start with packages/api/src/API.ts around line 301 and trace which Cognito token amplify-js sends for generated GraphQL mutations. Reproduce the flow with a Cognito user pool and an AppSync resolver checking $context.identity.claims.email; it is done when the resolver receives the user's email claim during the library-triggered mutation, consistently with the AppSync query editor.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, graphql, typescript
- Domain
- api, authentication
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100