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

Pass context through to access control functions #3153

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cold-plants-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/access-control': minor
'@keystonejs/keystone': minor
---

Made `context` available to user designed access control functions.
3 changes: 3 additions & 0 deletions docs/api/access-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ the list `User` it would match the input type `UserWhereInput`.
| `gqlName` | The name of the query or mutation which triggered the access check. |
| `itemId` | The `id` of the item being updated/deleted in singular `update` and `delete` operations. |
| `itemIds` | The `ids` of the items being updated/deleted in multiple `update` and `delete` operations. |
| `context` | The `context` of the originating GraphQL operation. |

When resolving `StaticAccess`:

Expand Down Expand Up @@ -281,6 +282,7 @@ interface AccessInput {
gqlName?: string;
itemId?: string;
itemIds?: [string];
context?: {};
}

type StaticAccess = boolean;
Expand Down Expand Up @@ -314,6 +316,7 @@ type FieldConfig = {
| `gqlName` | The name of the query or mutation which triggered the access check. |
| `itemId` | The `id` of the item being updated/deleted in singular `update` and `delete` operations. |
| `itemIds` | The `ids` of the items being updated/deleted in multiple `update` and `delete` operations. |
| `context` | The `context` of the originating GraphQL operation. |

When defining `StaticAccess`:

Expand Down
7 changes: 6 additions & 1 deletion packages/access-control/lib/access-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ module.exports = {
gqlName,
itemId,
itemIds,
context,
}) {
// Either a boolean or an object describing a where clause
let result;
Expand All @@ -207,6 +208,7 @@ module.exports = {
gqlName,
itemId,
itemIds,
context,
});
}

Expand Down Expand Up @@ -239,6 +241,7 @@ module.exports = {
gqlName,
itemId,
itemIds,
context,
}) {
let result;
if (typeof access[operation] !== 'function') {
Expand All @@ -254,6 +257,7 @@ module.exports = {
gqlName,
itemId,
itemIds,
context,
});
}

Expand All @@ -268,7 +272,7 @@ module.exports = {
return result;
},

async validateAuthAccessControl({ access, listKey, authentication = {}, gqlName }) {
async validateAuthAccessControl({ access, listKey, authentication = {}, gqlName, context }) {
const operation = 'auth';
// Either a boolean or an object describing a where clause
let result;
Expand All @@ -280,6 +284,7 @@ module.exports = {
listKey,
operation,
gqlName,
context,
});
}

Expand Down
9 changes: 6 additions & 3 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module.exports = class Keystone {
);

const getListAccessControlForUser = memoize(
async (listKey, originalInput, operation, { gqlName, itemId, itemIds } = {}) => {
async (listKey, originalInput, operation, { gqlName, itemId, itemIds, context } = {}) => {
return validateListAccessControl({
access: this.lists[listKey].access[schemaName],
originalInput,
Expand All @@ -175,6 +175,7 @@ module.exports = class Keystone {
gqlName,
itemId,
itemIds,
context,
});
},
{ isPromise: true }
Expand All @@ -187,7 +188,7 @@ module.exports = class Keystone {
originalInput,
existingItem,
operation,
{ gqlName, itemId, itemIds } = {}
{ gqlName, itemId, itemIds, context } = {}
) => {
return validateFieldAccessControl({
access: this.lists[listKey].fieldsByPath[fieldKey].access[schemaName],
Expand All @@ -200,18 +201,20 @@ module.exports = class Keystone {
gqlName,
itemId,
itemIds,
context,
});
},
{ isPromise: true }
);

const getAuthAccessControlForUser = memoize(
async (listKey, { gqlName } = {}) => {
async (listKey, { gqlName, context } = {}) => {
return validateAuthAccessControl({
access: this.lists[listKey].access[schemaName],
authentication,
listKey,
gqlName,
context,
});
},
{ isPromise: true }
Expand Down
20 changes: 13 additions & 7 deletions packages/keystone/lib/ListTypes/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ module.exports = class List {
field.path,
undefined,
item,
operation
operation,
{ context }
);
if (!access) {
// If the client handles errors correctly, it should be able to
Expand Down Expand Up @@ -437,7 +438,7 @@ module.exports = class List {
data,
existingItem,
operation,
{ gqlName, itemId: id, ...extraInternalData }
{ gqlName, itemId: id, context, ...extraInternalData }
);
if (!access) {
restrictedFields.push(field.path);
Expand All @@ -454,6 +455,7 @@ module.exports = class List {
async checkListAccess(context, originalInput, operation, { gqlName, ...extraInternalData }) {
const access = await context.getListAccessControlForUser(this.key, originalInput, operation, {
gqlName,
context,
...extraInternalData,
});
if (!access) {
Expand Down Expand Up @@ -1483,11 +1485,15 @@ module.exports = class List {
// NOTE: These could return a Boolean or a JSON object (if using the
// declarative syntax)
getAccess: () => ({
getCreate: () => context.getListAccessControlForUser(this.key, undefined, 'create'),
getRead: () => context.getListAccessControlForUser(this.key, undefined, 'read'),
getUpdate: () => context.getListAccessControlForUser(this.key, undefined, 'update'),
getDelete: () => context.getListAccessControlForUser(this.key, undefined, 'delete'),
getAuth: () => context.getAuthAccessControlForUser(this.key),
getCreate: () =>
context.getListAccessControlForUser(this.key, undefined, 'create', { context }),
getRead: () =>
context.getListAccessControlForUser(this.key, undefined, 'read', { context }),
getUpdate: () =>
context.getListAccessControlForUser(this.key, undefined, 'update', { context }),
getDelete: () =>
context.getListAccessControlForUser(this.key, undefined, 'delete', { context }),
getAuth: () => context.getAuthAccessControlForUser(this.key, { context }),
}),

getSchema: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/lib/providers/listAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ListAuthProvider {

async checkAccess(context, type, { gqlName }) {
const operation = 'auth';
const access = await context.getAuthAccessControlForUser(this.list.key, { gqlName });
const access = await context.getAuthAccessControlForUser(this.list.key, { gqlName, context });
if (!access) {
graphqlLogger.debug({ operation, access, gqlName }, 'Access statically or implicitly denied');
graphqlLogger.info({ operation, gqlName }, 'Access Denied');
Expand Down