@thzero/library_server_firebase 0.18.11 → 0.18.15

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.
Files changed (2) hide show
  1. package/auth/index.js +32 -0
  2. package/package.json +4 -4
package/auth/index.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
 
4
+ import { Mutex as asyncMutex } from 'async-mutex';
5
+
4
6
  import admin from 'firebase-admin';
5
7
 
6
8
  import LibraryServerConstants from '@thzero/library_server/constants.js';
7
9
 
10
+ import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
11
+
8
12
  import NotImplementedError from '@thzero/library_common/errors/notImplemented.js';
9
13
 
10
14
  import Service from '@thzero/library_server/service/index.js';
@@ -15,6 +19,10 @@ class FirebaseAuthAdminService extends Service {
15
19
  constructor() {
16
20
  super();
17
21
 
22
+ this._cacheTokens = new Map();
23
+ this._cacheTokensTtlDefault = 5 * 60 * 1000;
24
+ this._mutexCache = new asyncMutex();
25
+
18
26
  this._serviceUsers = null;
19
27
  }
20
28
 
@@ -117,6 +125,29 @@ class FirebaseAuthAdminService extends Service {
117
125
  if (String.isNullOrEmpty(token))
118
126
  return results;
119
127
 
128
+ if (this._cacheTokens.has(token)) {
129
+ // https://firebase.google.com/docs/auth/admin/manage-sessions
130
+ // firebase tokens are valid for an hour
131
+ // buffering...
132
+ const release = await this._mutexCache.acquire();
133
+ try {
134
+ if (this._cacheTokens.has(token)) {
135
+ const data = this._cacheTokens.get(token);
136
+ if (data) {
137
+ const now = LibraryMomentUtility.getTimestamp();
138
+ const delta = now - data.time;
139
+ if (delta <= this._cacheTokensTtlDefault)
140
+ return data.results;
141
+
142
+ this._cacheTokens.delete(token);
143
+ }
144
+ }
145
+ }
146
+ finally {
147
+ release();
148
+ }
149
+ }
150
+
120
151
  const decodedToken = await admin.auth().verifyIdToken(token);
121
152
  if (!decodedToken)
122
153
  return results;
@@ -148,6 +179,7 @@ class FirebaseAuthAdminService extends Service {
148
179
  if (configAuth.claims && configAuth.claims.useDefault && !results.claims)
149
180
  results.claims = [ this._defaultClaims() ];
150
181
 
182
+ this._cacheTokens.set(token, { time: LibraryMomentUtility.getTimestamp(), results: results });
151
183
  results.success = true;
152
184
  return results;
153
185
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server_firebase",
3
3
  "type": "module",
4
- "version": "0.18.11",
4
+ "version": "0.18.15",
5
5
  "version_major": 0,
6
6
  "version_minor": 18,
7
- "version_patch": 11,
8
- "version_date": "02/09/2025",
7
+ "version_patch": 15,
8
+ "version_date": "09/03/2025",
9
9
  "author": "thZero",
10
10
  "license": "MIT",
11
11
  "repository": {
@@ -24,7 +24,7 @@
24
24
  "test": "echo \"Error: no test specified\" && exit 1"
25
25
  },
26
26
  "dependencies": {
27
- "firebase-admin": "^13.1.0"
27
+ "firebase-admin": "^13.5.0"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@thzero/library_common": "^0.18",