@docyrus/signin 0.18.0 → 0.18.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/README.md +11 -0
- package/dist/{constants-C9ezX9zG.d.ts → constants-CvMiR-RM.d.ts} +29 -5
- package/dist/core-index.d.ts +1 -1
- package/dist/core-index.js +90 -30
- package/dist/core-index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +90 -30
- package/dist/index.js.map +1 -1
- package/dist/nextjs/index.js.map +1 -1
- package/dist/react-native/index.d.ts +1 -1
- package/dist/react-native/index.js +90 -30
- package/dist/react-native/index.js.map +1 -1
- package/dist/vue/index.js +90 -30
- package/dist/vue/index.js.map +1 -1
- package/package.json +2 -2
package/dist/vue/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var DEFAULT_CALLBACK_PATH = "/auth/callback";
|
|
|
16
16
|
var DEFAULT_ALLOWED_HOST_PATTERN = /^https:\/\/[^/]+\.docyrus\.app$/;
|
|
17
17
|
var REDIRECT_RETURN_KEY = "docyrus_auth_return_url";
|
|
18
18
|
var TOKEN_EXPIRY_BUFFER_SECONDS = 60;
|
|
19
|
+
var MIN_TOKEN_REFRESH_INTERVAL_MS = 1e4;
|
|
19
20
|
var IFRAME_TOKEN_REFRESH_TIMEOUT_MS = 1e4;
|
|
20
21
|
var HOST_ACTION_TIMEOUT_MS = 15e3;
|
|
21
22
|
var MSGRAPH_REQUEST_TIMEOUT_MS = 3e4;
|
|
@@ -152,18 +153,34 @@ var StandaloneOAuth2Auth = class {
|
|
|
152
153
|
};
|
|
153
154
|
|
|
154
155
|
// src/core/jwt.ts
|
|
155
|
-
|
|
156
|
+
var PER_ISSUANCE_CLAIMS = /* @__PURE__ */ new Set([
|
|
157
|
+
"exp",
|
|
158
|
+
"iat",
|
|
159
|
+
"nbf",
|
|
160
|
+
"jti"
|
|
161
|
+
]);
|
|
162
|
+
function decodeJwtPayload(accessToken) {
|
|
156
163
|
const parts = accessToken?.split(".");
|
|
157
164
|
if (!parts || parts.length < 2) return null;
|
|
158
165
|
try {
|
|
159
166
|
const payload = JSON.parse(
|
|
160
167
|
atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
|
|
161
168
|
);
|
|
162
|
-
return typeof payload
|
|
169
|
+
return payload && typeof payload === "object" ? payload : null;
|
|
163
170
|
} catch {
|
|
164
171
|
return null;
|
|
165
172
|
}
|
|
166
173
|
}
|
|
174
|
+
function parseJwtExp(accessToken) {
|
|
175
|
+
const exp = decodeJwtPayload(accessToken)?.["exp"];
|
|
176
|
+
return typeof exp === "number" ? exp : null;
|
|
177
|
+
}
|
|
178
|
+
function getJwtSessionKey(accessToken) {
|
|
179
|
+
const payload = decodeJwtPayload(accessToken);
|
|
180
|
+
if (!payload) return null;
|
|
181
|
+
const stableClaims = Object.keys(payload).filter((claim) => !PER_ISSUANCE_CLAIMS.has(claim)).sort().map((claim) => [claim, payload[claim]]);
|
|
182
|
+
return JSON.stringify(stableClaims);
|
|
183
|
+
}
|
|
167
184
|
|
|
168
185
|
// src/core/iframe-auth.ts
|
|
169
186
|
function postToHost(data) {
|
|
@@ -1088,12 +1105,18 @@ var AuthManager = class {
|
|
|
1088
1105
|
error = null;
|
|
1089
1106
|
client = null;
|
|
1090
1107
|
user = null;
|
|
1108
|
+
/** In-flight `/v1/users/me` request, tied to the client that issued it. */
|
|
1109
|
+
userFetch = null;
|
|
1091
1110
|
standaloneAuth = null;
|
|
1092
1111
|
iframeAuth = null;
|
|
1093
1112
|
guidyBridge = null;
|
|
1094
1113
|
msGraphClient = null;
|
|
1095
1114
|
reactNativeAuth = null;
|
|
1096
1115
|
tokenRefreshTimer = null;
|
|
1116
|
+
/** When the refresh timer last fired (ms), to keep proactive refreshes apart. */
|
|
1117
|
+
lastScheduledRefreshAt = 0;
|
|
1118
|
+
/** Session the current client was created for — see `getJwtSessionKey`. */
|
|
1119
|
+
sessionKey = null;
|
|
1097
1120
|
/** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
|
|
1098
1121
|
refreshInFlight = null;
|
|
1099
1122
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -1485,40 +1508,82 @@ var AuthManager = class {
|
|
|
1485
1508
|
}
|
|
1486
1509
|
this.tokens = null;
|
|
1487
1510
|
this.client = null;
|
|
1511
|
+
this.sessionKey = null;
|
|
1488
1512
|
this.user = null;
|
|
1489
1513
|
this.status = "unauthenticated";
|
|
1490
1514
|
this.error = null;
|
|
1491
1515
|
this.notify();
|
|
1492
1516
|
}
|
|
1493
1517
|
/**
|
|
1494
|
-
* Called when valid tokens are received (
|
|
1495
|
-
*
|
|
1496
|
-
*
|
|
1497
|
-
*
|
|
1518
|
+
* Called when valid tokens are received (any mode). In iframe mode that is
|
|
1519
|
+
* every host `signin` message — the first sign-in, the host's answer to each
|
|
1520
|
+
* `token-refresh-request`, and repeats of a token we already hold (the host
|
|
1521
|
+
* answering more than one `signin-ready`, e.g. under React StrictMode).
|
|
1522
|
+
*
|
|
1523
|
+
* The RestApiClient reads tokens lazily through its TokenManager, so a
|
|
1524
|
+
* refreshed token needs no new client. A new client — plus a user fetch —
|
|
1525
|
+
* is reserved for a new session (first sign-in, or the host switching user
|
|
1526
|
+
* or tenant): consumers key caches on client identity, so a fresh instance
|
|
1527
|
+
* makes them reload everything.
|
|
1528
|
+
*
|
|
1529
|
+
* On a new session the first notify() fires with user: null, and a second
|
|
1530
|
+
* one fires once the user is fetched.
|
|
1498
1531
|
*/
|
|
1499
1532
|
setAuthenticated(tokens) {
|
|
1533
|
+
const isRepeat = this.status === "authenticated" && tokens.accessToken === this.tokens?.accessToken && tokens.refreshToken === this.tokens?.refreshToken;
|
|
1534
|
+
if (isRepeat) {
|
|
1535
|
+
this.scheduleTokenRefresh(tokens);
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
const sessionKey = getJwtSessionKey(tokens.accessToken);
|
|
1539
|
+
const isNewSession = !this.client || sessionKey === null || sessionKey !== this.sessionKey;
|
|
1500
1540
|
this.tokens = tokens;
|
|
1541
|
+
this.sessionKey = sessionKey;
|
|
1501
1542
|
this.error = null;
|
|
1502
1543
|
this.status = "authenticated";
|
|
1503
1544
|
if (this.config.ssr) {
|
|
1504
1545
|
syncTokenToCookie(tokens.accessToken, this.config.ssrCookieKey, tokens.expiresAt);
|
|
1505
1546
|
}
|
|
1506
|
-
|
|
1547
|
+
if (isNewSession) {
|
|
1548
|
+
this.createClient();
|
|
1549
|
+
}
|
|
1507
1550
|
this.scheduleTokenRefresh(tokens);
|
|
1508
1551
|
this.notify();
|
|
1509
|
-
this.
|
|
1510
|
-
|
|
1511
|
-
/** Fetch the current user from /v1/users/me. Silently sets user to null on failure. */
|
|
1512
|
-
async fetchUser() {
|
|
1513
|
-
if (!this.client) return;
|
|
1514
|
-
try {
|
|
1515
|
-
const response = await this.client.get("/v1/users/me");
|
|
1516
|
-
const maybeUser = response;
|
|
1517
|
-
this.user = maybeUser && typeof maybeUser === "object" && "id" in maybeUser && "email" in maybeUser ? maybeUser : response.data;
|
|
1518
|
-
} catch {
|
|
1519
|
-
this.user = null;
|
|
1552
|
+
if (isNewSession || !this.user) {
|
|
1553
|
+
void this.fetchUser();
|
|
1520
1554
|
}
|
|
1521
|
-
|
|
1555
|
+
}
|
|
1556
|
+
/**
|
|
1557
|
+
* Fetch the current user from /v1/users/me. Silently sets user to null on failure.
|
|
1558
|
+
*
|
|
1559
|
+
* Concurrent calls share one request: `setAuthenticated` fetches whenever the
|
|
1560
|
+
* user is still missing, so a second token push that lands while the first
|
|
1561
|
+
* fetch is in flight (e.g. stored tokens restored on load, then the host or a
|
|
1562
|
+
* bridge pushing its session) would otherwise fire a duplicate request. A
|
|
1563
|
+
* result is dropped if the client changed meanwhile (sign-out or a new
|
|
1564
|
+
* session), so a stale user never overwrites the current one.
|
|
1565
|
+
*/
|
|
1566
|
+
fetchUser() {
|
|
1567
|
+
const { client } = this;
|
|
1568
|
+
if (!client) return Promise.resolve();
|
|
1569
|
+
if (this.userFetch?.client === client) return this.userFetch.promise;
|
|
1570
|
+
const promise = (async () => {
|
|
1571
|
+
let user = null;
|
|
1572
|
+
try {
|
|
1573
|
+
const response = await client.get("/v1/users/me");
|
|
1574
|
+
const maybeUser = response;
|
|
1575
|
+
user = maybeUser && typeof maybeUser === "object" && "id" in maybeUser && "email" in maybeUser ? maybeUser : response.data;
|
|
1576
|
+
} catch {
|
|
1577
|
+
user = null;
|
|
1578
|
+
}
|
|
1579
|
+
if (this.client !== client) return;
|
|
1580
|
+
this.user = user;
|
|
1581
|
+
this.notify();
|
|
1582
|
+
})().finally(() => {
|
|
1583
|
+
if (this.userFetch?.promise === promise) this.userFetch = null;
|
|
1584
|
+
});
|
|
1585
|
+
this.userFetch = { client, promise };
|
|
1586
|
+
return promise;
|
|
1522
1587
|
}
|
|
1523
1588
|
/**
|
|
1524
1589
|
* Create a RestApiClient with a custom TokenManager that proactively
|
|
@@ -1617,18 +1682,13 @@ var AuthManager = class {
|
|
|
1617
1682
|
if (!this.autoRefreshEnabled) return;
|
|
1618
1683
|
const expiresAt = this.resolveExpiry(tokens);
|
|
1619
1684
|
if (!expiresAt) return;
|
|
1620
|
-
const
|
|
1621
|
-
const
|
|
1622
|
-
const
|
|
1623
|
-
|
|
1624
|
-
this.handleTokenRefresh().catch((err) => {
|
|
1625
|
-
this.error = err instanceof Error ? err : new Error(String(err));
|
|
1626
|
-
this.status = "unauthenticated";
|
|
1627
|
-
this.notify();
|
|
1628
|
-
});
|
|
1629
|
-
return;
|
|
1630
|
-
}
|
|
1685
|
+
const now = Date.now();
|
|
1686
|
+
const dueInMs = expiresAt * 1e3 - TOKEN_EXPIRY_BUFFER_SECONDS * 1e3 - now;
|
|
1687
|
+
const throttleMs = this.lastScheduledRefreshAt + MIN_TOKEN_REFRESH_INTERVAL_MS - now;
|
|
1688
|
+
const refreshInMs = Math.max(dueInMs, throttleMs, 0);
|
|
1631
1689
|
this.tokenRefreshTimer = setTimeout(() => {
|
|
1690
|
+
this.tokenRefreshTimer = null;
|
|
1691
|
+
this.lastScheduledRefreshAt = Date.now();
|
|
1632
1692
|
this.handleTokenRefresh().catch((err) => {
|
|
1633
1693
|
this.error = err instanceof Error ? err : new Error(String(err));
|
|
1634
1694
|
this.status = "unauthenticated";
|