Lesson 2 of 4 Identity Federation and Temporary Credentials

Securing Customer Sign-In with Amazon Cognito

This lesson explains how Amazon Cognito handles authentication and authorisation for the end users, or customers, of web and mobile applications.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish user pools from identity pools.
  • Describe Cognito threat protection against credential stuffing and account takeover.
  • Describe how Cognito tokens integrate with other AWS services.
  • Scope identity pool credentials to each user's own S3 folder.

User pools and identity pools

A user pool is a user directory for an application. It handles sign-up and sign-in and issues JSON Web Tokens (JWTs): an ID token describing the user, an access token for calling APIs, and a refresh token. It provides password policies, MFA by authenticator app, SMS or email, passwordless sign-in and passkeys, sign-in through social providers and SAML or OIDC identity providers, Lambda triggers to customise each step, and managed login pages.

An identity pool does something different: it exchanges a token, from a user pool or another provider, for temporary AWS credentials for an IAM role, so the application can call AWS services such as S3 directly. It supports authenticated and guest roles, chooses roles from token claims, and supports policy variables such as ${cognito-identity.amazonaws.com:sub}, which restricts each user to their own S3 prefix. Many applications use both. The following table summarises the differences.

User pools
PurposeUser directory: sign-up and sign-in
IssuesJWTs: ID, access and refresh tokens
FeaturesPassword policy, MFA (TOTP, SMS, email), passwordless and passkeys, social and SAML/OIDC federation, Lambda triggers, managed login
Identity pools
PurposeExchange tokens for temporary AWS credentials
IssuesIAM role credentials
FeaturesAuthenticated and guest roles, role mapping by claims, policy variables like ${cognito-identity.amazonaws.com:sub} for per-user S3 prefixes

The following diagram follows a mobile app user from sign-in, through a Cognito identity pool, to the user's own S3 prefix.

Direct uploads from a mobile app with a Cognito identity pool. The pool exchanges the sign-in token for temporary credentials of the authenticated role, whose policy uses the identity ID as a variable, so each user reaches only private/<their ID>/. The trust policy checks the pool (aud) and authenticated (amr).
The scenario is illustrative.
Sort into groups

User pool or identity pool?

Exam skills 4.1.1 · 4.1.2

Decide which Cognito component each statement describes.

Log in to try this activity Interactive activities are free with an account.

Threat protection

Sign-in pages are targets for credential stuffing, in which attackers try usernames and passwords leaked from other sites, and for account takeover. Cognito threat protection, available in the Plus feature tier, evaluates each sign-in for risk, based on factors such as unusual devices, locations and IP addresses, and checks whether the credentials appear in known leaks. Depending on the configured response, it can allow the sign-in, require MFA (adaptive authentication), block the attempt or notify the user. It can run in audit-only mode first, so the effect of the rules can be assessed before they are enforced.

Integrations

Cognito issues standard JSON Web Tokens (JWTs) to signed-in users: an ID token describing the user, and an access token describing what the user may do. Other AWS services can accept these tokens directly, so applications do not need to implement token validation themselves, and identity pools can exchange them for temporary AWS credentials. The following list describes these integrations.

  • API Gateway and ALBs validate Cognito tokens directly.
  • AWS WAF protects user pool endpoints.
  • Verified Permissions uses Cognito as an identity source for fine-grained authorisation.
Match the pairs

Services that use Cognito tokens

Exam skills 4.1.1

Match each service with how it works with Cognito.

Log in to try this activity Interactive activities are free with an account.
Worked example

Per-user folders in S3 for a mobile app

A mobile app lets users upload photos directly to S3 without a back end in the path. A Cognito identity pool exchanges the user's sign-in token for temporary credentials of an authenticated role, whose policy uses the identity ID as a variable so each user can reach only their own prefix:

{
  "Version": "2012-10-17",
  "Statement": [
    { "Effect": "Allow", "Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
      "Resource": "arn:aws:s3:::acme-photos/private/${cognito-identity.amazonaws.com:sub}/*" },
    { "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::acme-photos",
      "Condition": { "StringLike": { "s3:prefix": "private/${cognito-identity.amazonaws.com:sub}/*" } } }
  ]
}

The role's trust policy must restrict cognito-identity.amazonaws.com:aud to this identity pool and require amr to be authenticated; otherwise another pool's users could assume it. Threat protection on the user pool adds checks for compromised credentials at sign-in.

Check yourself

How can sign-ins with leaked passwords be detected?

Show answer

With Cognito threat protection.

Exam Tips

Remember for the exam

  • When application users sign in with Google or a corporate SAML identity provider, use Cognito user pool federation.
  • When a mobile app uploads directly to S3 with per-user access, use a Cognito identity pool with policy variables.
  • To detect sign-ins with leaked passwords, use Cognito threat protection.
  • To protect an API with user pool tokens, use an API Gateway Cognito authorizer.
  • User pools issue JWTs; identity pools issue AWS credentials.