Skip to content

Commit

Permalink
Merge pull request #1351 from rrdelaney/rrdelaney/bind-auth-scope-loa…
Browse files Browse the repository at this point in the history
…ders

Bind auth scope loaders to scopes object
  • Loading branch information
hayes authored Nov 27, 2024
2 parents fd89fcc + 68a9e93 commit 356ca4d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-shoes-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pothos/plugin-scope-auth": patch
---

Bind `authScopes` loaders to returned provider
3 changes: 2 additions & 1 deletion packages/plugin-scope-auth/src/request-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ export default class RequestCache<Types extends SchemaTypes> {
const key = this.cacheKey ? this.cacheKey(arg) : arg;

if (!cache.has(key)) {
const loader = scopes[name];
let loader = scopes[name];

if (typeof loader !== 'function') {
throw new PothosValidationError(
`Attempted to evaluate scope ${String(name)} as scope loader, but it is not a function`,
);
}

loader = loader.bind(scopes);
let result: MaybePromise<boolean>;

if (this.treatErrorsAsUnauthorized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type Query {
forAsyncPermission: String
forAsyncPermissionFn(permission: String): String
forBooleanFn(result: Boolean!): String
forBoundPermission: String
forSyncPermission: String
forSyncPermissionFn(permission: String): String
grantedFromRoot: String
Expand Down
18 changes: 12 additions & 6 deletions packages/plugin-scope-auth/tests/example/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ interface Context {
count?: (name: string) => void;
}

interface AuthScopes {
loggedIn: boolean;
admin: boolean;
syncPermission: string;
asyncPermission: string;
boundPermission: boolean;
}

const builder = new SchemaBuilder<{
Context: Context;
Interfaces: {
StringInterface: {};
};
AuthScopes: {
loggedIn: boolean;
admin: boolean;
syncPermission: string;
asyncPermission: string;
};
AuthScopes: AuthScopes;
AuthContexts: {
loggedIn: Context & { user: User; isLoggedIn: true };
admin: Context & { user: User; isAdmin: true };
Expand Down Expand Up @@ -59,6 +62,9 @@ const builder = new SchemaBuilder<{

return await !!context.user?.permissions.includes(perm);
},
boundPermission(this: AuthScopes) {
return this.admin;
},
};
},
},
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-scope-auth/tests/example/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ builder.queryType({
},
resolve: () => 'ok',
}),
forBoundPermission: t.string({
authScopes: {
boundPermission: true,
},
resolve: () => 'ok',
}),
forAll: t.string({
authScopes: {
$all: {
Expand Down
56 changes: 56 additions & 0 deletions packages/plugin-scope-auth/tests/field-auth-scopes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,62 @@ describe('queries for field authScopes with', () => {
`);
});

it('field scope with bound loader', async () => {
const query = gql`
query {
forBoundPermission
}
`;

const result = await execute({
schema: exampleSchema,
document: query,
contextValue: {
user: new User({
'x-user-id': '1',
'x-roles': 'admin',
}),
},
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"forBoundPermission": "ok",
},
}
`);
});

it('field scope with bound loader (unauthorized)', async () => {
const query = gql`
query {
forBoundPermission
}
`;

const result = await execute({
schema: exampleSchema,
document: query,
contextValue: {
user: new User({
'x-user-id': '1',
}),
},
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"forBoundPermission": null,
},
"errors": [
[GraphQLError: Not authorized to resolve Query.forBoundPermission],
],
}
`);
});

it('field with $any (sync)', async () => {
const query = gql`
query {
Expand Down

0 comments on commit 356ca4d

Please sign in to comment.