@vario-software/vario-app-framework-backend 2025.37.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.
@@ -0,0 +1,30 @@
1
+ class PromiseSingletonMap
2
+ {
3
+ constructor()
4
+ {
5
+ this.promiseMap = new Map();
6
+ }
7
+
8
+ /**
9
+ * Executes the function only once per key.
10
+ * On parallel access, the existing promise is returned.
11
+ */
12
+ run(key, fn)
13
+ {
14
+ if (this.promiseMap.has(key))
15
+ {
16
+ return this.promiseMap.get(key);
17
+ }
18
+
19
+ const promise = fn().finally(() =>
20
+ {
21
+ this.promiseMap.delete(key);
22
+ });
23
+
24
+ this.promiseMap.set(key, promise);
25
+
26
+ return promise;
27
+ }
28
+ }
29
+
30
+ module.exports = PromiseSingletonMap;
package/utils/token.js ADDED
@@ -0,0 +1,83 @@
1
+ const { jwtVerify, decodeJwt } = require('jose');
2
+ const { getApp } = require('#backend/utils/context.js');
3
+
4
+ function validateOfflineToken(offlineToken)
5
+ {
6
+ return new Promise((resolve, reject) =>
7
+ {
8
+ if (!offlineToken)
9
+ {
10
+ console.log('No token to read');
11
+ reject();
12
+ return;
13
+ }
14
+
15
+ const app = getApp();
16
+
17
+ try
18
+ {
19
+ const payload = decodeJwt(offlineToken);
20
+
21
+ const { azp } = payload;
22
+ const { clientId } = app.client;
23
+
24
+ if (!azp || azp !== clientId)
25
+ {
26
+ reject();
27
+ return;
28
+ }
29
+
30
+ resolve(payload);
31
+ }
32
+ catch (error)
33
+ {
34
+ console.log('cannot decodeJwT');
35
+ }
36
+ });
37
+ }
38
+
39
+ function validateAppToken(appToken)
40
+ {
41
+ return new Promise((resolve, reject) =>
42
+ {
43
+ if (!appToken)
44
+ {
45
+ reject();
46
+ return;
47
+ }
48
+
49
+ const app = getApp();
50
+
51
+ const { clientSecret, appIdentifier } = app.client;
52
+
53
+ const key = new TextEncoder().encode(clientSecret);
54
+
55
+ jwtVerify(appToken, key)
56
+ .then(({ payload }) =>
57
+ {
58
+ const { aud, exp } = payload;
59
+
60
+ if (!aud || aud !== appIdentifier)
61
+ {
62
+ console.log('First cond', !aud, aud !== appIdentifier);
63
+ reject();
64
+ return;
65
+ }
66
+
67
+ if (!exp || exp < (Date.now() / 1000))
68
+ {
69
+ console.log('Second cond', !exp, exp < (Date.now() / 1000));
70
+ reject();
71
+ return;
72
+ }
73
+
74
+ resolve(payload);
75
+ })
76
+ .catch(reject);
77
+ });
78
+ }
79
+
80
+ module.exports = {
81
+ validateOfflineToken,
82
+ validateAppToken,
83
+ };