@simpleview/sv-auth-client 4.6.2 → 4.6.3-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,24 +1,29 @@
1
1
  {
2
2
  "name": "@simpleview/sv-auth-client",
3
- "version": "4.6.2",
3
+ "version": "4.6.3-0",
4
4
  "description": "Client for communicating with sv-auth",
5
5
  "author": "Owen Allen <owenallenaz@gmail.com>",
6
6
  "dependencies": {
7
- "@simpleview/sv-graphql-client": ">=1.3.0",
7
+ "@graphql-tools/utils": "8.9.0",
8
+ "@simpleview/sv-graphql-client": ">=2.0.0",
8
9
  "@simpleview/express-async-handler": "1.0.2",
9
- "graphql": "14.2.1",
10
- "graphql-tools": "4.0.4",
10
+ "graphql-tag": "2.12.6",
11
11
  "jsonwebtoken": "8.5.1"
12
12
  },
13
+ "peerDependencies": {
14
+ "graphql": ">=15.0.0"
15
+ },
13
16
  "devDependencies": {
14
17
  "@babel/core": "7.14.5",
15
18
  "@babel/eslint-parser": "7.14.5",
19
+ "@graphql-tools/schema": "8.5.1",
16
20
  "@simpleview/mochalib": "1.0.1",
17
21
  "@simpleview/assertlib": "1.2.0",
18
- "apollo-server": "2.21.2",
22
+ "apollo-server": "3.10.0",
19
23
  "cookie": "0.5.0",
20
24
  "cookie-session": "2.0.0",
21
25
  "eslint": "7.28.0",
26
+ "graphql": "16.5.0",
22
27
  "mocha": "5.2.0",
23
28
  "supertest": "6.2.4"
24
29
  },
@@ -1,73 +1,81 @@
1
1
  //@ts-check
2
2
  const { GraphServer } = require("@simpleview/sv-graphql-client");
3
- const { SchemaDirectiveVisitor } = require("graphql-tools");
4
- const { gql } = require("apollo-server");
3
+ const gql = require("graphql-tag");
5
4
  const { defaultFieldResolver } = require("graphql");
5
+ const { mapSchema, getDirective, MapperKind } = require("@graphql-tools/utils");
6
6
 
7
7
  const AdminPrefix = require("./prefixes/AdminPrefix");
8
8
 
9
9
  function getDirectiveCheckPerm({ name, graphUrl }) {
10
10
  const graphServer = new GraphServer({ graphUrl, prefixes : [AdminPrefix] });
11
11
 
12
- class DirectiveCheckPerm extends SchemaDirectiveVisitor {
13
- visitFieldDefinition(field) {
14
- const { resolve = defaultFieldResolver } = field;
15
- const directiveArgs = this.args;
16
- field.resolve = async function(parent, args, context, info) {
17
- // check if the resolver requires sv user
18
- if (directiveArgs.sv === true && context.user.sv !== true) {
19
- throw new Error("User is not authorized to access this resource (ERR: 1005).");
20
- }
12
+ function schemaTransformer(schema) {
13
+ return mapSchema(schema, {
14
+ [MapperKind.OBJECT_FIELD] : (fieldConfig) => {
15
+ const directiveArgs = getDirective(schema, fieldConfig, name)?.[0]
21
16
 
22
- // check if the resolver requires specific permissions
23
- if (directiveArgs.perms !== undefined) {
24
- const allowed = context.user.can(directiveArgs.perms);
25
- if (allowed === false) {
26
- throw new Error("User is not authorized to access this resource (ERR: 1006).");
27
- }
28
- }
17
+ if (directiveArgs) {
18
+ const { resolve = defaultFieldResolver } = fieldConfig;
29
19
 
30
- if (directiveArgs.bindings !== undefined) {
31
- const headers = {};
20
+ fieldConfig.resolve = async function(parent, args, context, info) {
21
+ // check if the resolver requires sv user
22
+ if (directiveArgs.sv === true && context.user.sv !== true) {
23
+ throw new Error("User is not authorized to access this resource (ERR: 1005).");
24
+ }
32
25
 
33
- if (context.headers) {
34
- const allowedHeaders = ["x-sv-object-bindings"];
35
- for (let header of allowedHeaders) {
36
- if (context.headers[header]) {
37
- headers[header] = context.headers[header];
26
+ // check if the resolver requires specific permissions
27
+ if (directiveArgs.perms !== undefined) {
28
+ const allowed = context.user.can(directiveArgs.perms);
29
+ if (allowed === false) {
30
+ throw new Error("User is not authorized to access this resource (ERR: 1006).");
38
31
  }
39
32
  }
40
- }
41
33
 
42
- const bindings = await graphServer.admin.object_bindings_mine({
43
- filter : directiveArgs.bindings,
44
- fields : `
45
- success
46
- permissionObj
47
- `,
48
- context : {
49
- acct_id : context.user.acct_id,
50
- token : context.token
51
- },
52
- headers
53
- });
54
-
55
- if (bindings.success === false) {
56
- throw new Error("Unable to retrieve user bindings.");
34
+ if (directiveArgs.bindings !== undefined) {
35
+ const headers = {};
36
+
37
+ if (context.headers) {
38
+ const allowedHeaders = ["x-sv-object-bindings"];
39
+ for (let header of allowedHeaders) {
40
+ if (context.headers[header]) {
41
+ headers[header] = context.headers[header];
42
+ }
43
+ }
44
+ }
45
+
46
+ const bindings = await graphServer.admin.object_bindings_mine({
47
+ filter : directiveArgs.bindings,
48
+ fields : `
49
+ success
50
+ permissionObj
51
+ `,
52
+ context : {
53
+ acct_id : context.user.acct_id,
54
+ token : context.token
55
+ },
56
+ headers
57
+ });
58
+
59
+ if (bindings.success === false) {
60
+ throw new Error("Unable to retrieve user bindings.");
61
+ }
62
+
63
+ context.user.permissionObjBindings = bindings.permissionObj;
64
+ }
65
+
66
+ return await resolve.call(this, parent, args, context, info);
57
67
  }
58
68
 
59
- context.user.permissionObjBindings = bindings.permissionObj;
69
+ return fieldConfig;
60
70
  }
61
-
62
- return await resolve.call(this, parent, args, context, info);
63
71
  }
64
- }
72
+ });
65
73
  }
66
74
 
67
75
  return {
68
- schemaDirectives : {
69
- [name] : DirectiveCheckPerm
70
- },
76
+ schemaTransformers : [
77
+ schemaTransformer
78
+ ],
71
79
  typeDefs : gql`
72
80
  directive @${name}(
73
81
  sv: Boolean
@@ -1,47 +1,55 @@
1
- const { SchemaDirectiveVisitor } = require("graphql-tools");
2
- const { gql } = require("apollo-server");
1
+ const gql = require("graphql-tag");
3
2
  const { defaultFieldResolver } = require("graphql");
3
+ const { mapSchema, getDirective, MapperKind } = require("@graphql-tools/utils");
4
4
 
5
5
  const AuthClient = require("./AuthClient");
6
6
 
7
7
  function getDirectiveGetUser({ name, graphUrl }) {
8
8
  const authClient = new AuthClient({ graphUrl });
9
9
 
10
- class DirectiveGetUser extends SchemaDirectiveVisitor {
11
- visitFieldDefinition(field) {
12
- const { resolve = defaultFieldResolver } = field;
13
- const directiveArgs = this.args
14
- field.resolve = async function(parent, args, context, info) {
15
- // check the context token
16
- if (context.token === undefined) {
17
- throw new Error("User is not authorized to access this resource (ERR: 1000).");
18
- }
10
+ function schemaTransformer(schema) {
11
+ return mapSchema(schema, {
12
+ [MapperKind.OBJECT_FIELD] : (fieldConfig) => {
13
+ const directiveArgs = getDirective(schema, fieldConfig, name)?.[0]
19
14
 
20
- // assign acct_id either based on the field acct_id or directive passed acct_id
21
- const acct_id = directiveArgs.acct_id !== undefined ? directiveArgs.acct_id : args.acct_id;
22
- // call getUser
23
- const user = await authClient.getUser({
24
- acct_id,
25
- token : context.token,
26
- headers: context.headers
27
- });
28
-
29
- if (user === undefined) {
30
- throw new Error("User is not authorized to access this resource (ERR: 1007).");
31
- }
15
+ if (directiveArgs) {
16
+ const { resolve = defaultFieldResolver } = fieldConfig;
17
+
18
+ fieldConfig.resolve = async function(parent, args, context, info) {
19
+ // check the context token
20
+ if (context.token === undefined) {
21
+ throw new Error("User is not authorized to access this resource (ERR: 1000).");
22
+ }
23
+
24
+ // assign acct_id either based on the field acct_id or directive passed acct_id
25
+ const acct_id = directiveArgs.acct_id !== undefined ? directiveArgs.acct_id : args.acct_id;
26
+ // call getUser
27
+ const user = await authClient.getUser({
28
+ acct_id,
29
+ token : context.token,
30
+ headers: context.headers
31
+ });
32
32
 
33
- // assign user
34
- context.user = user;
33
+ if (user === undefined) {
34
+ throw new Error("User is not authorized to access this resource (ERR: 1007).");
35
+ }
35
36
 
36
- return await resolve.call(this, parent, args, context, info);
37
+ // assign user
38
+ context.user = user;
39
+
40
+ return await resolve.call(this, parent, args, context, info);
41
+ }
42
+
43
+ return fieldConfig;
44
+ }
37
45
  }
38
- }
46
+ });
39
47
  }
40
48
 
41
49
  return {
42
- schemaDirectives : {
43
- [name] : DirectiveGetUser
44
- },
50
+ schemaTransformers : [
51
+ schemaTransformer
52
+ ],
45
53
  typeDefs : gql`
46
54
  directive @${name}(
47
55
  acct_id: String