Skip to content

Commit

Permalink
added custom authorization rule
Browse files Browse the repository at this point in the history
  • Loading branch information
renebrandel committed Nov 7, 2023
1 parent 0af509d commit df573ad
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-wolves-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/amplify-api-next-alpha': minor
---

Added support for "a.allow.custom()" auth rule
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion packages/amplify-api-next/__tests__/ModelType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,40 @@ describe('model auth rules', () => {
});
}
}
});

it(`can define a custom authorization rule`, () => {
const schema = a.schema({
Widget: a.model({
title: a.string().required(),
})
.authorization([a.allow.custom()]),
});

const graphql = schema.transform().schema;
expect(graphql).toMatchSnapshot()
})

const TestOperations: Operation[][] = [
// each individual operation
...Operations.map((op) => [op]),

// a couple sanity checks to support a combinations
['create', 'read', 'update', 'delete'],
['create', 'read', 'listen'],
];

for (const operations of TestOperations) {
it(`can define custom auth rule for operations ${operations}`, () => {
const schema = a.schema({
widget: a
.model({
title: a.string().required(),
})
.authorization([a.allow.custom().to(operations)]),
});

const graphql = schema.transform().schema;
expect(graphql).toMatchSnapshot();
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,90 @@ exports[`model auth rules can create a static Admins group rule 1`] = `
}"
`;

exports[`model auth rules can define a custom authorization rule 1`] = `
"type Widget @model @auth(rules: [{allow: custom}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations create 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [create]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations create,read,listen 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [create, read, listen]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations create,read,update,delete 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [create, read, update, delete]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations delete 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [delete]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations get 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [get]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations list 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [list]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations listen 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [listen]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations read 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [read]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations search 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [search]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations sync 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [sync]}])
{
title: String!
}"
`;

exports[`model auth rules can define custom auth rule for operations update 1`] = `
"type widget @model @auth(rules: [{allow: custom, operations: [update]}])
{
title: String!
}"
`;

exports[`model auth rules can define owner auth with no provider 1`] = `
"type widget @model @auth(rules: [{allow: owner}])
{
Expand Down
12 changes: 12 additions & 0 deletions packages/amplify-api-next/src/Authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,18 @@ export const allow = {
},
);
},

custom(provider?: CustomProvider) {
return authData(
{
strategy: 'custom',
provider,
},
{
to
}
)
}
} as const;

/**
Expand Down

0 comments on commit df573ad

Please sign in to comment.