-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
388 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {SchemaDirectiveVisitor} from 'graphql-tools'; | ||
import {GraphQLField, GraphQLResolveInfo} from 'graphql'; | ||
import {RequestContext} from '../../context'; | ||
import {gql} from 'apollo-server-core'; | ||
import {PolicyResult, Policy} from './types'; | ||
import {PolicyExecutor} from './policy-executor'; | ||
|
||
export class PolicyQueryDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field: GraphQLField<any, any>) { | ||
field.resolve = async ( | ||
parent: any, | ||
args: any, | ||
context: RequestContext, | ||
info: GraphQLResolveInfo | ||
): Promise<PolicyResult> => { | ||
const policy: Policy = { | ||
namespace: this.args.namespace, | ||
name: this.args.name, | ||
args: args, | ||
}; | ||
|
||
const executor = new PolicyExecutor([policy], parent, args, context, info); | ||
try { | ||
await executor.validatePolicies(); | ||
return {allow: true}; | ||
} catch (error) { | ||
return {allow: false}; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
export const sdl = gql` | ||
directive @policyQuery(namespace: String!, name: String!) on FIELD_DEFINITION | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
services/src/tests/e2e/tests/__snapshots__/authorization_with_queries.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Authorization with queries Query denied employee 1 1`] = ` | ||
Object { | ||
"data": Object { | ||
"deniedEmployee1": Object { | ||
"address": null, | ||
"id": "2", | ||
"name": "Mark Zuckerberg", | ||
}, | ||
}, | ||
"errors": Array [ | ||
Object { | ||
"extensions": Object { | ||
"code": "INTERNAL_SERVER_ERROR", | ||
"exception": Object { | ||
"stacktrace": Array [ | ||
"Error: Unauthorized by policy notClassified in namespace namespace", | ||
" at PolicyExecutor.validatePolicy (/service/dist/modules/directives/policy/policy-executor.js:33:19)", | ||
" at async Promise.all (index 0)", | ||
" at async PolicyExecutor.validatePolicies (/service/dist/modules/directives/policy/policy-executor.js:20:9)", | ||
" at async field.resolve (/service/dist/modules/directives/policy/policy.js:13:13)", | ||
], | ||
}, | ||
}, | ||
"locations": Array [ | ||
Object { | ||
"column": 7, | ||
"line": 6, | ||
}, | ||
], | ||
"message": "Unauthorized by policy notClassified in namespace namespace", | ||
"path": Array [ | ||
"deniedEmployee1", | ||
"address", | ||
], | ||
}, | ||
], | ||
"extensions": Any<Object>, | ||
"status": 200, | ||
} | ||
`; | ||
exports[`Authorization with queries Query denied employee 2 1`] = ` | ||
Object { | ||
"data": Object { | ||
"deniedEmployee2": Object { | ||
"address": null, | ||
"id": "2", | ||
"name": "Tom Baker", | ||
}, | ||
}, | ||
"errors": Array [ | ||
Object { | ||
"extensions": Object { | ||
"code": "INTERNAL_SERVER_ERROR", | ||
"exception": Object { | ||
"stacktrace": Array [ | ||
"Error: Unauthorized by policy notClassified in namespace namespace", | ||
" at PolicyExecutor.validatePolicy (/service/dist/modules/directives/policy/policy-executor.js:33:19)", | ||
" at async Promise.all (index 0)", | ||
" at async PolicyExecutor.validatePolicies (/service/dist/modules/directives/policy/policy-executor.js:20:9)", | ||
" at async field.resolve (/service/dist/modules/directives/policy/policy.js:13:13)", | ||
], | ||
}, | ||
}, | ||
"locations": Array [ | ||
Object { | ||
"column": 7, | ||
"line": 6, | ||
}, | ||
], | ||
"message": "Unauthorized by policy notClassified in namespace namespace", | ||
"path": Array [ | ||
"deniedEmployee2", | ||
"address", | ||
], | ||
}, | ||
], | ||
"extensions": Any<Object>, | ||
"status": 200, | ||
} | ||
`; |
Oops, something went wrong.