@adobe/helix-config-storage 1.7.2 → 1.7.3

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.7.3](https://github.com/adobe/helix-config-storage/compare/v1.7.2...v1.7.3) (2024-09-13)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * remove transient site token store ([#27](https://github.com/adobe/helix-config-storage/issues/27)) ([adb1543](https://github.com/adobe/helix-config-storage/commit/adb15437ba334ea3fe7276d4c9f4c0b785b736a3))
7
+
1
8
  ## [1.7.2](https://github.com/adobe/helix-config-storage/compare/v1.7.1...v1.7.2) (2024-09-07)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-config-storage",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "Helix Config Storage",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/index.js CHANGED
@@ -13,4 +13,3 @@ export { ConfigStore } from './config-store.js';
13
13
  export { SCHEMAS } from './config-validator.js';
14
14
  export * from './ValidationError.js';
15
15
  export * from './config-merge.js';
16
- export * from './transient-token-store.js';
@@ -1,181 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- /**
14
- * @typedef Token
15
- * @property {string} id
16
- * @property {string} value
17
- * @property {Date} created
18
- */
19
-
20
- /**
21
- * @typedef SiteTokens
22
- * @property {Token} preview
23
- * @property {Token} live
24
- */
25
-
26
- import crypto from 'crypto';
27
- import { HelixStorage } from '@adobe/helix-shared-storage';
28
-
29
- /**
30
- * Store to manage transient site tokens.
31
- * - the tokens have a fixed expiration time (default 24h) which can be verified on the edge
32
- * - the transient site tokens are managed and delivered independent of the fixed site tokens
33
- */
34
- export class TransientTokenStore {
35
- /**
36
- * Org name
37
- */
38
- org;
39
-
40
- /**
41
- * Site name
42
- */
43
- site;
44
-
45
- /**
46
- * S3/R2 Key for the transient site tokens
47
- */
48
- #key;
49
-
50
- /**
51
- * loaded tokens
52
- */
53
- #tokens;
54
-
55
- /**
56
- * modified flag
57
- */
58
- #modified;
59
-
60
- /**
61
- * flag indicating that the store was modified
62
- */
63
- wasModified = false;
64
-
65
- /**
66
- * consistent "now"
67
- */
68
- #now;
69
-
70
- constructor(org, site) {
71
- this.org = org;
72
- this.site = site;
73
- this.#key = `orgs/${this.org}/sites/${this.site}/transient-site-tokens.json`;
74
- this.#now = new Date();
75
- }
76
-
77
- /**
78
- * Returns the current time (mainly used for tests)
79
- * @returns {Date}
80
- */
81
- now() {
82
- return this.#now;
83
- }
84
-
85
- /**
86
- * Loads the transient site tokens from the storage
87
- * @param ctx
88
- * @returns {Promise<SiteTokens>}
89
- */
90
- async #load(ctx) {
91
- if (!this.#tokens) {
92
- this.#tokens = {
93
- preview: undefined,
94
- live: undefined,
95
- };
96
- this.#modified = false;
97
- const storage = HelixStorage.fromContext(ctx).configBus();
98
- const buf = await storage.get(this.#key);
99
- if (buf) {
100
- const data = JSON.parse(buf.toString('utf-8'));
101
- this.#tokens.preview = data.tokens.preview;
102
- this.#tokens.live = data.tokens.live;
103
- }
104
- }
105
- return this.#tokens;
106
- }
107
-
108
- /**
109
- * Saves the transient site tokens to the storage if modified
110
- * @param ctx
111
- * @returns {Promise<void>}
112
- */
113
- async #save(ctx) {
114
- if (this.#modified) {
115
- const storage = HelixStorage.fromContext(ctx).configBus();
116
- await storage.put(this.#key, JSON.stringify({
117
- tokens: this.#tokens,
118
- }));
119
- this.#modified = false;
120
- this.wasModified = true;
121
- }
122
- }
123
-
124
- /**
125
- * Returns the transient site token for the given partition. If the token does not exist or is
126
- * expired, it will be created.
127
- * @param ctx
128
- * @param partition
129
- * @returns {Promise<Token>}
130
- */
131
- async getOrCreateToken(ctx, partition) {
132
- if (partition !== 'preview' && partition !== 'live') {
133
- throw new Error(`Invalid partition: ${partition}`);
134
- }
135
- const tokens = await this.#load(ctx);
136
- let token = tokens[partition];
137
- if (!token) {
138
- const value = crypto.randomBytes(32).toString('base64url');
139
- const id = crypto.createHash('sha256').update(value).digest().toString('base64url');
140
- const created = this.#now.toUTCString();
141
- token = {
142
- id,
143
- value,
144
- created,
145
- };
146
- tokens[partition] = token;
147
- this.#modified = true;
148
- }
149
- await this.#save(ctx);
150
- return token;
151
- }
152
-
153
- /**
154
- * Returns the token header values for the given user id.
155
- * @param ctx
156
- * @param partition
157
- * @param userid
158
- * @returns {Promise<string>}
159
- */
160
- async getTokenHeader(ctx, partition, userid) {
161
- const token = await this.getOrCreateToken(ctx, partition);
162
- const user64 = Buffer.from(userid)
163
- .toString('base64url');
164
- const key = `${user64};${this.#now.toISOString().split('T')[0]}`;
165
- const hash = crypto
166
- .createHmac('sha512', key)
167
- .update(token.value, 'utf-8')
168
- .digest()
169
- .toString('base64url');
170
- return `hlxtst_${hash};${user64}`;
171
- }
172
-
173
- /**
174
- * Returns the transient site tokens
175
- * @param ctx
176
- * @returns {Promise<SiteTokens>}
177
- */
178
- async getSiteTokens(ctx) {
179
- return this.#load(ctx);
180
- }
181
- }