@simpleview/sv-auth-client 5.3.0 → 6.7.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleview/sv-auth-client",
3
- "version": "5.3.0",
3
+ "version": "6.7.0",
4
4
  "description": "Client for communicating with sv-auth",
5
5
  "author": "Owen Allen <owenallenaz@gmail.com>",
6
6
  "dependencies": {
@@ -8,8 +8,10 @@
8
8
  "@simpleview/sv-graphql-client": ">=2.0.3",
9
9
  "@simpleview/express-async-handler": "1.0.2",
10
10
  "axios": "1.3.4",
11
+ "expiry-map": "2.0.0",
11
12
  "graphql-tag": "2.12.6",
12
- "jsonwebtoken": "9.0.0"
13
+ "jsonwebtoken": "9.0.0",
14
+ "p-memoize-cjs": "7.1.0"
13
15
  },
14
16
  "peerDependencies": {
15
17
  "graphql": ">=16.0.0"
package/readme.md CHANGED
@@ -74,10 +74,6 @@ const user = authClient.getUser({
74
74
  });
75
75
  ```
76
76
 
77
- #### AuthClient.close
78
-
79
- If you are finished with an AuthClient instance, call `authClient.close()` in order to shut it down. Generally this is only needed in unit tests, otherwise there is an internal `setInterval` which will keep the process open.
80
-
81
77
  ### getTokenFromHeaders
82
78
 
83
79
  Extracts the token from the `authorization` header.
package/src/AuthClient.js CHANGED
@@ -2,58 +2,43 @@
2
2
  const { GraphServer } = require("@simpleview/sv-graphql-client");
3
3
  const AuthPrefix = require("./prefixes/AuthPrefix");
4
4
  const User = require("./User");
5
+ const ExpiryMap = require("expiry-map");
6
+ const { pMemoize } = require("p-memoize-cjs");
5
7
 
6
- function AuthClient({ graphUrl, cacheDuration = 1000 * 60 * 60 }) {
7
- this._graphServer = new GraphServer({ graphUrl, prefixes : [AuthPrefix] });
8
- this._cache = {};
9
- this._interval = setInterval(() => {
10
- const now = Date.now();
11
- for (const i in this._cache) {
12
- if (this._cache[i].created + cacheDuration > now) {
13
- delete this._cache[i];
14
- }
8
+ class CustomExpiry extends ExpiryMap {
9
+ constructor(args) {
10
+ super(args);
11
+ }
12
+ set(key, value) {
13
+ // don't cache requests if the user didn't exist
14
+ if (value === undefined) {
15
+ return this;
15
16
  }
16
- }, cacheDuration);
17
17
 
18
- // unref the interval so that it doesn't keep the process open
19
- this._interval.unref();
18
+ return super.set(key, value);
19
+ }
20
+ }
21
+
22
+ function AuthClient({ graphUrl, cacheDuration = 1000 * 60 * 60 }) {
23
+ this._graphServer = new GraphServer({ graphUrl, prefixes : [AuthPrefix] });
24
+ this._cache = new CustomExpiry(cacheDuration);
25
+ this._getUserDoc = pMemoize(getUserDoc.bind(this), {
26
+ cache: this._cache,
27
+ cacheKey: ([{ acct_id, token }]) => `${token}_${acct_id}`
28
+ });
20
29
  }
21
30
 
22
31
  Object.defineProperty(AuthClient.prototype, "cacheLength", {
23
32
  get : function() {
24
- return Object.keys(this._cache).length;
33
+ return this._cache.size;
25
34
  }
26
35
  });
27
36
 
28
- AuthClient.prototype.close = function() {
29
- clearInterval(this._interval);
30
- }
31
-
32
37
  AuthClient.prototype.clearCache = function() {
33
- this._cache = {};
38
+ this._cache.clear();
34
39
  }
35
40
 
36
- AuthClient.prototype._getUserDoc = async function({ acct_id, token }) {
37
- const cacheKey = `${token}_${acct_id}`;
38
- const cacheEntry = this._cache[cacheKey];
39
- if (cacheEntry !== undefined) {
40
- const cacheResult = await this._graphServer.auth.check_token_cache({
41
- date : new Date(cacheEntry.created).toISOString(),
42
- acct_id,
43
- fields : `success message`,
44
- context : {
45
- token
46
- }
47
- });
48
-
49
- if (cacheResult.success === true) {
50
- cacheEntry.hits++;
51
- return cacheEntry.user;
52
- } else {
53
- delete this._cache[cacheKey];
54
- }
55
- }
56
-
41
+ async function getUserDoc({ acct_id, token }) {
57
42
  const userResult = await this._graphServer.auth.current({
58
43
  acct_id,
59
44
  fields : `
@@ -85,12 +70,6 @@ AuthClient.prototype._getUserDoc = async function({ acct_id, token }) {
85
70
  return;
86
71
  }
87
72
 
88
- this._cache[cacheKey] = {
89
- user : userResult.doc,
90
- created : Date.now(),
91
- hits : 0
92
- }
93
-
94
73
  return userResult.doc;
95
74
  }
96
75
 
@@ -115,4 +94,4 @@ AuthClient.prototype.getUser = async function({ acct_id, token, headers }) {
115
94
  return user;
116
95
  }
117
96
 
118
- module.exports = AuthClient;
97
+ module.exports = AuthClient;
@@ -25,7 +25,8 @@ class AdminPrefix {
25
25
  this[`${name}_remove`] = api.remove.bind(api);
26
26
  }
27
27
  }
28
- async users_import({fields, context }) {
28
+
29
+ async users_import({ fields, context }) {
29
30
  context = context || this._graphServer.context;
30
31
 
31
32
  const variables = {
@@ -53,6 +54,7 @@ class AdminPrefix {
53
54
 
54
55
  return returnData;
55
56
  }
57
+
56
58
  async users_send_invitation({ input, fields, context }) {
57
59
  context = context || this._graphServer.context;
58
60
 
@@ -72,8 +74,8 @@ class AdminPrefix {
72
74
  }
73
75
  `,
74
76
  variables,
75
- url : this._graphUrl,
76
- token : context.token
77
+ url: this._graphUrl,
78
+ token: context.token
77
79
  });
78
80
 
79
81
  const returnData = response.admin.users_send_invitation;
@@ -82,6 +84,58 @@ class AdminPrefix {
82
84
 
83
85
  return returnData;
84
86
  }
87
+
88
+ async account_global_settings_upsert({ input, fields, context }) {
89
+ context = context || this._graphServer.context;
90
+
91
+ const variables = {
92
+ input,
93
+ acct_id: context.acct_id
94
+ }
95
+
96
+ return query({
97
+ query: `
98
+ mutation($acct_id: String!, $input: admin_account_global_settings_upsert!) {
99
+ admin(acct_id: $acct_id) {
100
+ account_global_settings_upsert(input: $input) {
101
+ ${fields}
102
+ }
103
+ }
104
+ }
105
+ `,
106
+ variables,
107
+ url: this._graphUrl,
108
+ token: context.token,
109
+ key: "admin.account_global_settings_upsert",
110
+ clean: true
111
+ });
112
+ }
113
+
114
+ async account_global_settings_remove({ fields, context }) {
115
+ context = context || this._graphServer.context;
116
+
117
+ const variables = {
118
+ acct_id: context.acct_id
119
+ }
120
+
121
+ return query({
122
+ query: `
123
+ mutation($acct_id: String!) {
124
+ admin(acct_id: $acct_id) {
125
+ account_global_settings_remove {
126
+ ${fields}
127
+ }
128
+ }
129
+ }
130
+ `,
131
+ variables,
132
+ url: this._graphUrl,
133
+ token: context.token,
134
+ key: "admin.account_global_settings_remove",
135
+ clean: true
136
+ });
137
+ }
138
+
85
139
  async setup({ context, fields }) {
86
140
  context = context || this._graphServer.context;
87
141
 
@@ -110,6 +164,7 @@ class AdminPrefix {
110
164
 
111
165
  return returnData;
112
166
  }
167
+
113
168
  /**
114
169
  * @param {object} args
115
170
  * @param {object} [args.filter]
@@ -151,4 +206,4 @@ class AdminPrefix {
151
206
  }
152
207
  }
153
208
 
154
- module.exports = AdminPrefix;
209
+ module.exports = AdminPrefix;
@@ -1,5 +1,7 @@
1
1
  const Accounts = require("./auth/Accounts");
2
2
  const OauthClients = require("./auth/OauthClients");
3
+ const SamlClients = require("./auth/SamlClients");
4
+ const SVProducts = require("./auth/SVProducts");
3
5
  const TestItems = require("./auth/TestItems");
4
6
  const { query, nullToUndefined } = require("@simpleview/sv-graphql-client");
5
7
 
@@ -12,19 +14,31 @@ class AuthPrefix {
12
14
 
13
15
  this._accounts = new Accounts({
14
16
  graphUrl,
15
- name : "accounts",
17
+ name: "accounts",
16
18
  graphServer
17
19
  });
18
20
 
19
21
  this._oauthClients = new OauthClients({
20
22
  graphUrl,
21
- name : "oauth_clients",
23
+ name: "oauth_clients",
24
+ graphServer
25
+ });
26
+
27
+ this._samlClients = new SamlClients({
28
+ graphUrl,
29
+ name: "saml_clients",
30
+ graphServer
31
+ });
32
+
33
+ this._sv_products = new SVProducts({
34
+ graphUrl,
35
+ name: "sv_products",
22
36
  graphServer
23
37
  });
24
38
 
25
39
  this._testItems = new TestItems({
26
40
  graphUrl,
27
- name : "test_items",
41
+ name: "test_items",
28
42
  graphServer
29
43
  })
30
44
  }
@@ -121,6 +135,30 @@ class AuthPrefix {
121
135
  return this._oauthClients.remove(...args);
122
136
  }
123
137
 
138
+ async saml_clients(...args) {
139
+ return this._samlClients.find(...args);
140
+ }
141
+
142
+ async saml_clients_upsert(...args) {
143
+ return this._samlClients.upsert(...args);
144
+ }
145
+
146
+ async saml_clients_remove(...args) {
147
+ return this._samlClients.remove(...args);
148
+ }
149
+
150
+ async sv_products(...args) {
151
+ return this._sv_products.find(...args);
152
+ }
153
+
154
+ async sv_products_upsert(...args) {
155
+ return this._sv_products.upsert(...args);
156
+ }
157
+
158
+ async sv_products_remove(...args) {
159
+ return this._sv_products.remove(...args);
160
+ }
161
+
124
162
  async current({ acct_id, fields, context }) {
125
163
  context = context || this._graphServer.context;
126
164
 
@@ -1,3 +1,3 @@
1
1
  const GraphAuthApi = require("./GraphAuthApi");
2
2
 
3
- module.exports = GraphAuthApi;
3
+ module.exports = GraphAuthApi();
@@ -1,6 +1,6 @@
1
1
  const { query, nullToUndefined } = require("@simpleview/sv-graphql-client");
2
2
 
3
- class GraphAuthApi {
3
+ module.exports = ({ excludeFilterOptions = false } = {}) => class GraphAuthApi {
4
4
  constructor({ graphUrl, name, graphServer }) {
5
5
  this._graphUrl = graphUrl;
6
6
  this._name = name;
@@ -18,9 +18,9 @@ class GraphAuthApi {
18
18
 
19
19
  const response = await query({
20
20
  query : `
21
- query($filter: auth_${method}_filter, $options: auth_${method}_options) {
21
+ query($filter: auth_${method}_filter${excludeFilterOptions ? "" : `, $options: auth_${method}_options`}) {
22
22
  auth {
23
- ${method}(filter: $filter, options: $options) {
23
+ ${method}(filter: $filter${excludeFilterOptions ? "" : ", options: $options"}) {
24
24
  ${fields}
25
25
  }
26
26
  }
@@ -101,5 +101,3 @@ class GraphAuthApi {
101
101
  return returnData;
102
102
  }
103
103
  }
104
-
105
- module.exports = GraphAuthApi;
@@ -1,3 +1,3 @@
1
1
  const GraphAuthApi = require("./GraphAuthApi");
2
2
 
3
- module.exports = GraphAuthApi;
3
+ module.exports = GraphAuthApi();
@@ -0,0 +1,3 @@
1
+ const GraphAuthApi = require("./GraphAuthApi");
2
+
3
+ module.exports = GraphAuthApi({ excludeFilterOptions: true });
@@ -0,0 +1,3 @@
1
+ const GraphAuthApi = require("./GraphAuthApi");
2
+
3
+ module.exports = GraphAuthApi({ excludeFilterOptions: true });