Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated Jun 20, 2024

カスタム識別情報およびグループクレームの設定

Amplify Dataは、デフォルトで提供されるAmazon Cognito の cognito:groups またはダブルコロン区切りのクレーム sub::username をJWTトークンから使用したくない場合に、カスタム識別情報およびグループクレームの使用をサポートしています。これは、サードパーティのOIDCシステムからのトークンを使用している場合、または事前トークン生成Lambdaトリガーを使用している場合など、データベースから読み込むグループのリストで外部システムからクレームを入力したい場合に便利です。

カスタムクレームを使用するには、必要に応じて identityClaim または groupClaim を指定します。以下の例では、identityClaim が指定され、レコードオーナーは この user_id クレームに対してチェックされます。同様に、user_groups クレームに「Moderator」文字列が含まれている場合、アクセスが許可されます。

amplify/data/resource.ts
import { a, defineData, type ClientSchema } from '@aws-amplify/backend';
const schema = a.schema({
Post: a
.model({
id: a.id(),
owner: a.string(),
postname: a.string(),
content: a.string(),
})
.authorization(allow => [
allow.owner().identityClaim('user_id'),
allow.groups(['Moderator']).withClaimIn('user_groups'),
]),
});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({ schema });

アプリケーション内では、userPools認証モードでモデルに対するCRUD操作を実行できます。

try {
final todo = Todo(content: 'My new todo');
final request = ModelMutations.create(
todo,
authorizationMode: APIAuthorizationType.userPools,
);
final createdTodo = await Amplify.API.mutations(request: request).response;
if (createdTodo == null) {
safePrint('errors: ${response.errors}');
return;
}
safePrint('Mutation result: ${createdTodo.name}');
} on APIException catch (e) {
safePrint('Failed to create todo', e);
}