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

Page updated Apr 29, 2024

Set up authorization rules

Amplify Flutter v1 is now in Maintenance Mode until April 30th, 2025. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Flutter to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

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 tune these examples, please see the CLI documentation on owner based authorization.

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

Static Group Authorization

The following are commonly used patterns for static group authorization. For more information on how to 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.
type YourModel @model @auth(rules: [{ allow: groups,
groups: ["Admin"] }]) {
...
}
  • Users belonging to the "Admin" group can create and delete, other signed users can read and update.
type YourModel
@model
@auth(
rules: [
{ allow: groups, groups: ["Admin"], operations: [create, delete] }
{ allow: private, operations: [read, update] }
]
) {
...
}

Owner and Static Group Combined

The following are commonly used patterns for combining owner and static group authorization. For more information on how to 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
type YourModel
@model
@auth(rules: [{ allow: owner }, { allow: groups, groups: ["Admin"] }]) {
...
}

Public Data Access

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

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

Signed-in User Data Access

The following are commonly used patterns for private authorization. For more information on how to 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.
type YourModel @model @auth(rules: [{ allow: private }]) {
...
}
  • IAM authenticated users can CRUD all posts, regardless of who created it. Guest users do not have access:
type YourModel @model @auth(rules: [{ allow: private, provider: iam }]) {
...
}

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 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.
type YourModel
@model
@auth(rules: [{ allow: owner, provider: oidc, identityClaim: "sub" }]) {
...
}

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
import 'package:amplify_api/amplify_api.dart';
class CustomOIDCProvider extends OIDCAuthProvider {
const CustomOIDCProvider();
Future<String?> getLatestAuthToken() async {
...
}
}
  • Register your instance of APIAuthProvider prior to calling Amplify.configure():
Future<void> initialize() async {
try {
final datastorePlugin = AmplifyDataStore(
modelProvider: ModelProvider.instance,
);
final api = AmplifyAPI(
authProviders: const [
CustomOIDCProvider(),
],
);
await Amplify.addPlugins([datastorePlugin, api]);
await Amplify.configure(amplifyconfig);
} on Exception catch (e) {
print('Error configuring Amplify: $e');
}
}

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 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.
type YourModel
@model
@auth(
rules: [
{
allow: groups
provider: oidc
groups: ["Admin"]
groupClaim: "https://myapp.com/claims/groups"
}
]
) {
...
}

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:

Future<void> initialize() async {
try {
final datastorePlugin = AmplifyDataStore(
modelProvider: ModelProvider.instance,
// Be sure to add the authModeStrategy
authModeStrategy: AuthModeStrategy.multiAuth,
);
final api = AmplifyAPI();
await Amplify.addPlugins([datastorePlugin, api]);
await Amplify.configure(amplifyconfig);
} on Exception catch (e) {
print('Error configuring Amplify: $e');
}
}

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

type YourModel
@model
@auth(
rules: [
{ allow: owner }
{ allow: public, provider: apiKey, operations: [read] }
]
) {
...
}

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.