Identity and Access Management · Topic 2 of 6
Cognito and Application Authentication
Understand Cognito user pools and identity pools, token types, MFA and federation, and how apps authenticate customers and get temporary AWS credentials.
- 6 min
- Includes a diagram
- A user pool authenticates and issues JWTs. An identity pool exchanges identities for temporary AWS credentials.
- The three tokens are ID, access and refresh. APIs should be authorised with the access token.
- Users can sign in locally or through social, SAML or OIDC federation in a user pool.
What it is
Amazon Cognito provides sign up, sign in and access control for the users of your web and mobile applications. It is the AWS answer when the people signing in are customers, not employees.
Cognito has two parts that people often confuse. A user pool is a user directory that authenticates users and issues tokens. An identity pool exchanges identities for temporary AWS credentials.
How it works
User pools
A user pool stores users or federates with external providers. After a successful sign in it returns three JSON Web Tokens (JWTs).
| Token | Purpose |
|---|---|
| ID token | Claims about the user, such as name and email, for the application |
| Access token | Authorises calls to APIs and to the user pool's own APIs |
| Refresh token | Obtains new ID and access tokens without a new sign in |
Features that matter for the exam:
- Hosted UI and managed login. A ready sign in page that supports OAuth 2.0 flows, including the authorization code grant.
- Federation. Sign in with social providers (such as Google or Apple), SAML identity providers and OIDC providers.
- MFA. SMS messages and TOTP (time based one time password) authenticator apps. MFA can be optional or required. Newer feature plans also add passwordless sign in with passkeys.
- Adaptive authentication. The advanced security (threat protection) features detect risky sign ins, such as compromised credentials or unusual locations, and can require MFA or block them.
- Lambda triggers. Run code at stages such as pre sign up, pre token generation and custom challenge, to add rules or custom claims.
- Groups. Assign users to groups, and the group appears in the token, which an authorizer or application can use.
- Password and account policy. Control password strength, verification and recovery.
Identity pools
An identity pool (federated identities) takes a proof of identity and returns temporary AWS credentials from STS. The proof can be a user pool token, a social token, a SAML or OIDC token, or nothing at all for guest access.
You define an IAM role for authenticated users and another for guests. Role selection can also map from token claims or groups to different roles.
Use an identity pool when the client app must call an AWS service directly, such as uploading to S3 from a mobile app. Scope the role tightly, and use the identity ID in the policy so each user reaches only their own prefix.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/${cognito-identity.amazonaws.com:sub}/*"
}]
}
The variable expands to the caller's identity ID, so every user is confined to a personal folder.
Using Cognito with other services
| Integration | How it works |
|---|---|
| API Gateway | A Cognito authorizer or JWT authorizer validates the token before the call reaches the backend |
| Application Load Balancer | Authenticates users with a user pool before forwarding, and passes claims in headers |
| AWS WAF | Can be attached to a user pool to protect the hosted sign in |
| AppSync | Uses the user pool as an authorization mode |
Choosing between the parts
| Need | Choose |
|---|---|
| Sign up, sign in and tokens for app users | User pool |
| Mobile app must call AWS services directly with scoped credentials | Identity pool |
| Social or SAML sign in for customers | User pool federation |
| Guest access to a limited resource | Identity pool unauthenticated role |
| Employees accessing AWS accounts | IAM Identity Center, not Cognito |
Token validation
A backend must verify tokens: the signature using the user pool's published keys, the issuer, the audience or client ID, the expiry and the token use (ID or access). API Gateway and ALB do this for you. A custom service must do it itself and must never trust claims from an unverified token.
Machine to machine access
For service to service calls, a user pool resource server can issue access tokens using the OAuth 2.0 client credentials grant, with custom scopes. This avoids creating fake users for backend clients.
Security hardening
Protect user pools with MFA, advanced security features, WAF rate limits, short token lifetimes and the revocation of refresh tokens when a user signs out or is compromised. Use a custom domain with a certificate for the hosted UI, and keep client secrets out of mobile apps, since public clients cannot keep secrets.
What to remember for the exam
- A user pool authenticates and issues JWTs. An identity pool exchanges identities for temporary AWS credentials.
- The three tokens are ID, access and refresh. APIs should be authorised with the access token.
- Users can sign in locally or through social, SAML or OIDC federation in a user pool.
- Adaptive authentication and MFA reduce account takeover risk.
- Lambda triggers customise the sign in flow and token contents.
- Identity pools can serve guests with a separate unauthenticated role, and map groups or claims to roles.
- Use
${cognito-identity.amazonaws.com:sub}in policies to give each user a private prefix. - API Gateway and ALB can validate Cognito tokens without custom code.
"A mobile app must let each signed in user upload to their own S3 folder" points to a user pool for sign in plus an identity pool with a role whose policy uses the identity ID variable.
Common mistakes
Mixing up the pools. A user pool returns tokens and an identity pool returns AWS credentials, so a question about credentials needs the identity pool.
Giving the authenticated role broad access. Every signed in user then shares the same wide permissions, so scope by identity ID or claim.
Trusting the ID token for API authorisation. Use the access token and its scopes for API access.
Putting a client secret in a mobile app. A public client cannot protect a secret, so use a public app client without one.
Enabling guest access in an identity pool lets anyone obtain AWS credentials, so make the unauthenticated role extremely limited or turn guest access off.
Check yourself
Which Cognito component returns temporary AWS credentials?
An identity pool, which exchanges a user pool token or other identity proof for STS credentials.
Which token should an API use for authorisation?
The access token, since it carries scopes and groups for authorisation, while the ID token describes the user.
How can an identity pool policy limit each user to their own S3 prefix?
Use the
${cognito-identity.amazonaws.com:sub}policy variable in the resource path.
Topic 2 of 6 in Identity and Access Management
Test yourself on Identity and Access Management
The full AWS SCS-C03 question bank has 858 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.