You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once you have constructed the Apollo client you can use it as normal for queries, mutations, and subscriptions to AppSync.
48
48
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") }
0 commit comments