supaapps-auth 3.0.1 → 3.2.2
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.d.ts +4 -1
- package/dist/index.js +38 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -36,11 +36,13 @@ declare enum Platforms {
|
|
|
36
36
|
GITHUB = "github",
|
|
37
37
|
APPLE = "apple",
|
|
38
38
|
LINKEDIN = "linkedin",
|
|
39
|
-
MICROSOFT = "microsoft"
|
|
39
|
+
MICROSOFT = "microsoft",
|
|
40
|
+
SUPPORT = "support"
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
declare class AuthManager {
|
|
43
44
|
private static instance;
|
|
45
|
+
private readonly tokenExpiryLeewaySeconds;
|
|
44
46
|
private authServer;
|
|
45
47
|
private realmName;
|
|
46
48
|
private redirectUri;
|
|
@@ -80,6 +82,7 @@ declare class AuthManager {
|
|
|
80
82
|
private saveTokens;
|
|
81
83
|
loginUsingEmail(email: string, password: string): Promise<void>;
|
|
82
84
|
loginUsingPkce(code: string): Promise<void>;
|
|
85
|
+
loginUsingImpersonation(code: string, redirectUri?: string): Promise<void>;
|
|
83
86
|
logout(): Promise<void>;
|
|
84
87
|
static validateToken(authServer: string, bearerToken: string): Promise<UserTokenPayload>;
|
|
85
88
|
static resetInstance(): void;
|
package/dist/index.js
CHANGED
|
@@ -18,6 +18,7 @@ var Platforms = /* @__PURE__ */ ((Platforms2) => {
|
|
|
18
18
|
Platforms2["APPLE"] = "apple";
|
|
19
19
|
Platforms2["LINKEDIN"] = "linkedin";
|
|
20
20
|
Platforms2["MICROSOFT"] = "microsoft";
|
|
21
|
+
Platforms2["SUPPORT"] = "support";
|
|
21
22
|
return Platforms2;
|
|
22
23
|
})(Platforms || {});
|
|
23
24
|
|
|
@@ -67,6 +68,7 @@ function generateCodeChallenge(verifier) {
|
|
|
67
68
|
// src/AuthManager.ts
|
|
68
69
|
var AuthManager = class _AuthManager {
|
|
69
70
|
static instance = null;
|
|
71
|
+
tokenExpiryLeewaySeconds = 10;
|
|
70
72
|
authServer;
|
|
71
73
|
realmName;
|
|
72
74
|
redirectUri;
|
|
@@ -107,10 +109,10 @@ var AuthManager = class _AuthManager {
|
|
|
107
109
|
return JSON.parse(atob(token.split(".")[1]));
|
|
108
110
|
}
|
|
109
111
|
generatePKCEPair() {
|
|
110
|
-
const verifier = localStorage.getItem("
|
|
111
|
-
const challenge = localStorage.getItem("
|
|
112
|
-
localStorage.setItem("
|
|
113
|
-
localStorage.setItem("
|
|
112
|
+
const verifier = localStorage.getItem("code_verifier") ?? generateCodeVerifier();
|
|
113
|
+
const challenge = localStorage.getItem("code_challenge") ?? generateCodeChallenge(verifier);
|
|
114
|
+
localStorage.setItem("code_verifier", verifier);
|
|
115
|
+
localStorage.setItem("code_challenge", challenge);
|
|
114
116
|
return { verifier, challenge };
|
|
115
117
|
}
|
|
116
118
|
async refreshAccessToken(isInitialization = false) {
|
|
@@ -140,13 +142,17 @@ var AuthManager = class _AuthManager {
|
|
|
140
142
|
async checkAccessToken(isInitilization = false) {
|
|
141
143
|
const accessToken = localStorage.getItem("access_token");
|
|
142
144
|
if (accessToken && this.isTokenExpired(accessToken)) {
|
|
145
|
+
console.info(
|
|
146
|
+
"[AuthManager] Access token expired or near expiry; refreshing."
|
|
147
|
+
);
|
|
143
148
|
return this.refreshAccessToken(isInitilization);
|
|
144
149
|
}
|
|
145
150
|
return accessToken;
|
|
146
151
|
}
|
|
147
152
|
isTokenExpired(token) {
|
|
148
153
|
const decoded = this.tokenToPayload(token);
|
|
149
|
-
|
|
154
|
+
const nowSeconds = Date.now() / 1e3;
|
|
155
|
+
return decoded.exp <= nowSeconds + this.tokenExpiryLeewaySeconds;
|
|
150
156
|
}
|
|
151
157
|
async mustBeLoggedIn() {
|
|
152
158
|
if (!await this.isLoggedIn()) {
|
|
@@ -167,6 +173,9 @@ var AuthManager = class _AuthManager {
|
|
|
167
173
|
}
|
|
168
174
|
if (this.isTokenExpired(accessToken)) {
|
|
169
175
|
try {
|
|
176
|
+
console.info(
|
|
177
|
+
"[AuthManager] Access token expired or near expiry; refreshing."
|
|
178
|
+
);
|
|
170
179
|
await this.refreshAccessToken();
|
|
171
180
|
return true;
|
|
172
181
|
} catch {
|
|
@@ -365,17 +374,18 @@ var AuthManager = class _AuthManager {
|
|
|
365
374
|
this.saveTokens(response, false);
|
|
366
375
|
}
|
|
367
376
|
saveTokens(response, byRefresh) {
|
|
377
|
+
const eventType = byRefresh ? "user-updated" /* USER_UPDATED */ : "user-logged-in" /* USER_LOGGED_IN */;
|
|
378
|
+
const userPayload = this.tokenToPayload(response.data.access_token);
|
|
368
379
|
localStorage.setItem("access_token", response.data.access_token);
|
|
369
380
|
localStorage.setItem(
|
|
370
381
|
"refresh_token",
|
|
371
382
|
response.data.refresh_token
|
|
372
383
|
);
|
|
373
384
|
this.onStateChange({
|
|
374
|
-
type:
|
|
375
|
-
user:
|
|
385
|
+
type: eventType,
|
|
386
|
+
user: userPayload
|
|
376
387
|
});
|
|
377
|
-
|
|
378
|
-
localStorage.setItem("user", JSON.stringify(user));
|
|
388
|
+
localStorage.setItem("user", JSON.stringify(userPayload));
|
|
379
389
|
}
|
|
380
390
|
async loginUsingEmail(email, password) {
|
|
381
391
|
const response = await axios.post(
|
|
@@ -393,7 +403,7 @@ var AuthManager = class _AuthManager {
|
|
|
393
403
|
}
|
|
394
404
|
async loginUsingPkce(code) {
|
|
395
405
|
try {
|
|
396
|
-
const codeVerifier = localStorage.getItem("
|
|
406
|
+
const codeVerifier = localStorage.getItem("code_verifier");
|
|
397
407
|
if (!codeVerifier) {
|
|
398
408
|
throw new Error("Code verifier not found");
|
|
399
409
|
}
|
|
@@ -408,10 +418,26 @@ var AuthManager = class _AuthManager {
|
|
|
408
418
|
);
|
|
409
419
|
this.saveTokens(response, false);
|
|
410
420
|
} finally {
|
|
411
|
-
localStorage.removeItem("
|
|
412
|
-
localStorage.removeItem("
|
|
421
|
+
localStorage.removeItem("code_verifier");
|
|
422
|
+
localStorage.removeItem("code_challenge");
|
|
413
423
|
}
|
|
414
424
|
}
|
|
425
|
+
async loginUsingImpersonation(code, redirectUri) {
|
|
426
|
+
const response = await axios.post(
|
|
427
|
+
`${this.authServer}auth/impersonation_exchange`,
|
|
428
|
+
{
|
|
429
|
+
code,
|
|
430
|
+
...redirectUri ? { redirect_uri: redirectUri } : {}
|
|
431
|
+
}
|
|
432
|
+
);
|
|
433
|
+
if (response.data.message || response.data.error) {
|
|
434
|
+
throw new Error(response.data.message || response.data.error);
|
|
435
|
+
}
|
|
436
|
+
if (!response.data.access_token) {
|
|
437
|
+
throw new Error("Something went wrong");
|
|
438
|
+
}
|
|
439
|
+
this.saveTokens(response, false);
|
|
440
|
+
}
|
|
415
441
|
async logout() {
|
|
416
442
|
try {
|
|
417
443
|
const accessToken = localStorage.getItem("access_token");
|