@fluid-app/fluid-cli 0.1.797 → 0.1.798
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/bin/fluid.mjs +3 -3
- package/dist/client-8ZkZdMPP.mjs +2 -0
- package/dist/{hook-DEOTaLvy.mjs → hook-O0j_i9gX.mjs} +3 -3
- package/dist/{hook-DEOTaLvy.mjs.map → hook-O0j_i9gX.mjs.map} +1 -1
- package/dist/index.mjs +2 -2
- package/dist/{profiles-C3Mu_PMx.mjs → profiles-Sitg25-8.mjs} +490 -99
- package/dist/{profiles-C3Mu_PMx.mjs.map → profiles-Sitg25-8.mjs.map} +1 -1
- package/package.json +1 -1
- package/dist/client-Dgx4oZhb.mjs +0 -2
|
@@ -1450,20 +1450,6 @@ const AUTH_CONSTANTS = {
|
|
|
1450
1450
|
* blocks third-party cookies, the identity service refuses the grant and the
|
|
1451
1451
|
* session falls back to the silent `prompt=none` redirect.
|
|
1452
1452
|
*/
|
|
1453
|
-
function createRefresher(run) {
|
|
1454
|
-
let inflight = null;
|
|
1455
|
-
return {
|
|
1456
|
-
refresh() {
|
|
1457
|
-
inflight ??= run().finally(() => {
|
|
1458
|
-
inflight = null;
|
|
1459
|
-
});
|
|
1460
|
-
return inflight;
|
|
1461
|
-
},
|
|
1462
|
-
inFlight() {
|
|
1463
|
-
return inflight !== null;
|
|
1464
|
-
}
|
|
1465
|
-
};
|
|
1466
|
-
}
|
|
1467
1453
|
function classifyRefreshFailure(error) {
|
|
1468
1454
|
const code = oauthErrorCode(error);
|
|
1469
1455
|
if (code === "temporarily_unavailable" || code === "slow_down" || code === "server_error") return "unavailable";
|
|
@@ -1479,7 +1465,8 @@ const REFUSAL_REASONS = new Set([
|
|
|
1479
1465
|
"not_mirrored",
|
|
1480
1466
|
"no_membership",
|
|
1481
1467
|
"bound",
|
|
1482
|
-
"flushed"
|
|
1468
|
+
"flushed",
|
|
1469
|
+
"chain_company_refused"
|
|
1483
1470
|
]);
|
|
1484
1471
|
function isRefusalReason(value) {
|
|
1485
1472
|
return typeof value === "string" && REFUSAL_REASONS.has(value);
|
|
@@ -1539,6 +1526,12 @@ function refusalReason(error) {
|
|
|
1539
1526
|
const reason = error.body?.reason;
|
|
1540
1527
|
return isRefusalReason(reason) ? reason : null;
|
|
1541
1528
|
}
|
|
1529
|
+
/** The company a `chain_company_refused` answer names; null when absent or empty. */
|
|
1530
|
+
function homeOrg(error) {
|
|
1531
|
+
if (!isApiError(error)) return null;
|
|
1532
|
+
const home = error.body?.home_org;
|
|
1533
|
+
return typeof home === "string" && home !== "" ? home : null;
|
|
1534
|
+
}
|
|
1542
1535
|
/** The refusal an identity answer carried, null when it carried no OAuth error. */
|
|
1543
1536
|
function moveRefusal(error) {
|
|
1544
1537
|
const code = oauthErrorCode(error);
|
|
@@ -1683,7 +1676,76 @@ async function createCodeChallenge(verifier, cryptoImpl = crypto) {
|
|
|
1683
1676
|
* before it revokes regardless.
|
|
1684
1677
|
*/
|
|
1685
1678
|
const SIGN_OUT_WAIT_MS = 5e3;
|
|
1679
|
+
/**
|
|
1680
|
+
* The longest a sign-in waits for an earlier redemption's `/token` to answer
|
|
1681
|
+
* before it sends its own. The authorization code lives 60 s.
|
|
1682
|
+
*/
|
|
1683
|
+
const REDEMPTION_WAIT_MS = 2e4;
|
|
1684
|
+
/**
|
|
1685
|
+
* The longest a code redemption may stay unanswered before the client
|
|
1686
|
+
* abandons it: the code's 60 s life, the server's 10 s mint deadline and
|
|
1687
|
+
* 20 s of database slack.
|
|
1688
|
+
*/
|
|
1689
|
+
const REDEMPTION_DEADLINE_MS = 9e4;
|
|
1686
1690
|
const LOGIN_HINT_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
1691
|
+
/**
|
|
1692
|
+
* Where the refresh chain's company stands. Only a named refresh's 200 sets
|
|
1693
|
+
* `doubt` or `moved`, and `homeTarget()` and `known()` read home only through
|
|
1694
|
+
* them.
|
|
1695
|
+
*/
|
|
1696
|
+
function createChainTracker() {
|
|
1697
|
+
let org = null;
|
|
1698
|
+
let doubt = false;
|
|
1699
|
+
let learned = false;
|
|
1700
|
+
let home;
|
|
1701
|
+
let moved = false;
|
|
1702
|
+
function clear() {
|
|
1703
|
+
doubt = false;
|
|
1704
|
+
learned = false;
|
|
1705
|
+
home = void 0;
|
|
1706
|
+
moved = false;
|
|
1707
|
+
}
|
|
1708
|
+
return {
|
|
1709
|
+
get org() {
|
|
1710
|
+
return org;
|
|
1711
|
+
},
|
|
1712
|
+
get doubt() {
|
|
1713
|
+
return doubt;
|
|
1714
|
+
},
|
|
1715
|
+
confirmed(chainOrg) {
|
|
1716
|
+
org = chainOrg;
|
|
1717
|
+
doubt = false;
|
|
1718
|
+
learned = true;
|
|
1719
|
+
if (home === void 0 && !moved) home = chainOrg;
|
|
1720
|
+
},
|
|
1721
|
+
namedMoved(named) {
|
|
1722
|
+
if (doubt || !sameOrg(named, org)) {
|
|
1723
|
+
doubt = true;
|
|
1724
|
+
moved = true;
|
|
1725
|
+
}
|
|
1726
|
+
},
|
|
1727
|
+
homeRetried(homeCompany, renewed) {
|
|
1728
|
+
home = homeCompany;
|
|
1729
|
+
if (renewed) {
|
|
1730
|
+
doubt = true;
|
|
1731
|
+
moved = false;
|
|
1732
|
+
}
|
|
1733
|
+
},
|
|
1734
|
+
known() {
|
|
1735
|
+
return !doubt && learned && home !== void 0 && home !== null;
|
|
1736
|
+
},
|
|
1737
|
+
homeTarget() {
|
|
1738
|
+
return moved && home !== void 0 && home !== null ? home : null;
|
|
1739
|
+
},
|
|
1740
|
+
signingIn() {
|
|
1741
|
+
clear();
|
|
1742
|
+
},
|
|
1743
|
+
reset() {
|
|
1744
|
+
org = null;
|
|
1745
|
+
clear();
|
|
1746
|
+
}
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1687
1749
|
/** Company ids are uuids, which the identity service compares in any case. */
|
|
1688
1750
|
function sameOrg(a, b) {
|
|
1689
1751
|
if (a === null || a === void 0 || b === null || b === void 0) return (a ?? null) === (b ?? null);
|
|
@@ -1709,11 +1771,79 @@ function createNativeSession(config) {
|
|
|
1709
1771
|
if (await config.storage.get(storageKey) === value) await config.storage.delete(storageKey);
|
|
1710
1772
|
})
|
|
1711
1773
|
};
|
|
1774
|
+
const refreshOpKey = `${storageKey}.refresh-op`;
|
|
1775
|
+
let refreshOp = null;
|
|
1776
|
+
let refreshOpStored = false;
|
|
1777
|
+
function fingerprint(value) {
|
|
1778
|
+
let h1 = 3735928559;
|
|
1779
|
+
let h2 = 1103547991;
|
|
1780
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
1781
|
+
const code = value.charCodeAt(i);
|
|
1782
|
+
h1 = Math.imul(h1 ^ code, 2654435761);
|
|
1783
|
+
h2 = Math.imul(h2 ^ code, 1597334677);
|
|
1784
|
+
}
|
|
1785
|
+
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
|
|
1786
|
+
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
1787
|
+
return `${(h1 >>> 0).toString(16)}${(h2 >>> 0).toString(16)}`;
|
|
1788
|
+
}
|
|
1789
|
+
async function opIdFor(refreshToken) {
|
|
1790
|
+
const fp = fingerprint(refreshToken);
|
|
1791
|
+
if (refreshOp?.fp !== fp) {
|
|
1792
|
+
refreshOp = null;
|
|
1793
|
+
try {
|
|
1794
|
+
const saved = JSON.parse(await serial(() => config.storage.get(refreshOpKey)) ?? "null");
|
|
1795
|
+
if (saved?.fp === fp && typeof saved.op === "string") {
|
|
1796
|
+
refreshOp = saved;
|
|
1797
|
+
refreshOpStored = true;
|
|
1798
|
+
}
|
|
1799
|
+
} catch {
|
|
1800
|
+
refreshOp = null;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
if (refreshOp?.fp !== fp) {
|
|
1804
|
+
const next = {
|
|
1805
|
+
fp,
|
|
1806
|
+
op: createState(config.crypto)
|
|
1807
|
+
};
|
|
1808
|
+
try {
|
|
1809
|
+
await serial(() => config.storage.set(refreshOpKey, JSON.stringify(next)));
|
|
1810
|
+
refreshOp = next;
|
|
1811
|
+
refreshOpStored = true;
|
|
1812
|
+
} catch {
|
|
1813
|
+
refreshOp = null;
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
return refreshOp?.op;
|
|
1817
|
+
}
|
|
1818
|
+
async function renewOp(refreshToken) {
|
|
1819
|
+
const next = {
|
|
1820
|
+
fp: fingerprint(refreshToken),
|
|
1821
|
+
op: createState(config.crypto)
|
|
1822
|
+
};
|
|
1823
|
+
refreshOp = null;
|
|
1824
|
+
try {
|
|
1825
|
+
await serial(() => config.storage.set(refreshOpKey, JSON.stringify(next)));
|
|
1826
|
+
refreshOp = next;
|
|
1827
|
+
refreshOpStored = true;
|
|
1828
|
+
} catch {
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
return next.op;
|
|
1832
|
+
}
|
|
1833
|
+
async function clearRefreshOp() {
|
|
1834
|
+
refreshOp = null;
|
|
1835
|
+
if (!refreshOpStored) return;
|
|
1836
|
+
refreshOpStored = false;
|
|
1837
|
+
try {
|
|
1838
|
+
await serial(() => config.storage.delete(refreshOpKey));
|
|
1839
|
+
} catch {}
|
|
1840
|
+
}
|
|
1712
1841
|
const grace = AUTH_CONSTANTS.TOKEN_GRACE_PERIOD_MS;
|
|
1713
1842
|
let held = null;
|
|
1714
1843
|
let chain = null;
|
|
1715
|
-
|
|
1844
|
+
const tracker = createChainTracker();
|
|
1716
1845
|
let target = null;
|
|
1846
|
+
let switching = 0;
|
|
1717
1847
|
let pending = null;
|
|
1718
1848
|
let snapshot = {
|
|
1719
1849
|
status: "idle",
|
|
@@ -1722,6 +1852,15 @@ function createNativeSession(config) {
|
|
|
1722
1852
|
};
|
|
1723
1853
|
const listeners = /* @__PURE__ */ new Set();
|
|
1724
1854
|
let generation = 0;
|
|
1855
|
+
let redeemed = 0;
|
|
1856
|
+
let sendSeq = 0;
|
|
1857
|
+
let redeemedSeq = 0;
|
|
1858
|
+
const redemptions = /* @__PURE__ */ new Set();
|
|
1859
|
+
let latestRedemption = null;
|
|
1860
|
+
let announceLatest = () => {};
|
|
1861
|
+
let latestChanged = new Promise((resolve) => {
|
|
1862
|
+
announceLatest = resolve;
|
|
1863
|
+
});
|
|
1725
1864
|
let signOutsRunning = 0;
|
|
1726
1865
|
let pickedOrg = null;
|
|
1727
1866
|
const client = createIdentityHeyApiClient({
|
|
@@ -1730,6 +1869,21 @@ function createNativeSession(config) {
|
|
|
1730
1869
|
fetch: config.fetch,
|
|
1731
1870
|
getAccessToken: () => held?.accessToken
|
|
1732
1871
|
});
|
|
1872
|
+
function capture() {
|
|
1873
|
+
return {
|
|
1874
|
+
out: generation,
|
|
1875
|
+
red: redeemed
|
|
1876
|
+
};
|
|
1877
|
+
}
|
|
1878
|
+
function isCurrent(cap) {
|
|
1879
|
+
return cap.out === generation && cap.red === redeemed;
|
|
1880
|
+
}
|
|
1881
|
+
/** Runs `write` only while `cap` is still current; true when it ran. */
|
|
1882
|
+
function commit(cap, write) {
|
|
1883
|
+
if (!isCurrent(cap)) return false;
|
|
1884
|
+
write();
|
|
1885
|
+
return true;
|
|
1886
|
+
}
|
|
1733
1887
|
function update(patch) {
|
|
1734
1888
|
snapshot = {
|
|
1735
1889
|
...snapshot,
|
|
@@ -1742,6 +1896,20 @@ function createNativeSession(config) {
|
|
|
1742
1896
|
return held && sameOrg(held.target, target) && now() < held.expiresAt - grace ? held : null;
|
|
1743
1897
|
}
|
|
1744
1898
|
/**
|
|
1899
|
+
* Ends stale work. When a newer sign-in redeemed, its session may share this
|
|
1900
|
+
* one's SID, so only the local copy goes and nothing is revoked; when only a
|
|
1901
|
+
* sign-out moved, the token is revoked as the sign-out meant.
|
|
1902
|
+
*/
|
|
1903
|
+
async function dropStale(cap, refreshToken, stored) {
|
|
1904
|
+
if (refreshToken) {
|
|
1905
|
+
if (stored) try {
|
|
1906
|
+
await store.deleteIf(refreshToken);
|
|
1907
|
+
} catch {}
|
|
1908
|
+
if (cap.red === redeemed) await revokeQuietly(refreshToken);
|
|
1909
|
+
}
|
|
1910
|
+
throw new SessionError("not_signed_in");
|
|
1911
|
+
}
|
|
1912
|
+
/**
|
|
1745
1913
|
* Keeps a newly issued token pair: the refresh token goes to secure storage
|
|
1746
1914
|
* before the access token is used, so a crash cannot lose a rotation the
|
|
1747
1915
|
* server has already made. A response without a refresh token (the reuse
|
|
@@ -1749,19 +1917,11 @@ function createNativeSession(config) {
|
|
|
1749
1917
|
* session holds another company by exchange, the new chain token is
|
|
1750
1918
|
* exchanged into it before anything is handed out.
|
|
1751
1919
|
*/
|
|
1752
|
-
async function adopt(token,
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
if (token.refresh_token) await store.set(token.refresh_token);
|
|
1758
|
-
if (started !== generation) {
|
|
1759
|
-
if (token.refresh_token) {
|
|
1760
|
-
await store.deleteIf(token.refresh_token);
|
|
1761
|
-
await revokeQuietly(token.refresh_token);
|
|
1762
|
-
}
|
|
1763
|
-
throw new SessionError("not_signed_in");
|
|
1764
|
-
}
|
|
1920
|
+
async function adopt(token, cap) {
|
|
1921
|
+
const refreshToken = token.refresh_token;
|
|
1922
|
+
if (!isCurrent(cap)) return dropStale(cap, refreshToken, false);
|
|
1923
|
+
if (refreshToken) await store.set(refreshToken);
|
|
1924
|
+
if (!isCurrent(cap)) return dropStale(cap, refreshToken, true);
|
|
1765
1925
|
const issued = {
|
|
1766
1926
|
accessToken: token.access_token,
|
|
1767
1927
|
expiresAt: now() + token.expires_in * 1e3,
|
|
@@ -1769,17 +1929,27 @@ function createNativeSession(config) {
|
|
|
1769
1929
|
};
|
|
1770
1930
|
chain = issued;
|
|
1771
1931
|
if (target === null) held = issued;
|
|
1772
|
-
const
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1932
|
+
const rollback = () => {
|
|
1933
|
+
if (chain === issued) chain = null;
|
|
1934
|
+
if (held === issued) held = null;
|
|
1935
|
+
};
|
|
1936
|
+
let info;
|
|
1937
|
+
try {
|
|
1938
|
+
info = await getUserinfo({
|
|
1939
|
+
client,
|
|
1940
|
+
auth: issued.accessToken
|
|
1941
|
+
});
|
|
1942
|
+
} catch (error) {
|
|
1943
|
+
if (isCurrent(cap)) throw error;
|
|
1944
|
+
rollback();
|
|
1945
|
+
return dropStale(cap, cap.red === redeemed ? void 0 : refreshToken, true);
|
|
1780
1946
|
}
|
|
1781
|
-
|
|
1782
|
-
|
|
1947
|
+
if (!isCurrent(cap)) {
|
|
1948
|
+
rollback();
|
|
1949
|
+
return dropStale(cap, cap.red === redeemed ? void 0 : refreshToken, true);
|
|
1950
|
+
}
|
|
1951
|
+
tracker.confirmed(info.org ?? null);
|
|
1952
|
+
if (target !== null && !sameOrg(target, tracker.org)) return enter(target, cap);
|
|
1783
1953
|
held = {
|
|
1784
1954
|
...issued,
|
|
1785
1955
|
target
|
|
@@ -1797,7 +1967,7 @@ function createNativeSession(config) {
|
|
|
1797
1967
|
* rejects with `access_denied` and hands nothing out, so work started for
|
|
1798
1968
|
* `org` can never be given the chain's own company's token instead.
|
|
1799
1969
|
*/
|
|
1800
|
-
async function enter(org,
|
|
1970
|
+
async function enter(org, cap) {
|
|
1801
1971
|
const from = chain;
|
|
1802
1972
|
if (!from) throw new SessionError("not_signed_in");
|
|
1803
1973
|
let exchanged;
|
|
@@ -1812,12 +1982,12 @@ function createNativeSession(config) {
|
|
|
1812
1982
|
roles: null
|
|
1813
1983
|
});
|
|
1814
1984
|
} catch (error) {
|
|
1815
|
-
if (
|
|
1985
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
1816
1986
|
const sessionError = error instanceof SessionError && error.code === "access_denied" ? new SessionError("access_denied", `The identity service will not move this session to company ${org}.`, error.refusal) : new SessionError("temporarily_unavailable", error instanceof SessionError && error.code === "session_expired" ? "The identity service did not accept the session's token for an exchange; a refresh may." : "The identity service could not exchange the session's token; retry later.");
|
|
1817
1987
|
update({ error: sessionError });
|
|
1818
1988
|
throw sessionError;
|
|
1819
1989
|
}
|
|
1820
|
-
if (
|
|
1990
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
1821
1991
|
const expiresAt = Math.min(from.expiresAt, exchanged.expiresAt);
|
|
1822
1992
|
held = {
|
|
1823
1993
|
accessToken: exchanged.accessToken,
|
|
@@ -1861,15 +2031,17 @@ function createNativeSession(config) {
|
|
|
1861
2031
|
*
|
|
1862
2032
|
* Resolves with the error `completeSignIn()` rejects with.
|
|
1863
2033
|
*/
|
|
1864
|
-
async function settleUnfinishedSignIn(refreshToken, error) {
|
|
1865
|
-
|
|
2034
|
+
async function settleUnfinishedSignIn(refreshToken, cap, error) {
|
|
2035
|
+
commit(cap, () => {
|
|
2036
|
+
held = null;
|
|
2037
|
+
});
|
|
1866
2038
|
let kept = false;
|
|
1867
2039
|
if (classifyRefreshFailure(error) === "unavailable") try {
|
|
1868
2040
|
kept = await store.get() === refreshToken;
|
|
1869
2041
|
} catch {
|
|
1870
2042
|
kept = false;
|
|
1871
2043
|
}
|
|
1872
|
-
if (kept) {
|
|
2044
|
+
if (kept && isCurrent(cap)) {
|
|
1873
2045
|
const unavailable = new SessionError("temporarily_unavailable", "Signed in, but the identity service could not confirm the session yet; restore() finishes it.");
|
|
1874
2046
|
update({ error: unavailable });
|
|
1875
2047
|
return unavailable;
|
|
@@ -1877,11 +2049,13 @@ function createNativeSession(config) {
|
|
|
1877
2049
|
try {
|
|
1878
2050
|
await store.deleteIf(refreshToken);
|
|
1879
2051
|
} catch {}
|
|
1880
|
-
await revokeQuietly(refreshToken);
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
2052
|
+
if (cap.red === redeemed) await revokeQuietly(refreshToken);
|
|
2053
|
+
commit(cap, () => {
|
|
2054
|
+
update({
|
|
2055
|
+
status: "unauthenticated",
|
|
2056
|
+
facts: null,
|
|
2057
|
+
error: null
|
|
2058
|
+
});
|
|
1885
2059
|
});
|
|
1886
2060
|
return new SessionError("redemption_failed", "The sign-in could not be completed, so the session it opened was ended.");
|
|
1887
2061
|
}
|
|
@@ -1905,15 +2079,20 @@ function createNativeSession(config) {
|
|
|
1905
2079
|
await Promise.race([Promise.allSettled([...work]), bound]);
|
|
1906
2080
|
clearTimeout(timer);
|
|
1907
2081
|
}
|
|
1908
|
-
async function forget(error) {
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
2082
|
+
async function forget(cap, error, refreshToken) {
|
|
2083
|
+
if (!commit(cap, () => {
|
|
2084
|
+
held = null;
|
|
2085
|
+
chain = null;
|
|
2086
|
+
tracker.reset();
|
|
2087
|
+
})) return;
|
|
2088
|
+
await (refreshToken ? store.deleteIf(refreshToken) : store.delete());
|
|
2089
|
+
await clearRefreshOp();
|
|
2090
|
+
commit(cap, () => {
|
|
2091
|
+
update({
|
|
2092
|
+
status: "unauthenticated",
|
|
2093
|
+
facts: null,
|
|
2094
|
+
error
|
|
2095
|
+
});
|
|
1917
2096
|
});
|
|
1918
2097
|
}
|
|
1919
2098
|
/**
|
|
@@ -1921,46 +2100,112 @@ function createNativeSession(config) {
|
|
|
1921
2100
|
* service has ended that chain, so it can never succeed again. An outage
|
|
1922
2101
|
* keeps it, since nothing was consumed.
|
|
1923
2102
|
*/
|
|
1924
|
-
const
|
|
2103
|
+
const refreshes = /* @__PURE__ */ new Map();
|
|
2104
|
+
function refreshKey() {
|
|
2105
|
+
return `${generation}:${redeemed}`;
|
|
2106
|
+
}
|
|
2107
|
+
/** Joins the refresh for the current session, or starts one. */
|
|
2108
|
+
function refreshCurrent() {
|
|
2109
|
+
const key = refreshKey();
|
|
2110
|
+
let refreshing = refreshes.get(key);
|
|
2111
|
+
if (!refreshing) {
|
|
2112
|
+
refreshing = tracked(runRefresh());
|
|
2113
|
+
refreshes.set(key, refreshing);
|
|
2114
|
+
const done = () => {
|
|
2115
|
+
if (refreshes.get(key) === refreshing) refreshes.delete(key);
|
|
2116
|
+
};
|
|
2117
|
+
refreshing.then(done, done);
|
|
2118
|
+
}
|
|
2119
|
+
return refreshing;
|
|
2120
|
+
}
|
|
1925
2121
|
async function runRefresh() {
|
|
1926
2122
|
if (signOutsRunning > 0) throw new SessionError("not_signed_in");
|
|
1927
|
-
const
|
|
2123
|
+
const cap = capture();
|
|
1928
2124
|
const refreshToken = await store.get();
|
|
2125
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
1929
2126
|
if (!refreshToken) {
|
|
1930
|
-
await forget(null);
|
|
2127
|
+
await forget(cap, null);
|
|
1931
2128
|
throw new SessionError("not_signed_in");
|
|
1932
2129
|
}
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
2130
|
+
const renew = Boolean(config.renewWithOrg?.());
|
|
2131
|
+
const named = renew && tracker.known() ? target : null;
|
|
2132
|
+
let opId = await opIdFor(refreshToken);
|
|
2133
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2134
|
+
const refresh = async (org) => {
|
|
2135
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2136
|
+
return createToken({
|
|
1936
2137
|
client,
|
|
1937
2138
|
body: {
|
|
1938
2139
|
grant_type: "refresh_token",
|
|
1939
2140
|
client_id: config.clientId,
|
|
1940
|
-
refresh_token: refreshToken
|
|
2141
|
+
refresh_token: refreshToken,
|
|
2142
|
+
...opId ? { op_id: opId } : {},
|
|
2143
|
+
...org ? { org } : {}
|
|
1941
2144
|
}
|
|
1942
2145
|
});
|
|
2146
|
+
};
|
|
2147
|
+
let token;
|
|
2148
|
+
let namedRefused = false;
|
|
2149
|
+
let homeTried = false;
|
|
2150
|
+
let org = named;
|
|
2151
|
+
try {
|
|
2152
|
+
for (;;) try {
|
|
2153
|
+
token = await refresh(org);
|
|
2154
|
+
break;
|
|
2155
|
+
} catch (error) {
|
|
2156
|
+
if (!isCurrent(cap) || classifyRefreshFailure(error) === "unavailable") throw error;
|
|
2157
|
+
const home = refusalReason(error) === "chain_company_refused" ? homeOrg(error) : null;
|
|
2158
|
+
if (org !== null && !homeTried) {
|
|
2159
|
+
namedRefused = true;
|
|
2160
|
+
org = null;
|
|
2161
|
+
} else if (org === null && home !== null && !homeTried) {
|
|
2162
|
+
homeTried = true;
|
|
2163
|
+
org = home;
|
|
2164
|
+
} else throw error;
|
|
2165
|
+
opId = await renewOp(refreshToken);
|
|
2166
|
+
}
|
|
1943
2167
|
} catch (error) {
|
|
1944
|
-
if (
|
|
2168
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2169
|
+
if (oauthErrorCode(error) === "access_denied" && (refusalReason(error) === "chain_company_refused" || namedRefused && !homeTried)) {
|
|
2170
|
+
held = null;
|
|
2171
|
+
await renewOp(refreshToken);
|
|
2172
|
+
if (renew && switching === 0) target = tracker.homeTarget();
|
|
2173
|
+
const refused = new SessionError("access_denied", "The identity service will not issue a token for the company this session was moved to.", moveRefusal(error) ?? void 0);
|
|
2174
|
+
update({ error: refused });
|
|
2175
|
+
throw refused;
|
|
2176
|
+
}
|
|
1945
2177
|
if (classifyRefreshFailure(error) === "unavailable") {
|
|
1946
2178
|
const unavailable = new SessionError("temporarily_unavailable");
|
|
1947
2179
|
update({ error: unavailable });
|
|
1948
2180
|
throw unavailable;
|
|
1949
2181
|
}
|
|
1950
2182
|
const expired = new SessionError("session_expired", `The identity service refused the stored refresh token (${oauthErrorCode(error) ?? "rejected"}).`);
|
|
1951
|
-
await forget(expired);
|
|
2183
|
+
await forget(cap, expired, refreshToken);
|
|
1952
2184
|
throw expired;
|
|
1953
2185
|
}
|
|
2186
|
+
if (isCurrent(cap)) {
|
|
2187
|
+
if (homeTried && org !== null) tracker.homeRetried(org, renew);
|
|
2188
|
+
else if (org !== null) tracker.namedMoved(org);
|
|
2189
|
+
if (target === null) target = tracker.homeTarget();
|
|
2190
|
+
}
|
|
1954
2191
|
try {
|
|
1955
|
-
await adopt(token,
|
|
2192
|
+
await adopt(token, cap);
|
|
2193
|
+
await clearRefreshOp();
|
|
1956
2194
|
} catch (error) {
|
|
1957
|
-
if (
|
|
2195
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2196
|
+
if (error instanceof SessionError) {
|
|
2197
|
+
if (renew && switching === 0 && error.code === "access_denied") {
|
|
2198
|
+
held = null;
|
|
2199
|
+
target = tracker.homeTarget();
|
|
2200
|
+
}
|
|
2201
|
+
throw error;
|
|
2202
|
+
}
|
|
1958
2203
|
if (classifyRefreshFailure(error) === "unavailable") {
|
|
1959
2204
|
held = null;
|
|
1960
2205
|
throw new SessionError("temporarily_unavailable");
|
|
1961
2206
|
}
|
|
1962
2207
|
const expired = new SessionError("session_expired", "The identity service rejected the token it issued.");
|
|
1963
|
-
await forget(expired);
|
|
2208
|
+
await forget(cap, expired);
|
|
1964
2209
|
throw expired;
|
|
1965
2210
|
}
|
|
1966
2211
|
if (!held) throw new SessionError("not_signed_in");
|
|
@@ -1972,16 +2217,58 @@ function createNativeSession(config) {
|
|
|
1972
2217
|
* session has since moved from.
|
|
1973
2218
|
*/
|
|
1974
2219
|
async function freshToken() {
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
2220
|
+
let mismatches = 0;
|
|
2221
|
+
let retriedStale = false;
|
|
2222
|
+
while (mismatches < 2) {
|
|
2223
|
+
const key = refreshKey();
|
|
2224
|
+
const startRed = redeemed;
|
|
2225
|
+
let refreshed;
|
|
2226
|
+
let failure;
|
|
2227
|
+
try {
|
|
2228
|
+
refreshed = await refreshCurrent();
|
|
2229
|
+
} catch (error) {
|
|
2230
|
+
failure = error;
|
|
2231
|
+
}
|
|
2232
|
+
if (key !== refreshKey()) {
|
|
2233
|
+
let timer;
|
|
2234
|
+
const bound = new Promise((resolve) => {
|
|
2235
|
+
timer = setTimeout(() => resolve("unconfirmed"), config.signOutWaitMs ?? SIGN_OUT_WAIT_MS);
|
|
2236
|
+
});
|
|
2237
|
+
let failed = null;
|
|
2238
|
+
try {
|
|
2239
|
+
for (;;) {
|
|
2240
|
+
const moving = latestRedemption;
|
|
2241
|
+
if (!moving || moving === failed || moving.red !== redeemed || failed === null && redeemed === startRed) break;
|
|
2242
|
+
const outcome = await Promise.race([
|
|
2243
|
+
moving.confirmation,
|
|
2244
|
+
bound,
|
|
2245
|
+
latestChanged.then(() => "changed")
|
|
2246
|
+
]);
|
|
2247
|
+
if (outcome === "changed") continue;
|
|
2248
|
+
if (outcome === "unconfirmed") throw new SessionError("temporarily_unavailable", "The new sign-in has not been confirmed yet; retry.");
|
|
2249
|
+
const landed = outcome && current();
|
|
2250
|
+
if (landed) return landed;
|
|
2251
|
+
failed = moving;
|
|
2252
|
+
}
|
|
2253
|
+
} finally {
|
|
2254
|
+
clearTimeout(timer);
|
|
2255
|
+
}
|
|
2256
|
+
if (!retriedStale) {
|
|
2257
|
+
retriedStale = true;
|
|
2258
|
+
continue;
|
|
2259
|
+
}
|
|
2260
|
+
throw failure ?? new SessionError("not_signed_in");
|
|
2261
|
+
}
|
|
2262
|
+
if (failure) throw failure;
|
|
2263
|
+
if (refreshed && sameOrg(refreshed.target, target)) return refreshed;
|
|
2264
|
+
mismatches += 1;
|
|
1978
2265
|
}
|
|
1979
2266
|
throw new SessionError("temporarily_unavailable", "The session moved company while a token was being issued; retry.");
|
|
1980
2267
|
}
|
|
1981
2268
|
const session = {
|
|
1982
2269
|
async restore(options) {
|
|
1983
2270
|
if (options && "org" in options) {
|
|
1984
|
-
const org = options.org ??
|
|
2271
|
+
const org = options.org ?? tracker.homeTarget();
|
|
1985
2272
|
if (!sameOrg(org, target)) {
|
|
1986
2273
|
target = org;
|
|
1987
2274
|
held = null;
|
|
@@ -2025,7 +2312,6 @@ function createNativeSession(config) {
|
|
|
2025
2312
|
const request = pending;
|
|
2026
2313
|
pending = null;
|
|
2027
2314
|
if (signOutsRunning > 0) throw new SessionError("not_signed_in");
|
|
2028
|
-
const started = generation;
|
|
2029
2315
|
let callback;
|
|
2030
2316
|
try {
|
|
2031
2317
|
callback = new URL(callbackUrl);
|
|
@@ -2038,10 +2324,41 @@ function createNativeSession(config) {
|
|
|
2038
2324
|
if (error) throw new SessionError(error === "access_denied" || error === "login_required" || error === "account_selection_required" || error === "temporarily_unavailable" ? error : "redemption_failed", `The identity service answered ${error}.`);
|
|
2039
2325
|
const code = callback.searchParams.get("code");
|
|
2040
2326
|
if (!code) throw new SessionError("redemption_failed", "The redirect had no code.");
|
|
2327
|
+
let answered = () => {};
|
|
2328
|
+
const redemption = new Promise((resolve) => {
|
|
2329
|
+
answered = resolve;
|
|
2330
|
+
});
|
|
2331
|
+
const started = generation;
|
|
2332
|
+
const earlier = [...redemptions];
|
|
2333
|
+
redemptions.add(redemption);
|
|
2334
|
+
let sent;
|
|
2335
|
+
let sequence;
|
|
2041
2336
|
let token;
|
|
2042
2337
|
try {
|
|
2043
|
-
|
|
2338
|
+
if (earlier.length > 0) {
|
|
2339
|
+
let timer;
|
|
2340
|
+
const bound = new Promise((resolve) => {
|
|
2341
|
+
timer = setTimeout(() => resolve("expired"), REDEMPTION_WAIT_MS);
|
|
2342
|
+
});
|
|
2343
|
+
const waited = await Promise.race([Promise.all(earlier), bound]);
|
|
2344
|
+
clearTimeout(timer);
|
|
2345
|
+
if (waited === "expired" && earlier.some((e) => redemptions.has(e))) throw new SessionError("temporarily_unavailable", "An earlier sign-in is still being redeemed; retry.");
|
|
2346
|
+
}
|
|
2347
|
+
if (started !== generation || signOutsRunning > 0) throw new SessionError("not_signed_in");
|
|
2348
|
+
sent = capture();
|
|
2349
|
+
sequence = sendSeq += 1;
|
|
2350
|
+
const abort = new AbortController();
|
|
2351
|
+
const abandoned = new SessionError("temporarily_unavailable", "The identity service did not answer the sign-in in time.");
|
|
2352
|
+
let deadline;
|
|
2353
|
+
const lapsed = new Promise((_, reject) => {
|
|
2354
|
+
deadline = setTimeout(() => {
|
|
2355
|
+
abort.abort();
|
|
2356
|
+
reject(abandoned);
|
|
2357
|
+
}, REDEMPTION_DEADLINE_MS);
|
|
2358
|
+
});
|
|
2359
|
+
const sending = createToken({
|
|
2044
2360
|
client,
|
|
2361
|
+
signal: abort.signal,
|
|
2045
2362
|
body: {
|
|
2046
2363
|
grant_type: "authorization_code",
|
|
2047
2364
|
code,
|
|
@@ -2050,19 +2367,59 @@ function createNativeSession(config) {
|
|
|
2050
2367
|
code_verifier: request.verifier
|
|
2051
2368
|
}
|
|
2052
2369
|
});
|
|
2053
|
-
|
|
2054
|
-
|
|
2370
|
+
sending.catch(() => void 0);
|
|
2371
|
+
try {
|
|
2372
|
+
token = await Promise.race([sending, lapsed]);
|
|
2373
|
+
} catch (redeemError) {
|
|
2374
|
+
if (redeemError === abandoned) throw abandoned;
|
|
2375
|
+
throw classifyRefreshFailure(redeemError) === "unavailable" ? new SessionError("temporarily_unavailable") : new SessionError("redemption_failed", "The code was not redeemed.");
|
|
2376
|
+
} finally {
|
|
2377
|
+
clearTimeout(deadline);
|
|
2378
|
+
}
|
|
2379
|
+
} finally {
|
|
2380
|
+
redemptions.delete(redemption);
|
|
2381
|
+
answered();
|
|
2055
2382
|
}
|
|
2056
|
-
|
|
2383
|
+
const refreshToken = token.refresh_token;
|
|
2384
|
+
if (!refreshToken) throw new SessionError("redemption_failed", "The identity service issued no refresh token to a native client.");
|
|
2385
|
+
if (redeemedSeq > sequence) throw new SessionError("not_signed_in");
|
|
2386
|
+
if (sent.out !== generation) {
|
|
2387
|
+
if (sent.red === redeemed) await revokeQuietly(refreshToken);
|
|
2388
|
+
throw new SessionError("not_signed_in");
|
|
2389
|
+
}
|
|
2390
|
+
redeemed += 1;
|
|
2391
|
+
redeemedSeq = sequence;
|
|
2392
|
+
const cap = capture();
|
|
2393
|
+
tracker.signingIn();
|
|
2057
2394
|
target = null;
|
|
2395
|
+
pickedOrg = null;
|
|
2058
2396
|
held = null;
|
|
2397
|
+
chain = null;
|
|
2398
|
+
let confirmed = () => {};
|
|
2399
|
+
latestRedemption = {
|
|
2400
|
+
red: redeemed,
|
|
2401
|
+
confirmation: new Promise((resolve) => {
|
|
2402
|
+
confirmed = resolve;
|
|
2403
|
+
})
|
|
2404
|
+
};
|
|
2405
|
+
const announced = announceLatest;
|
|
2406
|
+
latestChanged = new Promise((resolve) => {
|
|
2407
|
+
announceLatest = resolve;
|
|
2408
|
+
});
|
|
2409
|
+
announced();
|
|
2410
|
+
let landed = false;
|
|
2059
2411
|
try {
|
|
2060
|
-
const facts = await adopt(token,
|
|
2061
|
-
|
|
2412
|
+
const facts = await adopt(token, cap);
|
|
2413
|
+
commit(cap, () => {
|
|
2414
|
+
pickedOrg = token.picked_org ?? null;
|
|
2415
|
+
});
|
|
2416
|
+
landed = isCurrent(cap) && current() !== null;
|
|
2062
2417
|
return facts;
|
|
2063
2418
|
} catch (adoptError) {
|
|
2064
2419
|
if (adoptError instanceof SessionError) throw adoptError;
|
|
2065
|
-
throw await settleUnfinishedSignIn(
|
|
2420
|
+
throw await settleUnfinishedSignIn(refreshToken, cap, adoptError);
|
|
2421
|
+
} finally {
|
|
2422
|
+
confirmed(landed);
|
|
2066
2423
|
}
|
|
2067
2424
|
},
|
|
2068
2425
|
async getAccessToken(options) {
|
|
@@ -2070,14 +2427,16 @@ function createNativeSession(config) {
|
|
|
2070
2427
|
if (fresh && fresh.accessToken !== options?.replacing) return fresh.accessToken;
|
|
2071
2428
|
return (await freshToken()).accessToken;
|
|
2072
2429
|
},
|
|
2073
|
-
async switchCompany(
|
|
2430
|
+
async switchCompany(requested) {
|
|
2074
2431
|
if (signOutsRunning > 0) throw new SessionError("not_signed_in");
|
|
2075
|
-
while (
|
|
2076
|
-
const
|
|
2432
|
+
while (refreshes.has(refreshKey())) await refreshCurrent().catch(() => void 0);
|
|
2433
|
+
const cap = capture();
|
|
2077
2434
|
const previous = target;
|
|
2078
2435
|
const previousHeld = held;
|
|
2079
|
-
|
|
2080
|
-
|
|
2436
|
+
const destination = requested === null ? tracker.homeTarget() : requested;
|
|
2437
|
+
switching += 1;
|
|
2438
|
+
target = destination;
|
|
2439
|
+
if (!sameOrg(previous, destination)) held = null;
|
|
2081
2440
|
try {
|
|
2082
2441
|
const base = chain && now() < chain.expiresAt - grace ? chain : null;
|
|
2083
2442
|
if (!base) {
|
|
@@ -2085,15 +2444,32 @@ function createNativeSession(config) {
|
|
|
2085
2444
|
if (!snapshot.facts) throw new SessionError("not_signed_in");
|
|
2086
2445
|
return snapshot.facts;
|
|
2087
2446
|
}
|
|
2088
|
-
|
|
2089
|
-
|
|
2447
|
+
let info = null;
|
|
2448
|
+
if (tracker.doubt) {
|
|
2449
|
+
try {
|
|
2450
|
+
info = await getUserinfo({
|
|
2451
|
+
client,
|
|
2452
|
+
auth: base.accessToken
|
|
2453
|
+
});
|
|
2454
|
+
} catch {
|
|
2455
|
+
const unavailable = new SessionError("temporarily_unavailable", "The identity service could not confirm the session's company; retry later.");
|
|
2456
|
+
commit(cap, () => {
|
|
2457
|
+
update({ error: unavailable });
|
|
2458
|
+
});
|
|
2459
|
+
throw unavailable;
|
|
2460
|
+
}
|
|
2461
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2462
|
+
tracker.confirmed(info.org ?? null);
|
|
2463
|
+
}
|
|
2464
|
+
if (destination === null || sameOrg(destination, tracker.org)) {
|
|
2465
|
+
info ??= await getUserinfo({
|
|
2090
2466
|
client,
|
|
2091
2467
|
auth: base.accessToken
|
|
2092
2468
|
});
|
|
2093
|
-
if (
|
|
2469
|
+
if (!isCurrent(cap)) throw new SessionError("not_signed_in");
|
|
2094
2470
|
held = {
|
|
2095
2471
|
...base,
|
|
2096
|
-
target:
|
|
2472
|
+
target: destination
|
|
2097
2473
|
};
|
|
2098
2474
|
const facts = toSessionFacts(info, base.expiresAt);
|
|
2099
2475
|
update({
|
|
@@ -2103,15 +2479,20 @@ function createNativeSession(config) {
|
|
|
2103
2479
|
});
|
|
2104
2480
|
return facts;
|
|
2105
2481
|
}
|
|
2106
|
-
return await tracked(enter(
|
|
2482
|
+
return await tracked(enter(destination, cap));
|
|
2107
2483
|
} catch (error) {
|
|
2108
|
-
if (sameOrg(target,
|
|
2109
|
-
target = previous;
|
|
2110
|
-
held =
|
|
2484
|
+
if (isCurrent(cap) && sameOrg(target, destination)) {
|
|
2485
|
+
target = previous ?? tracker.homeTarget();
|
|
2486
|
+
held = sameOrg(target, previous) || sameOrg(target, previousHeld?.target) ? previousHeld : null;
|
|
2111
2487
|
}
|
|
2112
2488
|
throw error;
|
|
2489
|
+
} finally {
|
|
2490
|
+
switching -= 1;
|
|
2113
2491
|
}
|
|
2114
2492
|
},
|
|
2493
|
+
homeOrg() {
|
|
2494
|
+
return tracker.homeTarget();
|
|
2495
|
+
},
|
|
2115
2496
|
facts() {
|
|
2116
2497
|
return snapshot.facts;
|
|
2117
2498
|
},
|
|
@@ -2126,15 +2507,23 @@ function createNativeSession(config) {
|
|
|
2126
2507
|
pending = null;
|
|
2127
2508
|
held = null;
|
|
2128
2509
|
chain = null;
|
|
2129
|
-
|
|
2510
|
+
tracker.reset();
|
|
2130
2511
|
target = null;
|
|
2131
2512
|
pickedOrg = null;
|
|
2132
2513
|
let refreshToken;
|
|
2514
|
+
let ended;
|
|
2133
2515
|
try {
|
|
2134
2516
|
await settleWork();
|
|
2135
2517
|
generation += 1;
|
|
2518
|
+
ended = capture();
|
|
2519
|
+
held = null;
|
|
2520
|
+
chain = null;
|
|
2521
|
+
tracker.reset();
|
|
2522
|
+
target = null;
|
|
2523
|
+
pickedOrg = null;
|
|
2136
2524
|
refreshToken = await store.get();
|
|
2137
2525
|
await store.delete();
|
|
2526
|
+
await clearRefreshOp();
|
|
2138
2527
|
} finally {
|
|
2139
2528
|
signOutsRunning -= 1;
|
|
2140
2529
|
}
|
|
@@ -2148,7 +2537,9 @@ function createNativeSession(config) {
|
|
|
2148
2537
|
await revoke(refreshToken);
|
|
2149
2538
|
} catch {
|
|
2150
2539
|
const error = new SessionError("sign_out_failed", "Signed out on this device, but the identity service did not confirm revoking the session.");
|
|
2151
|
-
|
|
2540
|
+
commit(ended, () => {
|
|
2541
|
+
update({ error });
|
|
2542
|
+
});
|
|
2152
2543
|
throw error;
|
|
2153
2544
|
}
|
|
2154
2545
|
},
|
|
@@ -3098,4 +3489,4 @@ async function endUnusedSessions(before, after) {
|
|
|
3098
3489
|
//#endregion
|
|
3099
3490
|
export { isClientProfile as A, readStoredConfig as C, getConfigDir as D, isStrictConfig as E, getConfigFilePath as O, readConfig as S, writeConfig as T, primeClientProfile as _, peekIdentityToken as a, ApiError as b, sessionFacts as c, OrphanedClientSecretError as d, ProfileExistsError as f, peekClientToken as g, invalidateClientToken as h, isRoleNotGrantedError as i, isIdentityProfile as j, createDefaultConfig as k, signInWithBrowser as l, deleteClientSecret as m, describeIdentityError as n, primeIdentityProfile as o, clientAccessToken as p, identityToken as r, profileAccessToken as s, endUnusedSessions as t, signOutIdentity as u, saveClientProfile as v, updateConfig as w, createFetchClient as x, resolveIdentityUrl as y };
|
|
3100
3491
|
|
|
3101
|
-
//# sourceMappingURL=profiles-
|
|
3492
|
+
//# sourceMappingURL=profiles-Sitg25-8.mjs.map
|