@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/README.md
CHANGED
|
@@ -694,6 +694,17 @@ Used when the app is embedded in an iframe on `*.docyrus.app`. The flow:
|
|
|
694
694
|
4. When tokens expire, provider sends `{ type: 'token-refresh-request' }` to host
|
|
695
695
|
5. Host responds with fresh tokens
|
|
696
696
|
|
|
697
|
+
The host may post `signin` as often as it likes. Only a new session — the
|
|
698
|
+
first token, or one for a different user/tenant — creates a new `client` and
|
|
699
|
+
re-fetches the user. Repeats of the current token are ignored, and refreshed
|
|
700
|
+
tokens for the same session just update `tokens`, so `client` keeps its
|
|
701
|
+
identity and anything keyed on it (query caches, inventory) stays warm. Two
|
|
702
|
+
tokens belong to the same session when their JWT payloads match apart from
|
|
703
|
+
`exp`, `iat`, `nbf` and `jti`; a token that isn't a decodable JWT always counts
|
|
704
|
+
as a new session. Proactive refreshes are spaced at least 10s apart, so a host
|
|
705
|
+
that answers with a token still inside the 60s expiry buffer can't cause a
|
|
706
|
+
refresh loop.
|
|
707
|
+
|
|
697
708
|
In addition to auth messages, the host may push two side-channel messages
|
|
698
709
|
that the embedded app can subscribe to:
|
|
699
710
|
|
|
@@ -19,12 +19,18 @@ declare class AuthManager {
|
|
|
19
19
|
private error;
|
|
20
20
|
private client;
|
|
21
21
|
private user;
|
|
22
|
+
/** In-flight `/v1/users/me` request, tied to the client that issued it. */
|
|
23
|
+
private userFetch;
|
|
22
24
|
private standaloneAuth;
|
|
23
25
|
private iframeAuth;
|
|
24
26
|
private guidyBridge;
|
|
25
27
|
private msGraphClient;
|
|
26
28
|
private reactNativeAuth;
|
|
27
29
|
private tokenRefreshTimer;
|
|
30
|
+
/** When the refresh timer last fired (ms), to keep proactive refreshes apart. */
|
|
31
|
+
private lastScheduledRefreshAt;
|
|
32
|
+
/** Session the current client was created for — see `getJwtSessionKey`. */
|
|
33
|
+
private sessionKey;
|
|
28
34
|
/** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
|
|
29
35
|
private refreshInFlight;
|
|
30
36
|
private listeners;
|
|
@@ -152,13 +158,31 @@ declare class AuthManager {
|
|
|
152
158
|
*/
|
|
153
159
|
signOut(): Promise<void>;
|
|
154
160
|
/**
|
|
155
|
-
* Called when valid tokens are received (
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
161
|
+
* Called when valid tokens are received (any mode). In iframe mode that is
|
|
162
|
+
* every host `signin` message — the first sign-in, the host's answer to each
|
|
163
|
+
* `token-refresh-request`, and repeats of a token we already hold (the host
|
|
164
|
+
* answering more than one `signin-ready`, e.g. under React StrictMode).
|
|
165
|
+
*
|
|
166
|
+
* The RestApiClient reads tokens lazily through its TokenManager, so a
|
|
167
|
+
* refreshed token needs no new client. A new client — plus a user fetch —
|
|
168
|
+
* is reserved for a new session (first sign-in, or the host switching user
|
|
169
|
+
* or tenant): consumers key caches on client identity, so a fresh instance
|
|
170
|
+
* makes them reload everything.
|
|
171
|
+
*
|
|
172
|
+
* On a new session the first notify() fires with user: null, and a second
|
|
173
|
+
* one fires once the user is fetched.
|
|
159
174
|
*/
|
|
160
175
|
private setAuthenticated;
|
|
161
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* Fetch the current user from /v1/users/me. Silently sets user to null on failure.
|
|
178
|
+
*
|
|
179
|
+
* Concurrent calls share one request: `setAuthenticated` fetches whenever the
|
|
180
|
+
* user is still missing, so a second token push that lands while the first
|
|
181
|
+
* fetch is in flight (e.g. stored tokens restored on load, then the host or a
|
|
182
|
+
* bridge pushing its session) would otherwise fire a duplicate request. A
|
|
183
|
+
* result is dropped if the client changed meanwhile (sign-out or a new
|
|
184
|
+
* session), so a stale user never overwrites the current one.
|
|
185
|
+
*/
|
|
162
186
|
private fetchUser;
|
|
163
187
|
/**
|
|
164
188
|
* Create a RestApiClient with a custom TokenManager that proactively
|
package/dist/core-index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AuthManager, f as AuthStateListener, D as DEFAULT_API_URL, d as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from './constants-
|
|
1
|
+
export { A as AuthManager, f as AuthStateListener, D as DEFAULT_API_URL, d as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from './constants-CvMiR-RM.js';
|
|
2
2
|
import { OAuth2Tokens } from '@docyrus/api-client';
|
|
3
3
|
import { D as DocyrusAuthConfig, T as GuidyElementsMessage, W as GuidyRoutesMessage, U as GuidyPointAckMessage, G as GuidyRoute, Q as GuidyCommandMessage, c as AuthMode } from './types-BcLDyMdw.js';
|
|
4
4
|
export { A as AclOperation, a as AclTargetType, b as AdaptiveCard, x as AppNavigationRequestMessage, y as AppRouteChangeMessage, e as AuthStatus, C as CalendarSendAckMessage, z as CalendarSendMessage, B as ChatSendAckMessage, E as ChatSendMessage, F as DocySendAckMessage, I as DocySendMessage, f as DocyrusAclRule, g as DocyrusAuthContextValue, h as DocyrusCalendarEventPayload, i as DocyrusEmailPayload, J as DocyrusNotification, j as DocyrusPermission, k as DocyrusRole, l as DocyrusShareAdaptiveCardPayload, m as DocyrusSharePayload, n as DocyrusShareTextPayload, o as DocyrusUser, K as EmailSendAckMessage, L as EmailSendMessage, N as GuidyCommandHandler, S as GuidyElement, V as GuidyPointMessage, X as GuidyScanMessage, H as HostNavigationHandler, Y as HostNavigationMessage, w as HostNavigationRequestOptions, v as HostNotificationHandler, Z as HostNotificationMessage, _ as HostSignInMessage, $ as IframeAuth, M as MsGraphAccount, p as MsGraphClient, q as MsGraphRequest, a0 as MsGraphRequestMessage, r as MsGraphRequestOptions, s as MsGraphResponse, a1 as MsGraphResponseMessage, t as MsGraphStatus, a2 as MsGraphStatusRequestMessage, a3 as MsGraphStatusResponseMessage, P as PermissionConfig, u as PermissionScope, R as RouteChangePayload, a4 as TokenRefreshRequestMessage } from './types-BcLDyMdw.js';
|
package/dist/core-index.js
CHANGED
|
@@ -15,6 +15,7 @@ var DEFAULT_CALLBACK_PATH = "/auth/callback";
|
|
|
15
15
|
var DEFAULT_ALLOWED_HOST_PATTERN = /^https:\/\/[^/]+\.docyrus\.app$/;
|
|
16
16
|
var REDIRECT_RETURN_KEY = "docyrus_auth_return_url";
|
|
17
17
|
var TOKEN_EXPIRY_BUFFER_SECONDS = 60;
|
|
18
|
+
var MIN_TOKEN_REFRESH_INTERVAL_MS = 1e4;
|
|
18
19
|
var IFRAME_TOKEN_REFRESH_TIMEOUT_MS = 1e4;
|
|
19
20
|
var HOST_ACTION_TIMEOUT_MS = 15e3;
|
|
20
21
|
var MSGRAPH_REQUEST_TIMEOUT_MS = 3e4;
|
|
@@ -151,18 +152,34 @@ var StandaloneOAuth2Auth = class {
|
|
|
151
152
|
};
|
|
152
153
|
|
|
153
154
|
// src/core/jwt.ts
|
|
154
|
-
|
|
155
|
+
var PER_ISSUANCE_CLAIMS = /* @__PURE__ */ new Set([
|
|
156
|
+
"exp",
|
|
157
|
+
"iat",
|
|
158
|
+
"nbf",
|
|
159
|
+
"jti"
|
|
160
|
+
]);
|
|
161
|
+
function decodeJwtPayload(accessToken) {
|
|
155
162
|
const parts = accessToken?.split(".");
|
|
156
163
|
if (!parts || parts.length < 2) return null;
|
|
157
164
|
try {
|
|
158
165
|
const payload = JSON.parse(
|
|
159
166
|
atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
|
|
160
167
|
);
|
|
161
|
-
return typeof payload
|
|
168
|
+
return payload && typeof payload === "object" ? payload : null;
|
|
162
169
|
} catch {
|
|
163
170
|
return null;
|
|
164
171
|
}
|
|
165
172
|
}
|
|
173
|
+
function parseJwtExp(accessToken) {
|
|
174
|
+
const exp = decodeJwtPayload(accessToken)?.["exp"];
|
|
175
|
+
return typeof exp === "number" ? exp : null;
|
|
176
|
+
}
|
|
177
|
+
function getJwtSessionKey(accessToken) {
|
|
178
|
+
const payload = decodeJwtPayload(accessToken);
|
|
179
|
+
if (!payload) return null;
|
|
180
|
+
const stableClaims = Object.keys(payload).filter((claim) => !PER_ISSUANCE_CLAIMS.has(claim)).sort().map((claim) => [claim, payload[claim]]);
|
|
181
|
+
return JSON.stringify(stableClaims);
|
|
182
|
+
}
|
|
166
183
|
|
|
167
184
|
// src/core/iframe-auth.ts
|
|
168
185
|
function postToHost(data) {
|
|
@@ -1087,12 +1104,18 @@ var AuthManager = class {
|
|
|
1087
1104
|
error = null;
|
|
1088
1105
|
client = null;
|
|
1089
1106
|
user = null;
|
|
1107
|
+
/** In-flight `/v1/users/me` request, tied to the client that issued it. */
|
|
1108
|
+
userFetch = null;
|
|
1090
1109
|
standaloneAuth = null;
|
|
1091
1110
|
iframeAuth = null;
|
|
1092
1111
|
guidyBridge = null;
|
|
1093
1112
|
msGraphClient = null;
|
|
1094
1113
|
reactNativeAuth = null;
|
|
1095
1114
|
tokenRefreshTimer = null;
|
|
1115
|
+
/** When the refresh timer last fired (ms), to keep proactive refreshes apart. */
|
|
1116
|
+
lastScheduledRefreshAt = 0;
|
|
1117
|
+
/** Session the current client was created for — see `getJwtSessionKey`. */
|
|
1118
|
+
sessionKey = null;
|
|
1096
1119
|
/** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
|
|
1097
1120
|
refreshInFlight = null;
|
|
1098
1121
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -1484,40 +1507,82 @@ var AuthManager = class {
|
|
|
1484
1507
|
}
|
|
1485
1508
|
this.tokens = null;
|
|
1486
1509
|
this.client = null;
|
|
1510
|
+
this.sessionKey = null;
|
|
1487
1511
|
this.user = null;
|
|
1488
1512
|
this.status = "unauthenticated";
|
|
1489
1513
|
this.error = null;
|
|
1490
1514
|
this.notify();
|
|
1491
1515
|
}
|
|
1492
1516
|
/**
|
|
1493
|
-
* Called when valid tokens are received (
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1496
|
-
*
|
|
1517
|
+
* Called when valid tokens are received (any mode). In iframe mode that is
|
|
1518
|
+
* every host `signin` message — the first sign-in, the host's answer to each
|
|
1519
|
+
* `token-refresh-request`, and repeats of a token we already hold (the host
|
|
1520
|
+
* answering more than one `signin-ready`, e.g. under React StrictMode).
|
|
1521
|
+
*
|
|
1522
|
+
* The RestApiClient reads tokens lazily through its TokenManager, so a
|
|
1523
|
+
* refreshed token needs no new client. A new client — plus a user fetch —
|
|
1524
|
+
* is reserved for a new session (first sign-in, or the host switching user
|
|
1525
|
+
* or tenant): consumers key caches on client identity, so a fresh instance
|
|
1526
|
+
* makes them reload everything.
|
|
1527
|
+
*
|
|
1528
|
+
* On a new session the first notify() fires with user: null, and a second
|
|
1529
|
+
* one fires once the user is fetched.
|
|
1497
1530
|
*/
|
|
1498
1531
|
setAuthenticated(tokens) {
|
|
1532
|
+
const isRepeat = this.status === "authenticated" && tokens.accessToken === this.tokens?.accessToken && tokens.refreshToken === this.tokens?.refreshToken;
|
|
1533
|
+
if (isRepeat) {
|
|
1534
|
+
this.scheduleTokenRefresh(tokens);
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
const sessionKey = getJwtSessionKey(tokens.accessToken);
|
|
1538
|
+
const isNewSession = !this.client || sessionKey === null || sessionKey !== this.sessionKey;
|
|
1499
1539
|
this.tokens = tokens;
|
|
1540
|
+
this.sessionKey = sessionKey;
|
|
1500
1541
|
this.error = null;
|
|
1501
1542
|
this.status = "authenticated";
|
|
1502
1543
|
if (this.config.ssr) {
|
|
1503
1544
|
syncTokenToCookie(tokens.accessToken, this.config.ssrCookieKey, tokens.expiresAt);
|
|
1504
1545
|
}
|
|
1505
|
-
|
|
1546
|
+
if (isNewSession) {
|
|
1547
|
+
this.createClient();
|
|
1548
|
+
}
|
|
1506
1549
|
this.scheduleTokenRefresh(tokens);
|
|
1507
1550
|
this.notify();
|
|
1508
|
-
this.
|
|
1509
|
-
|
|
1510
|
-
/** Fetch the current user from /v1/users/me. Silently sets user to null on failure. */
|
|
1511
|
-
async fetchUser() {
|
|
1512
|
-
if (!this.client) return;
|
|
1513
|
-
try {
|
|
1514
|
-
const response = await this.client.get("/v1/users/me");
|
|
1515
|
-
const maybeUser = response;
|
|
1516
|
-
this.user = maybeUser && typeof maybeUser === "object" && "id" in maybeUser && "email" in maybeUser ? maybeUser : response.data;
|
|
1517
|
-
} catch {
|
|
1518
|
-
this.user = null;
|
|
1551
|
+
if (isNewSession || !this.user) {
|
|
1552
|
+
void this.fetchUser();
|
|
1519
1553
|
}
|
|
1520
|
-
|
|
1554
|
+
}
|
|
1555
|
+
/**
|
|
1556
|
+
* Fetch the current user from /v1/users/me. Silently sets user to null on failure.
|
|
1557
|
+
*
|
|
1558
|
+
* Concurrent calls share one request: `setAuthenticated` fetches whenever the
|
|
1559
|
+
* user is still missing, so a second token push that lands while the first
|
|
1560
|
+
* fetch is in flight (e.g. stored tokens restored on load, then the host or a
|
|
1561
|
+
* bridge pushing its session) would otherwise fire a duplicate request. A
|
|
1562
|
+
* result is dropped if the client changed meanwhile (sign-out or a new
|
|
1563
|
+
* session), so a stale user never overwrites the current one.
|
|
1564
|
+
*/
|
|
1565
|
+
fetchUser() {
|
|
1566
|
+
const { client } = this;
|
|
1567
|
+
if (!client) return Promise.resolve();
|
|
1568
|
+
if (this.userFetch?.client === client) return this.userFetch.promise;
|
|
1569
|
+
const promise = (async () => {
|
|
1570
|
+
let user = null;
|
|
1571
|
+
try {
|
|
1572
|
+
const response = await client.get("/v1/users/me");
|
|
1573
|
+
const maybeUser = response;
|
|
1574
|
+
user = maybeUser && typeof maybeUser === "object" && "id" in maybeUser && "email" in maybeUser ? maybeUser : response.data;
|
|
1575
|
+
} catch {
|
|
1576
|
+
user = null;
|
|
1577
|
+
}
|
|
1578
|
+
if (this.client !== client) return;
|
|
1579
|
+
this.user = user;
|
|
1580
|
+
this.notify();
|
|
1581
|
+
})().finally(() => {
|
|
1582
|
+
if (this.userFetch?.promise === promise) this.userFetch = null;
|
|
1583
|
+
});
|
|
1584
|
+
this.userFetch = { client, promise };
|
|
1585
|
+
return promise;
|
|
1521
1586
|
}
|
|
1522
1587
|
/**
|
|
1523
1588
|
* Create a RestApiClient with a custom TokenManager that proactively
|
|
@@ -1616,18 +1681,13 @@ var AuthManager = class {
|
|
|
1616
1681
|
if (!this.autoRefreshEnabled) return;
|
|
1617
1682
|
const expiresAt = this.resolveExpiry(tokens);
|
|
1618
1683
|
if (!expiresAt) return;
|
|
1619
|
-
const
|
|
1620
|
-
const
|
|
1621
|
-
const
|
|
1622
|
-
|
|
1623
|
-
this.handleTokenRefresh().catch((err) => {
|
|
1624
|
-
this.error = err instanceof Error ? err : new Error(String(err));
|
|
1625
|
-
this.status = "unauthenticated";
|
|
1626
|
-
this.notify();
|
|
1627
|
-
});
|
|
1628
|
-
return;
|
|
1629
|
-
}
|
|
1684
|
+
const now = Date.now();
|
|
1685
|
+
const dueInMs = expiresAt * 1e3 - TOKEN_EXPIRY_BUFFER_SECONDS * 1e3 - now;
|
|
1686
|
+
const throttleMs = this.lastScheduledRefreshAt + MIN_TOKEN_REFRESH_INTERVAL_MS - now;
|
|
1687
|
+
const refreshInMs = Math.max(dueInMs, throttleMs, 0);
|
|
1630
1688
|
this.tokenRefreshTimer = setTimeout(() => {
|
|
1689
|
+
this.tokenRefreshTimer = null;
|
|
1690
|
+
this.lastScheduledRefreshAt = Date.now();
|
|
1631
1691
|
this.handleTokenRefresh().catch((err) => {
|
|
1632
1692
|
this.error = err instanceof Error ? err : new Error(String(err));
|
|
1633
1693
|
this.status = "unauthenticated";
|