Page updated Nov 9, 2023

Configure authorization modes

For client authorization AppSync supports API Keys, Amazon IAM credentials, Amazon Cognito User Pools, and 3rd party OIDC providers. This is inferred from the amplifyconfiguration.json/.dart file when you call Amplify.configure(). You can configure auth modes for an API using the Amplify CLI or manual configuration.

Auth Modes

API key

API Key is the easiest way to setup and prototype your application with AWS AppSync. This means it is also prone to abuse since anyone can easily discover the API Key and make requests to your public service. To have authorization checks, use the other auth modes such as Cognito user pool or AWS IAM. API Key will expiry according to the expiry time set when provisioning AWS AppSync and will require extending it or creating a new one if needed. Default API Key expiry time is 7 days.

Amazon Cognito User Pools

Amazon Cognito User Pools is most commonly used with AWS AppSync when adding authorization check on your API calls. If your application needs to interact with other AWS services besides AWS AppSync, such as Amazon S3, you will need to use AWS IAM credentials with Amazon Cognito Identity Pools. Amplify CLI can automatically configure this for you and will also automatically use the authenticated user from User Pools to federate with the Identity Pools to provide the AWS IAM credentials in the application. See this for more information about the differences. This allows you to have both User Pools' credentials for AWS AppSync and AWS IAM credentials for other AWS resources. You can learn more about Amplify Auth outlined in the Accessing credentials section.

IAM

Amazon Cognito Identity Pools allows you to use credentials from AWS IAM in your app. AWS IAM helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use AWS resources. Learn more about IAM . The Amplify CLI can automatically configure this for you.

OpenID Connect (OIDC)

If you are using a 3rd party OIDC provider you will need to configure it and manage the details of token refreshes yourself.

AWS Lambda

You can implement your own custom API authorization logic using an AWS Lambda function. To add a Lambda function as an authorization mode for your AppSync API, go to the Settings section of the AppSync console.

You will need to manage the details of token refreshes in your application code yourself.

Use Amplify CLI to configure authorization modes

Amplify CLI can automatically configure the auth modes for you when running amplify add api or amplify update api if you want to change the auth mode.

If you already have auth configured, then you need to run amplify update api to use this pre-configured auth mode and CLI will not ask for auth settings again.

1amplify update api
1? Please select from one of the below mentioned services:
2 > `GraphQL`
3? Select a setting to edit:
4 > `Authorization modes`
5? Choose the default authorization type for the API
6 API key
7 Amazon Cognito User Pool
8❯ IAM
9 OpenID Connect

Manual Configuration

API Key

Add the following snippet to your amplifyconfiguration.json/.dart file, under the awsAPIPlugin:

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "API_KEY",
9 "apiKey": "[API-KEY]"
10 }
11 }
12}

Amazon Cognito User Pools

Add the following snippet to your amplifyconfiguration.json/.dart file, under the awsCognitoAuthPlugin:

1{
2 ...
3 "awsCognitoAuthPlugin": {
4 "CognitoUserPool": {
5 "Default": {
6 "PoolId": "[POOL-ID]",
7 "AppClientId": "[APP-CLIENT-ID]",
8 "Region": "[REGION]"
9 }
10 }
11 }
12}

and under the awsAPIPlugin

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "AMAZON_COGNITO_USER_POOLS",
9 }
10 }
11}

Add the following code to your app:

1Amplify.addPlugin(new AWSCognitoAuthPlugin());
2Amplify.addPlugin(new AWSApiPlugin());
1Amplify.addPlugin(AWSCognitoAuthPlugin())
2Amplify.addPlugin(AWSApiPlugin())

IAM

Add the following snippet to your amplifyconfiguration.json/.dart file:

1{
2 ...
3 "awsCognitoAuthPlugin": {
4 "CredentialsProvider": {
5 "CognitoIdentity": {
6 "Default": {
7 "PoolId": "[COGNITO-IDENTITY-POOLID]",
8 "Region": "[REGION]"
9 }
10 }
11 }
12 }
13}

and under the awsAPIPlugin

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "AWS_IAM",
9 }
10 }
11}

OIDC

Update the amplifyconfiguration.json/.dart file and code snippet as follows:

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "OPENID_CONNECT",
9 }
10 }
11}

Add the following code to your app:

1ApiAuthProviders authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider(() -> "[OPEN-ID-CONNECT-TOKEN]")
3 .build();
4AWSApiPlugin plugin = AWSApiPlugin.builder()
5 .apiAuthProviders(authProviders)
6 .build();
7Amplify.addPlugin(plugin);
1val authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider { "[OPEN-ID-CONNECT-TOKEN]" }
3 .build()
4val plugin = AWSApiPlugin.builder()
5 .apiAuthProviders(authProviders)
6 .build()
7Amplify.addPlugin(plugin)

If you are using Cognito's user pool as the authorization type, this will by default retrieve and use the Access Token for your requests. If you would like to override this behavior and use the ID Token instead, you can treat Cognito user pool as your OIDC provider and use Amplify.Auth to retrieve the ID Token for your requests.

Add the following code to your app to initialize API plugin with OIDC auth provider:

This implementation uses CompletableFuture<T>, which requires minSdkVersion >= 24.

1ApiAuthProviders authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider(() -> {
3 CompletableFuture<String> future = new CompletableFuture<>();
4 Amplify.Auth.fetchAuthSession(
5 session -> future.complete(((AWSCognitoAuthSession) session)
6 .getUserPoolTokens()
7 .getValue()
8 .getIdToken()),
9 future::completeExceptionally
10 );
11 try {
12 return future.get();
13 } catch (Exception e) {
14 e.printStackTrace();
15 }
16 return null;
17 })
18 .build();
19AWSApiPlugin plugin = AWSApiPlugin.builder()
20 .apiAuthProviders(authProviders)
21 .build();
22Amplify.addPlugin(plugin);

This implementation uses CompletableFuture<T>, which requires minSdkVersion >= 24.

1val authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider {
3 val future = CompletableFuture<String>()
4 Amplify.Auth.fetchAuthSession(
5 { future.complete((it as AWSCognitoAuthSession).userPoolTokens.value?.idToken) },
6 { future.completeExceptionally(it) }
7 )
8 future.get()
9 }
10 .build()
11val plugin = AWSApiPlugin.builder()
12 .apiAuthProviders(authProviders)
13 .build()
14Amplify.addPlugin(plugin)

This implementation uses CompletableFuture<T>, which requires minSdkVersion >= 24.

1val authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider {
3 val session = runBlocking { Amplify.Auth.fetchAuthSession() }
4 return (session as AWSCognitoAuthSession).userPoolTokens.value?.idToken
5 }
6 .build()
7val plugin = AWSApiPlugin.builder()
8 .apiAuthProviders(authProviders)
9 .build()
10Amplify.addPlugin(plugin)

Using the rxbindings module can simplify this further.

1dependencies {
2 // other dependencies...
3 implementation 'com.amplifyframework:rxbindings:ANDROID_VERSION'
4}
1ApiAuthProviders authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider(() -> RxAmplify.Auth.fetchAuthSession()
3 .map(session -> ((AWSCognitoAuthSession) session)
4 .getUserPoolTokens()
5 .getValue()
6 .getIdToken())
7 .blockingGet())
8 .build();
9AWSApiPlugin plugin = AWSApiPlugin.builder()
10 .apiAuthProviders(authProviders)
11 .build();
12Amplify.addPlugin(plugin);

Using the rxbindings module can simplify this further.

1dependencies {
2 // other dependencies...
3 implementation 'com.amplifyframework:rxbindings:ANDROID_VERSION'
4}
1val authProviders = ApiAuthProviders.builder()
2 .oidcAuthProvider { RxAmplify.Auth.fetchAuthSession()
3 .map { (it as AWSCognitoAuthSession)
4 .userPoolTokens
5 .value
6 ?.idToken }
7 .blockingGet() }
8 .build()
9val plugin = AWSApiPlugin.builder()
10 .apiAuthProviders(authProviders)
11 .build()
12Amplify.addPlugin(plugin)

AWS Lambda

Amplify CLI does not currently allow you to configure Lambda as an authorization mode for your GraphQL API. To add a Lambda function as an authorization mode for your AppSync API, go to the Settings section of the AppSync console. Then, update the authorizationType value in the amplifyconfiguration.json/.dart file and code snippet as follows:

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "AWS_LAMBDA",
9 }
10 }
11}

Add the following code to your app:

1ApiAuthProviders authProviders = ApiAuthProviders.builder()
2 .functionAuthProvider(() -> "[AWS-LAMBDA-AUTH-TOKEN]")
3 .build();
4AWSApiPlugin plugin = AWSApiPlugin.builder()
5 .apiAuthProviders(authProviders)
6 .build();
7Amplify.addPlugin(plugin);
1val authProviders = ApiAuthProviders.builder()
2 .functionAuthProvider { "[AWS-LAMBDA-AUTH-TOKEN]" }
3 .build()
4val plugin = AWSApiPlugin.builder()
5 .apiAuthProviders(authProviders)
6 .build()
7Amplify.addPlugin(plugin)

NONE

You can also set authorization mode to NONE so that the library will not provide any request interception logic. You can use this when your API does not require any authorization or when you want to manipulate the request yourself, such as adding header values or authorization data.

1{
2 ...
3 "awsAPIPlugin": {
4 "[YOUR-GRAPHQLENDPOINT-NAME]": {
5 "endpointType": "GraphQL",
6 "endpoint": "[GRAPHQL-ENDPOINT]",
7 "region": "[REGION]",
8 "authorizationType": "NONE",
9 }
10 }
11}

You can register your own request interceptor to intercept the request and perform an action or inject something into your request before it is performed.

To specify your own headers, use the configureClient() configuration option on the AWSApiPlugin's builder. Specify the name of one of the configured APIs in your amplifyconfiguration.json. Apply customizations to the underlying OkHttp instance by providing a lambda expression as below.

1AWSApiPlugin plugin = AWSApiPlugin.builder()
2 .configureClient("yourApiName", okHttpBuilder -> {
3 okHttpBuilder.addInterceptor(chain -> {
4 Request originalRequest = chain.request();
5 Request updatedRequest = originalRequest.newBuilder()
6 .addHeader("customHeader", "someValue")
7 .build();
8 return chain.proceed(updatedRequest);
9 });
10 })
11 .build();
12Amplify.addPlugin(plugin);
1val plugin = AWSApiPlugin.builder()
2 .configureClient("yourApiName") { okHttpBuilder ->
3 okHttpBuilder.addInterceptor { chain ->
4 val originalRequest = chain.request()
5 val updatedRequest = originalRequest.newBuilder()
6 .addHeader("customHeader", "someValue")
7 .build()
8 chain.proceed(updatedRequest)
9 }
10 }
11 .build()
12Amplify.addPlugin(plugin)
1val plugin = AWSApiPlugin.builder()
2 .configureClient("yourApiName") { okHttpBuilder ->
3 okHttpBuilder.addInterceptor { chain ->
4 val originalRequest = chain.request()
5 val updatedRequest = originalRequest.newBuilder()
6 .addHeader("customHeader", "someValue")
7 .build()
8 chain.proceed(updatedRequest)
9 }
10 }
11 .build()
12Amplify.addPlugin(plugin)
1AWSApiPlugin plugin = AWSApiPlugin.builder()
2 .configureClient("yourApiName", okHttpBuilder -> {
3 okHttpBuilder.addInterceptor(chain -> {
4 Request originalRequest = chain.request();
5 Request updatedRequest = originalRequest.newBuilder()
6 .addHeader("customHeader", "someValue")
7 .build();
8 return chain.proceed(updatedRequest);
9 });
10 })
11 .build();
12RxAmplify.addPlugin(plugin);

Configure multiple authorization modes

There is currently a known issue where enabling multi-auth modes for the API plugin may break DataStore functionality if the DataStore plugin is also used in the same App.

If you are planning to enable multi-auth for only the DataStore plugin, please refer to the configure multiple authorization types section in DataStore documentation.

If you are planning to enable multi-auth for both the API and DataStore plugins, please monitor the aforementioned issue for the latest progress and updates.

This section talks about the capability of AWS AppSync to configure multiple authorization modes for a single AWS AppSync endpoint and region. Follow the AWS AppSync Multi-Auth to configure multiple authorization modes for your AWS AppSync endpoint.

You can now configure a single GraphQL API to deliver private and public data. Private data requires authenticated access using authorization mechanisms such as IAM, Cognito User Pools, and OIDC. Public data does not require authenticated access and is delivered through authorization mechanisms such as API Keys. You can also configure a single GraphQL API to deliver private data using more than one authorization type. For example, you can configure your GraphQL API to authorize some schema fields using OIDC, while other schema fields through Cognito User Pools and/or IAM.

As discussed in the above linked documentation, certain fields may be protected by different authorization types. This can lead the same query, mutation, or subscription to have different responses based on the authorization sent with the request; Therefore, it is recommended to use the different friendly_name_<AuthMode> as the apiName parameter in the Amplify.API call to reference each authorization type.

The following snippets highlight the new values in the amplifyconfiguration.json/.dart and the client code configurations.

The friendly_name illustrated here is created from Amplify CLI prompt. There are 4 clients in this configuration that connect to the same API except that they use different AuthMode.

1{
2 "UserAgent": "aws-amplify-cli/2.0",
3 "Version": "1.0",
4 "api": {
5 "plugins": {
6 "awsAPIPlugin": {
7 "[FRIENDLY-NAME-API-WITH-API-KEY]": {
8 "endpointType": "GraphQL",
9 "endpoint": "[GRAPHQL-ENDPOINT]",
10 "region": "[REGION]",
11 "authorizationType": "API_KEY",
12 "apiKey": "[API_KEY]"
13 },
14 "[FRIENDLY-NAME-API-WITH-IAM]": {
15 "endpointType": "GraphQL",
16 "endpoint": "[GRAPHQL-ENDPOINT]",
17 "region": "[REGION]",
18 "authorizationType": "AWS_IAM"
19 },
20 "[FRIENDLY-NAME-API-WITH-USER-POOLS]": {
21 "endpointType": "GraphQL",
22 "endpoint": "https://xyz.appsync-api.us-west-2.amazonaws.com/graphql",
23 "region": "[REGION]",
24 "authorizationType": "AMAZON_COGNITO_USER_POOLS"
25 },
26 "[FRIENDLY-NAME-API-WITH-OPENID-CONNECT]": {
27 "endpointType": "GraphQL",
28 "endpoint": "https://xyz.appsync-api.us-west-2.amazonaws.com/graphql",
29 "region": "[REGION]",
30 "authorizationType": "OPENID_CONNECT"
31 }
32 }
33 }
34 }
35}

The GRAPHQL-ENDPOINT from AWS AppSync will look similar to https://xyz.appsync-api.us-west-2.amazonaws.com/graphql.

When you have configured multiple APIs, you can specify the name of the API as a parameter as the target for an operation:

1Amplify.API.mutate(
2 "[FRIENDLY-NAME-API-WITH-API-KEY]",
3 request,
4 response -> Log.i("MyAmplifyApp", "Mutation successful"),
5 error -> Log.e("MyAmplifyApp", "Failed to mutate model.", error)
6);
1Amplify.API.mutate(
2 "[FRIENDLY-NAME-API-WITH-API-KEY]",
3 request,
4 { Log.i("MyAmplifyApp", "Mutation successful") },
5 { Log.e("MyAmplifyApp", "Failed to mutate model.", it) }
6)
1try {
2 val apiName = "[FRIENDLY-NAME-API-WITH-API-KEY]"
3 val response = Amplify.API.mutate(request, apiName)
4 Log.i("MyAmplifyApp", "Mutation successful")
5} catch (error: ApiException) {
6 Log.e("MyAmplifyApp", "Failed to mutate model.", error)
7}