Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Update Data fails with error: "Unauthorized on [owner]" but owner is defined to be allowed to CRUD on resource.ts #5834

Open
4 of 14 tasks
fujimonfn opened this issue Jan 19, 2025 · 1 comment
Labels
pending-triage This issue is in the backlog of issues to triage

Comments

@fujimonfn
Copy link

Description

**Summary: **
For data with fields and authorizations defined as follows, create, read, and delete succeed, but only update fails.

DeviceTokenData: a
.model({
id: a.id().required(),
owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
deviceToken: a.string().required(),
})
.secondaryIndexes((index) => [
index('owner')
])
.authorization((allow) => [allow.owner()]),

Background:
We built a data model to store device tokens for sending push notifications.
To define a secondary index on the owner, we defined an “owner” field that does not need to be explicitly defined, and only for this field we defined a field-level authorization for this field only at the field level.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

  1. Define model like this. Make sure to set default auth mode "userPool".

DeviceTokenData: a
.model({
id: a.id().required(),
owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
deviceToken: a.string().required(),
})
.secondaryIndexes((index) => [
index('owner')
])
.authorization((allow) => [allow.owner()]),

  1. implement Update
    // get record first
    final queryRequest = ModelQueries.get(
    DeviceTokenData.classType,
    DeviceTokenDataModelIdentifier(id: deviceId),
    );
    final queryResponse = await Amplify.API.query(request: queryRequest).response;
    // then update
    final newDeviceTokenData = queryResponse.data!.copyWith(deviceToken: deviceToken);
    final updateRequest = ModelMutations.update(
    newDeviceTokenData
    );
    final updateResponse = await Amplify.API.mutate(request: updateRequest).response;
    if (updateResponse.errors.isNotEmpty) {
    logger.e('Update failed: ${updateResponse.errors.map((e) => 'Message: ${e.message}, ErrorType: ${e.errorType}').join('\n')}');
    return;
    }

Update failed: Message: Unauthorized on [owner], ErrorType: Unauthorized<…>

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.24.4

Amplify Flutter Version

2.5.0

Deployment Method

Amplify Gen 2

Schema

const schema = a
    .schema({
        DeviceTokenData: a
            .model({
                id: a.id().required(),
                owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
                deviceToken: a.string().required(),
            })
            .secondaryIndexes((index) => [
                index('owner')
            ])
            .authorization((allow) => [allow.owner()]),
    })
    .authorization((allow) => [
        allow.publicApiKey().to([]),
    ]);

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
    schema,
    authorizationModes: {
        defaultAuthorizationMode: 'userPool',
        apiKeyAuthorizationMode: {
            expiresInDays: 7,
        },
    },
});
@github-actions github-actions bot added pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Jan 19, 2025
@tyllark
Copy link
Member

tyllark commented Jan 20, 2025

Hello @fujimonfn, thank you for taking the time to open this issue. We will investigate and get back to you.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-triage This issue is in the backlog of issues to triage
Projects
None yet
Development

No branches or pull requests

2 participants