@simpleview/sv-auth-client 5.2.0 → 6.0.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.2.0",
3
+ "version": "6.0.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;
@@ -255,6 +255,26 @@ class AuthPrefix {
255
255
  return result.auth.login_google;
256
256
  }
257
257
 
258
+ async login_microsoft({ token, fields }) {
259
+ const result = await query({
260
+ query : `
261
+ query($token: String!) {
262
+ auth {
263
+ login_microsoft(token: $token) {
264
+ ${fields}
265
+ }
266
+ }
267
+ }
268
+ `,
269
+ variables : {
270
+ token
271
+ },
272
+ url : this._graphUrl
273
+ });
274
+
275
+ return result.auth.login_microsoft;
276
+ }
277
+
258
278
  async login_service_account({ input, fields }) {
259
279
  const result = await query({
260
280
  query : `