@darkauth/client 0.2.1 → 1.4.4
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/dist/index.js +20 -7
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -29,9 +29,19 @@ let cfg = {
|
|
|
29
29
|
};
|
|
30
30
|
const OBFUSCATION_KEY = "DarkAuth-Storage-Protection-2025";
|
|
31
31
|
const EMPTY_DRK = new Uint8Array(0);
|
|
32
|
+
const ID_TOKEN_KEY = "id_token";
|
|
32
33
|
export function setConfig(next) {
|
|
33
34
|
cfg = { ...cfg, ...next };
|
|
34
35
|
}
|
|
36
|
+
function setStoredIdToken(token) {
|
|
37
|
+
localStorage.setItem(ID_TOKEN_KEY, token);
|
|
38
|
+
}
|
|
39
|
+
function getStoredIdToken() {
|
|
40
|
+
return localStorage.getItem(ID_TOKEN_KEY);
|
|
41
|
+
}
|
|
42
|
+
function clearStoredIdToken() {
|
|
43
|
+
localStorage.removeItem(ID_TOKEN_KEY);
|
|
44
|
+
}
|
|
35
45
|
function bytesToBase64Url(bytes) {
|
|
36
46
|
let s = "";
|
|
37
47
|
for (const b of bytes)
|
|
@@ -158,7 +168,7 @@ export async function handleCallback() {
|
|
|
158
168
|
history.replaceState(null, "", location.origin + location.pathname);
|
|
159
169
|
}
|
|
160
170
|
catch { }
|
|
161
|
-
|
|
171
|
+
setStoredIdToken(idToken);
|
|
162
172
|
localStorage.removeItem("drk_protected");
|
|
163
173
|
if (refreshToken)
|
|
164
174
|
localStorage.setItem("refresh_token", refreshToken);
|
|
@@ -182,7 +192,7 @@ export async function handleCallback() {
|
|
|
182
192
|
history.replaceState(null, "", location.origin + location.pathname);
|
|
183
193
|
}
|
|
184
194
|
catch { }
|
|
185
|
-
|
|
195
|
+
setStoredIdToken(idToken);
|
|
186
196
|
const obfuscatedDrk = obfuscateKey(drk);
|
|
187
197
|
localStorage.setItem("drk_protected", bytesToBase64Url(obfuscatedDrk));
|
|
188
198
|
if (refreshToken)
|
|
@@ -190,7 +200,7 @@ export async function handleCallback() {
|
|
|
190
200
|
return { idToken, drk, refreshToken };
|
|
191
201
|
}
|
|
192
202
|
export function getStoredSession() {
|
|
193
|
-
const idToken =
|
|
203
|
+
const idToken = getStoredIdToken();
|
|
194
204
|
const obfuscatedDrkBase64 = localStorage.getItem("drk_protected");
|
|
195
205
|
if (!idToken)
|
|
196
206
|
return null;
|
|
@@ -224,14 +234,17 @@ export async function refreshSession() {
|
|
|
224
234
|
});
|
|
225
235
|
if (!response.ok) {
|
|
226
236
|
if (response.status === 401) {
|
|
227
|
-
localStorage.
|
|
237
|
+
const latestRefreshToken = localStorage.getItem("refresh_token");
|
|
238
|
+
if (latestRefreshToken === refreshToken) {
|
|
239
|
+
localStorage.removeItem("refresh_token");
|
|
240
|
+
}
|
|
228
241
|
}
|
|
229
242
|
return null;
|
|
230
243
|
}
|
|
231
244
|
const tokenResponse = await response.json();
|
|
232
245
|
const idToken = tokenResponse.id_token;
|
|
233
246
|
const newRefreshToken = tokenResponse.refresh_token;
|
|
234
|
-
|
|
247
|
+
setStoredIdToken(idToken);
|
|
235
248
|
if (newRefreshToken)
|
|
236
249
|
localStorage.setItem("refresh_token", newRefreshToken);
|
|
237
250
|
const obfuscatedDrkBase64 = localStorage.getItem("drk_protected");
|
|
@@ -242,14 +255,14 @@ export async function refreshSession() {
|
|
|
242
255
|
return { idToken, drk, refreshToken: newRefreshToken || refreshToken };
|
|
243
256
|
}
|
|
244
257
|
export function logout() {
|
|
245
|
-
|
|
258
|
+
clearStoredIdToken();
|
|
246
259
|
localStorage.removeItem("drk_protected");
|
|
247
260
|
sessionStorage.removeItem("zk_eph_priv_jwk");
|
|
248
261
|
sessionStorage.removeItem("pkce_verifier");
|
|
249
262
|
localStorage.removeItem("refresh_token");
|
|
250
263
|
}
|
|
251
264
|
export function getCurrentUser() {
|
|
252
|
-
const idToken =
|
|
265
|
+
const idToken = getStoredIdToken();
|
|
253
266
|
if (!idToken)
|
|
254
267
|
return null;
|
|
255
268
|
return parseJwt(idToken);
|