Page updated Jan 16, 2024

Set up authorization rules

Amplify gives you the ability to limit which individuals or groups should have access to create, read, update, or delete data on your types by specifying an @auth directive.

Here's a high-level overview of the authorization scenarios we support in the Amplify libraries. Each scenario has options you can tune to fit the needs of your application.

DataStore only supports authorization rules specified on this page.

The links provided below to the CLI documentation for specific authorization rules only apply to those particular rules. Datastore only supports a subset of all possible authorization rules provided by the CLI. Therefore, the other authorization rules or setups described in the linked page do not apply to Datastore.

If your app uses authentication, it is recommended to call DataStore.clear() on sign-in or sign-out to remove any user-specific data. In scenarios where a mobile device can be shared by several users, calling DataStore.clear() will ensure that data does not leak from one user to another.

Commonly used @auth rule patterns

Per User / Owner Based Data Access

The following are commonly used patterns for owner based authorization. For more information on how to fine tune these examples, please see the CLI documentation on owner based authorization.

  • Create/Read/Update/Delete mutations are private to the owner.
1type YourModel @model @auth(rules: [{ allow: owner }]) {
2 ...
3}
  • Owners can create and delete; other signed-in users can read and update.
1type YourModel
2 @model
3 @auth(
4 rules: [
5 { allow: owner, operations: [create, delete] }
6 { allow: private, operations: [read, update] }
7 ]
8 ) {
9 ...
10}

Static Group Authorization

The following are commonly used patterns for static group authorization. For more information on how to fine tune these examples, please see the CLI documentation on static group authorization.

  • Users belonging to the "Admin" group can CRUD (create, read, update, and delete), others cannot access anything.
1type YourModel @model @auth(rules: [{ allow: groups,
2 groups: ["Admin"] }]) {
3 ...
4}
  • Users belonging to the "Admin" group can create and delete, other signed users can read and update.
1type YourModel
2 @model
3 @auth(
4 rules: [
5 { allow: groups, groups: ["Admin"], operations: [create, delete] }
6 { allow: private, operations: [read, update] }
7 ]
8 ) {
9 ...
10}

Owner and Static Group Combined

The following are commonly used patterns for combining owner and static group authorization. For more information on how to fine tune these examples, please see the CLI documentation on static group authorization.

  • Users have their own data, but users who belong to the Admin group have access to their data and anyone else in that group. Users in the Admin group have the ability to make mutation on behalf of users not in the Admin group
1type YourModel
2 @model
3 @auth(rules: [{ allow: owner }, { allow: groups, groups: ["Admin"] }]) {
4 ...
5}

Public Data Access

The following are commonly used patterns to grant everyone access. For more information on how to fine tune these examples, please see the CLI documentation on public data access.

  • Auth provider is API Key
1type YourModel @model @auth(rules: [{ allow: public }]) {
2 ...
3}
  • Auth provider is IAM
1type YourModel @model @auth(rules: [{ allow: public, provider: iam }]) {
2 ...
3}

Signed-in User Data Access

The following are commonly used patterns for private authorization. For more information on how to fine tune these examples, please see the CLI documentation on signed-in user data access.

  • Cognito user pool authenticated users can CRUD all posts, regardless of who created it. Guest users do not have access.
1type YourModel @model @auth(rules: [{ allow: private }]) {
2 ...
3}
  • IAM authenticated users can CRUD all posts, regardless of who created it. Guest users do not have access:
1type YourModel @model @auth(rules: [{ allow: private, provider: iam }]) {
2 ...
3}

Owner based Authorization with OIDC provider

The following are commonly used patterns for owner based authorization using a 3rd party OIDC provider (e.g. Facebook, Google, etc...). For more information on how to fine tune these examples, please see the CLI documentation on using an oidc authorization provider.

  • Using a 3rd party OIDC provider to achieve owner based authorization.
1type YourModel
2 @model
3 @auth(rules: [{ allow: owner, provider: oidc, identityClaim: "sub" }]) {
4 ...
5}

When using a third-party OIDC auth provider, you are required to subclass OIDCAuthProvider and configure the API plugin with an instance of this class. The responsibility of the auth provider is to return the JWT token that was minted by your OIDC service. To do this:

  • Create a class which extends OIDCAuthProvider
1import 'package:amplify_api/amplify_api.dart';
2
3class CustomOIDCProvider extends OIDCAuthProvider {
4 const CustomOIDCProvider();
5
6
7 Future<String?> getLatestAuthToken() async {
8 ...
9 }
10}
  • Register your instance of APIAuthProvider prior to calling Amplify.configure():
1Future<void> initialize() async {
2 try {
3 final datastorePlugin = AmplifyDataStore(
4 modelProvider: ModelProvider.instance,
5 );
6 final api = AmplifyAPI(
7 authProviders: const [
8 CustomOIDCProvider(),
9 ],
10 );
11 await Amplify.addPlugins([datastorePlugin, api]);
12 await Amplify.configure(amplifyconfig);
13 } on Exception catch (e) {
14 print('Error configuring Amplify: $e');
15 }
16}

Static Group Authorization with OIDC provider

The following are commonly used patterns for using groupClaims to achieve group based authorization using a 3rd party OIDC provider. For more information on how to fine tune these examples, please see the CLI documentation on static group authorization.

  • Using a custom value for groupClaim to achieve static group authorization with a 3rd party OIDC provider.
1type YourModel
2 @model
3 @auth(
4 rules: [
5 {
6 allow: groups
7 provider: oidc
8 groups: ["Admin"]
9 groupClaim: "https://myapp.com/claims/groups"
10 }
11 ]
12 ) {
13 ...
14}

Configure Multiple Authorization Types

For some use cases, you will want DataStore to use multiple authorization types. For example, an app might use API Key for public content and Cognito User Pool for personalized content once the user logs in.

By default, DataStore uses your API's default authorization type specified in the amplifyconfiguration.json/.dart/aws-exports.js file. Every network request sent through DataStore uses that authorization type, regardless of the model's @auth rule. To change the default authorization type, run amplify update api.

To enable DataStore to use multiple authorization types based on the model's @auth rules, run amplify update api to configure additional auth types and deploy by running amplify push. Then, configure the "auth mode strategy" when initializing DataStore:

1Future<void> initialize() async {
2 try {
3 final datastorePlugin = AmplifyDataStore(
4 modelProvider: ModelProvider.instance,
5 // Be sure to add the authModeStrategy
6 authModeStrategy: AuthModeStrategy.multiAuth,
7 );
8 final api = AmplifyAPI();
9 await Amplify.addPlugins([datastorePlugin, api]);
10 await Amplify.configure(amplifyconfig);
11 } on Exception catch (e) {
12 print('Error configuring Amplify: $e');
13 }
14}

This configuration enables DataStore to synchronize data using the model's @auth rule provider for each model.

Multiple authorization types priority order

If there are multiple @auth rules on a model, the rules will be ranked by priority (see below), and DataStore will attempt the synchronization with each authorization type until one succeeds (or they all fail).

Priorityallow: AuthStrategyprovider
1owneruserPools
2owneroidc
3groupuserPools
4groupoidc
5privateuserPools
6privateiam
7publiciam
8publicapiKey

If there is not an authenticated user session, DataStore will only attempt public rules.

If a model has no auth rules defined, DataStore will continue to use the default authorization type from amplifyconfiguration.json/.dart.

Example with multiple authorization types

1type YourModel
2 @model
3 @auth(
4 rules: [
5 { allow: owner }
6 { allow: public, provider: apiKey, operations: [read] }
7 ]
8 ) {
9 ...
10}

DataStore will attempt to use owner-based authorization first when synchronizing data if there is an authenticated user. If that request fails for some reason, DataStore will attempt the request again with public authorization. If there is no authenticated user, public authorization will be used.