Skip to content

Commit e2b26f2

Browse files
author
Dane Pilcher
committed
add guide to extend api key expiration and rotate key
1 parent 9751131 commit e2b26f2

File tree

1 file changed

+46
-2
lines changed
  • src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access

1 file changed

+46
-2
lines changed

src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ In your application, you can perform CRUD operations against the model by specif
7979
try {
8080
final todo = Todo(content: 'My new todo');
8181
final request = ModelMutations.create(
82-
todo,
82+
todo,
8383
authorizationMode: APIAuthorizationType.apiKey,
8484
);
8585
final createdTodo = await Amplify.API.mutations(request: request).response;
@@ -112,6 +112,50 @@ do {
112112

113113
</InlineFilter>
114114

115+
### Extend API Key Expiration
116+
117+
If the API key has not expired, you can extend the expiration date by deploying your app again. The API key expiration date will be set to `expiresInDays` from the current date when the app is deployed. In the example below, the API key will expire 7 days from the last deployment.
118+
119+
```ts title="amplify/data/resource.ts"
120+
export const data = defineData({
121+
schema,
122+
authorizationModes: {
123+
defaultAuthorizationMode: 'apiKey',
124+
apiKeyAuthorizationMode: {
125+
expiresInDays: 7,
126+
},
127+
},
128+
});
129+
```
130+
131+
### Rotate an API Key
132+
133+
You can rotate an API key if it was expired, compromised, or deleted. To rotate an API key, you can override the logical ID of the API key resource in the `amplify/backend.ts` file. This will create a new API key with a new logical ID.
134+
135+
```ts title="amplify/backend.ts"
136+
const backend = defineBackend({
137+
auth,
138+
data,
139+
});
140+
141+
backend.data.resources.cfnResources.cfnApiKey?.overrideLogicalId(
142+
`recoverApiKey${new Date().getTime()}`
143+
);
144+
```
145+
146+
Deploy your app. After the deploy has finished, remove the override to the logical ID and deploy your app again to use the default logical ID.
147+
148+
```ts title="amplify/backend.ts"
149+
const backend = defineBackend({
150+
auth,
151+
data,
152+
});
153+
154+
// backend.data.resources.cfnResources.cfnApiKey?.overrideLogicalId(
155+
// `recoverApiKey${new Date().getTime()}`
156+
// );
157+
```
158+
115159
## Add public authorization rule using Amazon Cognito identity pool's unauthenticated role
116160

117161
You can also override the authorization provider. In the example below, `identityPool` is specified as the provider which allows you to use an "Unauthenticated Role" from the Cognito identity pool for public access instead of an API key. Your Auth resources defined in `amplify/auth/resource.ts` generates scoped down IAM policies for the "Unauthenticated role" in the Cognito identity pool automatically.
@@ -182,7 +226,7 @@ In your application, you can perform CRUD operations against the model with the
182226
try {
183227
final todo = Todo(content: 'My new todo');
184228
final request = ModelMutations.create(
185-
todo,
229+
todo,
186230
authorizationMode: APIAuthorizationType.iam,
187231
);
188232
final createdTodo = await Amplify.API.mutations(request: request).response;

0 commit comments

Comments
 (0)