Skip to content

Commit 56b6bc0

Browse files
author
Dane Pilcher
authored
add guide to extend api key expiration and rotate api key (#8158)
* add guide to extend api key expiration and rotate key * add some addtional details
1 parent 475f4b5 commit 56b6bc0

File tree

1 file changed

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

1 file changed

+48
-2
lines changed

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

Lines changed: 48 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,52 @@ 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` days from the date when the app is deployed. In the example below, the API key will expire 7 days from the latest 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+
159+
A new API key will be created for your app.
160+
115161
## Add public authorization rule using Amazon Cognito identity pool's unauthenticated role
116162

117163
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 +228,7 @@ In your application, you can perform CRUD operations against the model with the
182228
try {
183229
final todo = Todo(content: 'My new todo');
184230
final request = ModelMutations.create(
185-
todo,
231+
todo,
186232
authorizationMode: APIAuthorizationType.iam,
187233
);
188234
final createdTodo = await Amplify.API.mutations(request: request).response;

0 commit comments

Comments
 (0)