Set Up SSO with Amazon Cognito
OpenID Connect (OIDC) is a simple identity layer on top of the OAuth 2.0 protocol. It allows clients to verify the identity of end users based on the authentication performed by the identity provider, as well as to obtain basic profile information about end users in an interoperable and REST-like manner.
Amazon Cognito, a fully managed service by AWS, simplifies the process of adding authentication and identity management to your applications. By leveraging Cognito for SSO, organizations can enhance security, streamline user experience, and reduce the burden of managing multiple credentials.
The guide will show you how to integrate APISIX with Amazon Cognito to implement authorization code grant and client credential grant.
Prerequisite(s)
- Install Docker.
- Install cURL to send requests to the services for validation.
- Follow the Getting Started tutorial to start a new APISIX instance in Docker.
- Create an AWS account.
Implement Authorization Code Grant
The authorization code grant is used by web and mobile applications. The flow starts by authorization server displaying a login page in browser where users could key in their credentials. During the process, a short-lived authorization code is exchanged for an access token, which APISIX stores in browser session cookies and will be sent with every request visiting the upstream resource server.
Create a User Pool and App Client
A user pool is a user directory where you can add more applications (app clients) and set up authentication, security, and an authentication UI.
Log in to AWS Console and navigate to the Amazon Cognito Service. Under user pools, click create user pool. The process will create a user pool as well as an application, which you can still update later. During the setup:
- Select traditional web applications as the application type.
- Enter an application name, for example
apisix-app
. - Under options for sign-in identifiers, check the email option.
- Under required attributes for sign-up, select email, family name, and given name. When users log in the first time, they will be prompted to change the password and provide the required attributes. Adjust accordingly per your use case.
- Under return URL, enter
http://localhost:9080/anything/callback
as the callback URL.
Note, that options for sign-in identifiers and required attributes cannot be changed after the app client is created.
Finish creating the user pool and the app client.
Obtain Integration Configurations
Navigate to the newly created user pool, and click Applications > App clients. Click into the app client and find the client ID and secret:
Scroll down to the quick setup guide, where you can find the issuer URL:

Save these information into environment variables:
# replace with your values
export COGNITO_CLIENT_ID=58qa9qdqcub787lf1ohq4fqn7f
export COGNITO_CLIENT_SECRET=ohtgsk1mkii53vs2m3f7l7ln05foktjf5jso8mce2alahnabpku
export COGNITO_ISSUER_URL=https://cognito-idp.ap-northeast-3.amazonaws.com/ap-northeast-3_mSfuhPzhm
export COGNITO_DISCOVERY="${COGNITO_ISSUER_URL}/.well-known/openid-configuration"
Create a User
Navigate into the user pool and under User management, select Users. Create a new user:
Enter email address, phone number, first-time password, and create the user:
You can create more users based on your user base.
Configure APISIX
In this section, you will create a route with OIDC that forwards client requests to httpbin.org, a public HTTP request and response service. The route /anything/{anything}
of httpbin.org
returns anything passed in request data in JSON type, such as methods, arguments, and headers.
Create a route with openid-connect
plugin as such:
- Admin API
- ADC
- Ingress Controller
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "auth-with-oidc",
"uri":"/anything/*",
"plugins": {
"openid-connect": {
"client_id": "'"$COGNITO_CLIENT_ID"'",
"client_secret": "'"$COGNITO_CLIENT_SECRET"'",
"discovery": "'"$COGNITO_DISCOVERY"'",
"scope": "openid email phone",
"redirect_uri": "http://localhost:9080/anything/callback"
}
},
"upstream": {
"type":"roundrobin",
"nodes": {
"httpbin.org:80":1
}
}
}'
❶ client_id
: Cognito client ID.
❷ client_secret
: Cognito client secret.
❸ discovery
: URI to the discovery document.
❹ scope
: define the scopes corresponding to the scopes defined in the app client. You can double check the scopes under your app client, Login pages tab.
❺ redirect_uri
: URI to redirect after authentication.
services:
- name: httpbin Service
routes:
- uris:
- /anything/*
name: auth-with-oidc
plugins:
openid-connect:
client_id: '58qa9qdqcub787lf1ohq4fqn7f'
client_secret: ohtgsk1mkii53vs2m3f7l7ln05foktjf5jso8mce2alahnabpku
discovery: https://cognito-idp.ap-northeast-3.amazonaws.com/ap-northeast-3_mSfuhPzhm/.well-known/openid-configuration
scope: openid email phone
redirect_uri: http://localhost:9080/anything/callback
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1