Skip to content

Commit 55bc64e

Browse files
authored
chore(apollo): Add details about authorization modes to Apollo AppSync README (#2912)
1 parent 7d2a98f commit 55bc64e

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

apollo/README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,86 @@ For applications using `apollo-appsync-amplify`, you can connect directly to you
4040
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
4141

4242
val apolloClient = ApolloClient.Builder()
43-
.appSync(connector.endpoint, connect.apiKeyAuthorizer())
43+
.appSync(connector.endpoint, connector.apiKeyAuthorizer())
4444
.build()
4545
```
4646

4747
Once you have constructed the Apollo client you can use it as normal for queries, mutations, and subscriptions to AppSync.
4848

49+
## Authorization Modes
50+
51+
AWS AppSync supports [five different authorization modes](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html):
52+
53+
- API Key
54+
- AWS Lambda Function
55+
- AWS IAM Permissions
56+
- OIDC Provider
57+
- Amazon Cognito User Pool
58+
59+
The Apollo AppSync Extensions libraries expose three authorizer types to support these different authorization modes.
60+
61+
### ApiKeyAuthorizer
62+
63+
An `ApiKeyAuthorizer` is used to provide a key for [API Key authorization](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#api-key-authorization) requests.
64+
65+
This Authorizer can be used with a hardcoded API key, by fetching the key from some source, or reading it from `amplify_outputs.json`:
66+
67+
```kotlin
68+
// Create an authorizer directly with your API key:
69+
val authorizer = ApiKeyAuthorizer("[YOUR_API_KEY")
70+
```
71+
```kotlin
72+
// Create an authorizer that fetches your API key. The fetching function may be called many times,
73+
// and should internally implement an appropriate caching mechanism.
74+
val authorizer = ApiKeyAuthorizer { fetchApiKey() }
75+
```
76+
```kotlin
77+
// Using ApolloAmplifyConnector to read API key from amplify_outputs.json
78+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
79+
val authorizer = connector.apiKeyAuthorizer()
80+
```
81+
82+
### AuthTokenAuthorizer
83+
84+
An `AuthTokenAuthorizer` sets an authentication header for use with [AWS Lambda](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#aws-lambda-authorization),
85+
[OIDC provider](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#openid-connect-authorization), and
86+
[Amazon Cognito User Pool](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#amazon-cognito-user-pools-authorization)
87+
authorization modes.
88+
89+
Using `ApolloAmplifyConnector` allows you to automatically authorize requests for the signed-in Amplify user, or you can implement the Authorizer's function parameter yourself to provide other types of tokens.
90+
91+
```kotlin
92+
// Provide a token from e.g. an OIDC provider. The fetching function may be called many times,
93+
// and should internally implement an appropriate caching mechanism.
94+
val authorizer = AuthTokenAuthorizer { fetchUserToken() }
95+
```
96+
```kotlin
97+
// Use ApolloAmplifyConnector to get Cognito tokens from Amplify for the signed-in user
98+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
99+
val authorizer = connector.authTokenAuthorizer()
100+
// or
101+
val authorizer = AuthTokenAuthorizer { ApolloAmplifyConnector.fetchLatestCognitoAuthToken() }
102+
```
103+
104+
### IamAuthorizer
105+
106+
An `IamAuthorizer` is used to provide request signature headers for using [AWS IAM-based authorization](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#aws-iam-authorization).
107+
108+
Using `ApolloAmplifyConnector` is the easiest way to use this authorizer, but you can also implement the signing function yourself, by e.g. delegating to the [AWS Kotlin SDK](https://github.com/awslabs/aws-sdk-kotlin).
109+
110+
```kotlin
111+
// Provide an implementation of the signing function. This function should implement the
112+
// AWS Sig-v4 signing logic and return the authorization headers containing the token and signature.
113+
val authorizer = IamAuthorizer { signRequestAndReturnHeaders(it) }
114+
```
115+
```kotlin
116+
// Use ApolloAmplifyConnector to sign the request
117+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
118+
val authorizer = connector.iamAuthorizer()
119+
// or supply a region to sign via the companion function
120+
val authorizer = IamAuthorizer { ApolloAmplifyConnector.signAppSyncRequest(it, "us-east-1") }
121+
```
122+
49123
## Contributing
50124

51125
- [CONTRIBUTING.md](../CONTRIBUTING.md)

0 commit comments

Comments
 (0)