Send data securely to Amazon API Gateway using the OAuth Client Credential Flow with Cognito from Apple Shortcuts.
When building a shortcut in the Shortcuts app on an Apple device, it's sometimes necessary to offload processing or exchange data with a cloud provider such as Amazon Web Services (AWS).
This CloudFormation template provisions all the resources required to get started so that you can securely interact with AWS resources from your shortcuts via HTTP.
This pattern uses the OAuth Client Credentials Flow (defined in OAuth 2.0 RFC 6749, section 4.4) to securely exchange application credentials.
- User runs the shortcut.
- A HTTP POST request is sent to an Amazon Cognito User Pool /oauth2/token endpoint using the "Get contents of URL" action.
- The Cognito User Pool validates the application credentials and returns an access token to the shortcut.
- Another "Get contents of URL" action sends a HTTP request to the API Gateway REST API with the access token in the
Authorization
header in the formatBearer <ACCESS_TOKEN>
. - API Gateway sends a request to the Cognito User Pool to validate the token.
- Cognito User Pool confirms the token is valid and API Gateway proceeds with allowing the request through.
- API Gateway invokes Step Functions, using the HTTP request body as input for a state machine execution.
- The output from the state machine is returned to the shortcut.
You will need an AWS account and have the AWS CLI and AWS SAM CLI installed.
Remember to delete the stack when you're done as it will provision resources that start incurring costs.
-
Build the stack with
sam build
. -
Deploy the stack with
sam deploy
. -
Test getting an access token.
Replace the values surrounded by arrows as appropriate (you can get
CLIENT_SECRET
by running the command output by the stack output calledClientSecretCommand
):curl --location '<CLOUDFORMATION_OUTPUT:TokenUri>' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=client_credentials' \ --data-urlencode 'client_id=<CLOUDFORMATION_OUTPUT:ClientId>' \ --data-urlencode 'client_secret=<CLIENT_SECRET>' \ --data-urlencode 'scope=shortcuts-api-example/stepfunction:invoke'