@mitralab.io/platform-sdk 1.7.0 → 1.8.0-beta.0
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/CHANGELOG.md +27 -0
- package/README.md +14 -4
- package/dist/index.cjs +270 -29
- package/dist/index.d.cts +130 -7
- package/dist/index.d.ts +130 -7
- package/dist/index.js +273 -32
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ function stripTrailingSlashes(value) {
|
|
|
9
9
|
import {
|
|
10
10
|
createPublicFunctionsModule,
|
|
11
11
|
encodePathSegment,
|
|
12
|
-
expectObject as
|
|
12
|
+
expectObject as expectObject4
|
|
13
13
|
} from "@mitralab.io/sdk-core";
|
|
14
14
|
|
|
15
15
|
// src/utils/http-client.ts
|
|
@@ -100,7 +100,7 @@ var HttpClient = class {
|
|
|
100
100
|
* ```
|
|
101
101
|
*/
|
|
102
102
|
async request(path, options = {}) {
|
|
103
|
-
const { method = "GET", body, headers = {}, params, isRetry } = options;
|
|
103
|
+
const { method = "GET", body, headers = {}, params, isRetry, keepalive } = options;
|
|
104
104
|
const url = buildRequestUrl(this.baseUrl, path, params);
|
|
105
105
|
if (!isRetry && this.tokenGetter() && this.beforeAuthenticatedRequest) {
|
|
106
106
|
await this.beforeAuthenticatedRequest();
|
|
@@ -118,7 +118,8 @@ var HttpClient = class {
|
|
|
118
118
|
method,
|
|
119
119
|
headers: requestHeaders,
|
|
120
120
|
body: body ? JSON.stringify(body) : void 0,
|
|
121
|
-
redirect: "manual"
|
|
121
|
+
redirect: "manual",
|
|
122
|
+
...keepalive ? { keepalive } : {}
|
|
122
123
|
});
|
|
123
124
|
if (response.redirected || response.type === "opaqueredirect") {
|
|
124
125
|
const error = new MitraApiError(
|
|
@@ -365,7 +366,7 @@ function adoptLegacySession(session) {
|
|
|
365
366
|
}
|
|
366
367
|
|
|
367
368
|
// src/modules/auth.ts
|
|
368
|
-
import { createAuthModule } from "@mitralab.io/sdk-core";
|
|
369
|
+
import { createAuthModule, expectObject as expectObject3 } from "@mitralab.io/sdk-core";
|
|
369
370
|
|
|
370
371
|
// src/modules/api-key-auth.ts
|
|
371
372
|
import { resolveApiKeyToken } from "@mitralab.io/sdk-core";
|
|
@@ -406,6 +407,9 @@ async function resolveApiKeySession(publicClient, appId, apiKey) {
|
|
|
406
407
|
// src/modules/auth-page-flow.ts
|
|
407
408
|
import { expectObject } from "@mitralab.io/sdk-core";
|
|
408
409
|
var RESULT_TYPE = "mitra-oauth-result";
|
|
410
|
+
var SESSION_REQUEST_TYPE = "mitra-auth-session-request";
|
|
411
|
+
var SESSION_TYPE = "mitra-auth-session";
|
|
412
|
+
var PASSWORD_CHANGED_SIGN_IN_AGAIN = "PASSWORD_CHANGED_SIGN_IN_AGAIN";
|
|
409
413
|
var FIVE_MINUTES_MS = 5 * 60 * 1e3;
|
|
410
414
|
var TEN_MINUTES_MS = 10 * 60 * 1e3;
|
|
411
415
|
var PROVIDERS = {
|
|
@@ -430,6 +434,17 @@ var PROVIDERS = {
|
|
|
430
434
|
redirectStorage: "localStorage",
|
|
431
435
|
pendingRequestTtlMs: TEN_MINUTES_MS,
|
|
432
436
|
popupTimeoutMs: TEN_MINUTES_MS
|
|
437
|
+
},
|
|
438
|
+
// IAM answers a password login, signup or change with the same single-use
|
|
439
|
+
// exchange code the email flow gets. Signing up waits on a code read from a
|
|
440
|
+
// mailbox, hence the email timeout; there is no link, so nothing pending is
|
|
441
|
+
// written for another tab.
|
|
442
|
+
password: {
|
|
443
|
+
label: "Password",
|
|
444
|
+
exchangePath: "/api/v1/auth/magic-link/exchange",
|
|
445
|
+
sendsRedirectUri: false,
|
|
446
|
+
redirectStorage: "sessionStorage",
|
|
447
|
+
popupTimeoutMs: TEN_MINUTES_MS
|
|
433
448
|
}
|
|
434
449
|
};
|
|
435
450
|
var POPUP_WIDTH = 480;
|
|
@@ -502,6 +517,7 @@ var AuthPageFlow = class {
|
|
|
502
517
|
providerLabel;
|
|
503
518
|
redirectStorageKey;
|
|
504
519
|
popupPromise = null;
|
|
520
|
+
popupView;
|
|
505
521
|
constructor(config) {
|
|
506
522
|
this.appId = config.appId;
|
|
507
523
|
this.apiUrl = stripTrailingSlashes(config.apiUrl);
|
|
@@ -520,9 +536,24 @@ var AuthPageFlow = class {
|
|
|
520
536
|
if (options.mode !== void 0 && options.mode !== "popup") {
|
|
521
537
|
return Promise.reject(new Error(`Unsupported ${this.providerLabel} sign-in mode: ${String(options.mode)}`));
|
|
522
538
|
}
|
|
523
|
-
|
|
524
|
-
|
|
539
|
+
return this.signInWithPopup({});
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Opens the auth page in a popup on the given screen. A second call while the
|
|
543
|
+
* popup is open joins it when it asks for the same screen, and is refused
|
|
544
|
+
* otherwise: both would share one browser window, and the second would
|
|
545
|
+
* navigate it away from the first.
|
|
546
|
+
*/
|
|
547
|
+
signInWithPopup(request) {
|
|
548
|
+
const browserWindow = this.requireBrowser();
|
|
549
|
+
if (this.popupPromise) {
|
|
550
|
+
if (this.popupView === request.view) return this.popupPromise;
|
|
551
|
+
return Promise.reject(new Error(`Another ${this.providerLabel} popup is already open.`));
|
|
552
|
+
}
|
|
553
|
+
this.popupView = request.view;
|
|
554
|
+
this.popupPromise = this.startPopup(browserWindow, request).finally(() => {
|
|
525
555
|
this.popupPromise = null;
|
|
556
|
+
this.popupView = void 0;
|
|
526
557
|
});
|
|
527
558
|
return this.popupPromise;
|
|
528
559
|
}
|
|
@@ -588,7 +619,7 @@ var AuthPageFlow = class {
|
|
|
588
619
|
}
|
|
589
620
|
return this.exchangeCode(code, context.redirectUri);
|
|
590
621
|
}
|
|
591
|
-
async startPopup(browserWindow) {
|
|
622
|
+
async startPopup(browserWindow, request) {
|
|
592
623
|
const state = this.generateState();
|
|
593
624
|
const authPageUrl = resolveAuthPageUrl(
|
|
594
625
|
this.apiUrl,
|
|
@@ -602,8 +633,17 @@ var AuthPageFlow = class {
|
|
|
602
633
|
this.newRedirectContext(state, this.getRedirectUri(authPageUrl))
|
|
603
634
|
);
|
|
604
635
|
}
|
|
605
|
-
const popup = this.openPopup(
|
|
606
|
-
|
|
636
|
+
const popup = this.openPopup(
|
|
637
|
+
browserWindow,
|
|
638
|
+
this.buildStartUrl(browserWindow, authPageUrl, state, request.view)
|
|
639
|
+
);
|
|
640
|
+
const result = await this.waitForPopupResult(
|
|
641
|
+
browserWindow,
|
|
642
|
+
popup,
|
|
643
|
+
authPageUrl.origin,
|
|
644
|
+
state,
|
|
645
|
+
request.sessionToken
|
|
646
|
+
);
|
|
607
647
|
if (completedInAnotherTab) this.clearRedirectContext(browserWindow);
|
|
608
648
|
if (result.code) return this.exchangeCode(result.code, this.getRedirectUri(authPageUrl));
|
|
609
649
|
return expectAuthTokenResponse(result.token);
|
|
@@ -684,7 +724,7 @@ var AuthPageFlow = class {
|
|
|
684
724
|
});
|
|
685
725
|
return expectAuthTokenResponse(response);
|
|
686
726
|
}
|
|
687
|
-
buildStartUrl(browserWindow, authPageUrl, state) {
|
|
727
|
+
buildStartUrl(browserWindow, authPageUrl, state, view) {
|
|
688
728
|
const startUrl = new URL(authPageUrl);
|
|
689
729
|
startUrl.searchParams.set("provider", this.provider);
|
|
690
730
|
startUrl.searchParams.set("state", state);
|
|
@@ -692,6 +732,7 @@ var AuthPageFlow = class {
|
|
|
692
732
|
startUrl.searchParams.set("apiUrl", this.apiUrl);
|
|
693
733
|
startUrl.searchParams.set("origin", browserWindow.location.origin);
|
|
694
734
|
startUrl.searchParams.set("responseType", "code");
|
|
735
|
+
if (view) startUrl.searchParams.set("view", view);
|
|
695
736
|
return startUrl;
|
|
696
737
|
}
|
|
697
738
|
getRedirectUri(authPageUrl) {
|
|
@@ -748,7 +789,7 @@ var AuthPageFlow = class {
|
|
|
748
789
|
}
|
|
749
790
|
return popup;
|
|
750
791
|
}
|
|
751
|
-
waitForPopupResult(browserWindow, popup, expectedOrigin, expectedState) {
|
|
792
|
+
waitForPopupResult(browserWindow, popup, expectedOrigin, expectedState, sessionToken) {
|
|
752
793
|
return new Promise((resolve, reject) => {
|
|
753
794
|
const timeout = globalThis.setTimeout(() => {
|
|
754
795
|
cleanup();
|
|
@@ -760,31 +801,33 @@ var AuthPageFlow = class {
|
|
|
760
801
|
reject(new Error(`${this.providerLabel} sign-in was cancelled.`));
|
|
761
802
|
}
|
|
762
803
|
}, POPUP_CLOSED_POLL_MS);
|
|
804
|
+
let settled = false;
|
|
805
|
+
const handOverSession = async (readToken) => {
|
|
806
|
+
const accessToken = await readToken().catch(() => null);
|
|
807
|
+
if (settled) return;
|
|
808
|
+
if (!accessToken) {
|
|
809
|
+
cleanup();
|
|
810
|
+
reject(new Error(`${this.providerLabel} popup needs a signed-in session.`));
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
popup.postMessage({ type: SESSION_TYPE, state: expectedState, accessToken }, expectedOrigin);
|
|
814
|
+
};
|
|
763
815
|
const onMessage = (event) => {
|
|
764
816
|
if (event.origin !== expectedOrigin || event.source !== popup) return;
|
|
765
817
|
if (!event.data || typeof event.data !== "object") return;
|
|
766
818
|
const data = event.data;
|
|
767
|
-
if (data.type
|
|
768
|
-
|
|
769
|
-
cleanup();
|
|
770
|
-
reject(new Error(`Invalid ${this.providerLabel} sign-in state (possible CSRF).`));
|
|
771
|
-
return;
|
|
772
|
-
}
|
|
773
|
-
if (data.success !== true) {
|
|
774
|
-
cleanup();
|
|
775
|
-
reject(new Error(typeof data.error === "string" && data.error.trim() ? data.error : `${this.providerLabel} sign-in failed.`));
|
|
776
|
-
return;
|
|
777
|
-
}
|
|
778
|
-
const code = typeof data.code === "string" && data.code.trim() ? data.code : void 0;
|
|
779
|
-
if (!code && data.token === void 0) {
|
|
780
|
-
cleanup();
|
|
781
|
-
reject(new Error(`${this.providerLabel} auth page returned neither code nor token.`));
|
|
819
|
+
if (data.type === SESSION_REQUEST_TYPE) {
|
|
820
|
+
if (sessionToken && data.state === expectedState) void handOverSession(sessionToken);
|
|
782
821
|
return;
|
|
783
822
|
}
|
|
823
|
+
if (data.type !== RESULT_TYPE) return;
|
|
824
|
+
const outcome = readPopupResult(data, expectedState, this.providerLabel);
|
|
784
825
|
cleanup();
|
|
785
|
-
|
|
826
|
+
if ("error" in outcome) reject(outcome.error);
|
|
827
|
+
else resolve(outcome.value);
|
|
786
828
|
};
|
|
787
829
|
const cleanup = () => {
|
|
830
|
+
settled = true;
|
|
788
831
|
globalThis.clearTimeout(timeout);
|
|
789
832
|
globalThis.clearInterval(closedPoll);
|
|
790
833
|
browserWindow.removeEventListener("message", onMessage);
|
|
@@ -840,6 +883,30 @@ var AuthPageFlow = class {
|
|
|
840
883
|
return globalThis.window;
|
|
841
884
|
}
|
|
842
885
|
};
|
|
886
|
+
function readPopupResult(data, expectedState, providerLabel) {
|
|
887
|
+
if (data.state !== expectedState) {
|
|
888
|
+
return { error: new Error(`Invalid ${providerLabel} sign-in state (possible CSRF).`) };
|
|
889
|
+
}
|
|
890
|
+
if (data.success !== true && data.passwordChanged === true) {
|
|
891
|
+
return {
|
|
892
|
+
error: new MitraApiError(
|
|
893
|
+
"The password was changed, but signing in again failed. Sign in with the new password.",
|
|
894
|
+
401,
|
|
895
|
+
PASSWORD_CHANGED_SIGN_IN_AGAIN
|
|
896
|
+
)
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
if (data.success !== true) {
|
|
900
|
+
return {
|
|
901
|
+
error: new Error(typeof data.error === "string" && data.error.trim() ? data.error : `${providerLabel} sign-in failed.`)
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
const code = typeof data.code === "string" && data.code.trim() ? data.code : void 0;
|
|
905
|
+
if (!code && data.token === void 0) {
|
|
906
|
+
return { error: new Error(`${providerLabel} auth page returned neither code nor token.`) };
|
|
907
|
+
}
|
|
908
|
+
return { value: { ...code ? { code } : {}, ...data.token !== void 0 ? { token: data.token } : {} } };
|
|
909
|
+
}
|
|
843
910
|
|
|
844
911
|
// src/modules/email-code-flow.ts
|
|
845
912
|
import { expectObject as expectObject2 } from "@mitralab.io/sdk-core";
|
|
@@ -1050,6 +1117,8 @@ var EmailCodeFlow = class {
|
|
|
1050
1117
|
|
|
1051
1118
|
// src/modules/auth.ts
|
|
1052
1119
|
var DEFAULT_TOKEN_VALIDITY_MS = 3e4;
|
|
1120
|
+
var LOGOUT_PATH = "/api/v1/auth/logout";
|
|
1121
|
+
var PASSWORD_RESET_PATH = "/api/v1/auth/password/reset";
|
|
1053
1122
|
var DEFAULT_BACKOFFICE_TOKEN_VALIDITY_MS = 6e4;
|
|
1054
1123
|
var sessionPorts = /* @__PURE__ */ new WeakMap();
|
|
1055
1124
|
function getAuthSessionPort(auth) {
|
|
@@ -1079,6 +1148,22 @@ function subjectOf(token) {
|
|
|
1079
1148
|
const sub = decodeJwtPayload(token)?.sub;
|
|
1080
1149
|
return typeof sub === "string" && sub !== "" ? sub : null;
|
|
1081
1150
|
}
|
|
1151
|
+
function isExclusiveAppSession(refreshToken) {
|
|
1152
|
+
return decodeJwtPayload(refreshToken)?.login_mode === "exclusive";
|
|
1153
|
+
}
|
|
1154
|
+
function expectPasswordResetRequestResult(value) {
|
|
1155
|
+
const response = expectObject3(
|
|
1156
|
+
value,
|
|
1157
|
+
"Password reset request response",
|
|
1158
|
+
coreErrors
|
|
1159
|
+
);
|
|
1160
|
+
if (typeof response.resendAfterSeconds !== "number" || !Number.isFinite(response.resendAfterSeconds)) {
|
|
1161
|
+
throw coreErrors.invalidResponse(
|
|
1162
|
+
"Password reset request response has an invalid resendAfterSeconds field"
|
|
1163
|
+
);
|
|
1164
|
+
}
|
|
1165
|
+
return { resendAfterSeconds: response.resendAfterSeconds };
|
|
1166
|
+
}
|
|
1082
1167
|
function isTokenExpiring(token, minValidityMs) {
|
|
1083
1168
|
const exp = decodeJwtPayload(token)?.exp;
|
|
1084
1169
|
if (typeof exp !== "number" || !Number.isFinite(exp)) return false;
|
|
@@ -1121,6 +1206,7 @@ var AuthModule = class {
|
|
|
1121
1206
|
googleAuth;
|
|
1122
1207
|
microsoftAuth;
|
|
1123
1208
|
emailAuth;
|
|
1209
|
+
passwordAuth;
|
|
1124
1210
|
emailCode;
|
|
1125
1211
|
brand = null;
|
|
1126
1212
|
constructor(appId, iamBaseUrl, options = {}) {
|
|
@@ -1159,6 +1245,13 @@ var AuthModule = class {
|
|
|
1159
1245
|
client: this.publicClient,
|
|
1160
1246
|
provider: "email"
|
|
1161
1247
|
});
|
|
1248
|
+
this.passwordAuth = new AuthPageFlow({
|
|
1249
|
+
appId,
|
|
1250
|
+
apiUrl,
|
|
1251
|
+
authPageUrl: options.authPageUrl,
|
|
1252
|
+
client: this.publicClient,
|
|
1253
|
+
provider: "password"
|
|
1254
|
+
});
|
|
1162
1255
|
this.emailCode = new EmailCodeFlow({
|
|
1163
1256
|
appId,
|
|
1164
1257
|
client: this.publicClient,
|
|
@@ -1208,11 +1301,14 @@ var AuthModule = class {
|
|
|
1208
1301
|
get isAuthenticated() {
|
|
1209
1302
|
return this._currentUser !== null && this.#accessToken !== null;
|
|
1210
1303
|
}
|
|
1211
|
-
/**
|
|
1304
|
+
/**
|
|
1305
|
+
* @deprecated The password never goes through the app's code. Use
|
|
1306
|
+
* signInWithPassword(), which opens the Mitra auth page, signInWithEmail() or SSO.
|
|
1307
|
+
*/
|
|
1212
1308
|
signIn(_credentials) {
|
|
1213
1309
|
return Promise.reject(
|
|
1214
1310
|
new MitraApiError(
|
|
1215
|
-
"Email/password authentication is not available. Use signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1311
|
+
"Email/password authentication is not available. Use signInWithPassword(), signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1216
1312
|
0,
|
|
1217
1313
|
"UNSUPPORTED_AUTH_METHOD"
|
|
1218
1314
|
)
|
|
@@ -1456,19 +1552,150 @@ var AuthModule = class {
|
|
|
1456
1552
|
const tokenResponse = await this.emailAuth.completeRedirect();
|
|
1457
1553
|
return tokenResponse ? this.establishSession(tokenResponse) : null;
|
|
1458
1554
|
}
|
|
1459
|
-
/**
|
|
1555
|
+
/**
|
|
1556
|
+
* @deprecated The password never goes through the app's code. Use
|
|
1557
|
+
* signUpWithPassword(), which opens the Mitra auth page, signInWithEmail() or SSO.
|
|
1558
|
+
*/
|
|
1460
1559
|
signUp(_data) {
|
|
1461
1560
|
return Promise.reject(
|
|
1462
1561
|
new MitraApiError(
|
|
1463
|
-
"Email/password registration is not available. Use signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1562
|
+
"Email/password registration is not available. Use signUpWithPassword(), signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1464
1563
|
0,
|
|
1465
1564
|
"UNSUPPORTED_AUTH_METHOD"
|
|
1466
1565
|
)
|
|
1467
1566
|
);
|
|
1468
1567
|
}
|
|
1568
|
+
/**
|
|
1569
|
+
* Signs in with email and password, typed into the Mitra auth page in a popup.
|
|
1570
|
+
* The password never reaches the app's code: the page sends it to IAM and
|
|
1571
|
+
* hands back a single-use exchange code, redeemed like the email sign-in.
|
|
1572
|
+
*
|
|
1573
|
+
* Offer it only when `mitra.passwordLoginEnabled` is `true`, which needs an
|
|
1574
|
+
* app whose accounts are its own (exclusive login mode). Call it straight
|
|
1575
|
+
* from the click handler, before any `await`, or the browser blocks the popup.
|
|
1576
|
+
*
|
|
1577
|
+
* @returns The authenticated and hydrated user.
|
|
1578
|
+
* @throws {MitraApiError} When IAM rejects the exchange code.
|
|
1579
|
+
* @throws {Error} When the browser blocks or cancels the popup, the flow times
|
|
1580
|
+
* out, or the response fails origin, source, state, or shape validation. Wrong
|
|
1581
|
+
* credentials are answered on the auth page itself, not here.
|
|
1582
|
+
*
|
|
1583
|
+
* @example
|
|
1584
|
+
* ```typescript
|
|
1585
|
+
* const user = await mitra.auth.signInWithPassword();
|
|
1586
|
+
* ```
|
|
1587
|
+
*/
|
|
1588
|
+
async signInWithPassword() {
|
|
1589
|
+
return this.establishSession(await this.passwordAuth.signInWithPopup({ view: "login" }));
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Creates a password for an app account, on the Mitra auth page in a popup:
|
|
1593
|
+
* the page asks for the address, sends a code to it to prove the mailbox is
|
|
1594
|
+
* the person's, and takes the password with that code. The person ends signed
|
|
1595
|
+
* in, as with {@link signInWithPassword}.
|
|
1596
|
+
*
|
|
1597
|
+
* @returns The authenticated and hydrated user.
|
|
1598
|
+
* @throws The same errors as {@link signInWithPassword}.
|
|
1599
|
+
*
|
|
1600
|
+
* @example
|
|
1601
|
+
* ```typescript
|
|
1602
|
+
* const user = await mitra.auth.signUpWithPassword();
|
|
1603
|
+
* ```
|
|
1604
|
+
*/
|
|
1605
|
+
async signUpWithPassword() {
|
|
1606
|
+
return this.establishSession(await this.passwordAuth.signInWithPopup({ view: "signup" }));
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Changes the password of the signed-in person, on the Mitra auth page in a
|
|
1610
|
+
* popup. The page asks for the current and the new password itself, so the
|
|
1611
|
+
* app never sees either one.
|
|
1612
|
+
*
|
|
1613
|
+
* The page acts on this session, so it asks for the access token once it
|
|
1614
|
+
* loads, and the SDK answers with `postMessage` to the auth page origin only:
|
|
1615
|
+
* the token never goes in the popup URL.
|
|
1616
|
+
*
|
|
1617
|
+
* IAM ends every session of the account when the password changes, this one
|
|
1618
|
+
* included, so the page signs in again with the new password and hands back a
|
|
1619
|
+
* new session, which replaces the current one.
|
|
1620
|
+
*
|
|
1621
|
+
* When the change goes through but that new sign-in fails, the call rejects
|
|
1622
|
+
* with `PASSWORD_CHANGED_SIGN_IN_AGAIN` and the local session is cleared,
|
|
1623
|
+
* since IAM already ended it: send the person to sign in with the new password.
|
|
1624
|
+
*
|
|
1625
|
+
* @returns The user of the new session.
|
|
1626
|
+
* @throws {MitraApiError} `401` when nobody is signed in, and
|
|
1627
|
+
* `PASSWORD_CHANGED_SIGN_IN_AGAIN` (status `401`) as above.
|
|
1628
|
+
* @throws The same errors as {@link signInWithPassword}, and an error when the
|
|
1629
|
+
* session ends while the popup is open.
|
|
1630
|
+
*
|
|
1631
|
+
* @example
|
|
1632
|
+
* ```typescript
|
|
1633
|
+
* await mitra.auth.changePassword();
|
|
1634
|
+
* ```
|
|
1635
|
+
*/
|
|
1636
|
+
async changePassword() {
|
|
1637
|
+
if (!this.#accessToken) {
|
|
1638
|
+
throw new MitraApiError("Sign in before changing the password.", 401);
|
|
1639
|
+
}
|
|
1640
|
+
const sessionToken = async () => {
|
|
1641
|
+
await this.ensureFreshSession();
|
|
1642
|
+
return this.#accessToken;
|
|
1643
|
+
};
|
|
1644
|
+
const generation = this.sessionGeneration;
|
|
1645
|
+
let tokenResponse;
|
|
1646
|
+
try {
|
|
1647
|
+
tokenResponse = await this.passwordAuth.signInWithPopup({ view: "change", sessionToken });
|
|
1648
|
+
} catch (error) {
|
|
1649
|
+
if (error instanceof MitraApiError && error.code === PASSWORD_CHANGED_SIGN_IN_AGAIN && generation === this.sessionGeneration) {
|
|
1650
|
+
this.clearAuthState();
|
|
1651
|
+
}
|
|
1652
|
+
throw error;
|
|
1653
|
+
}
|
|
1654
|
+
return this.establishSession(tokenResponse);
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Asks IAM to email a link to set a new password. Only the address goes: the
|
|
1658
|
+
* link opens the Mitra auth page, where the new password is typed, so there
|
|
1659
|
+
* is no method here to confirm the reset.
|
|
1660
|
+
*
|
|
1661
|
+
* IAM answers the same whether or not the address has an account, so this
|
|
1662
|
+
* resolves without saying which.
|
|
1663
|
+
*
|
|
1664
|
+
* @param email - The address of the account.
|
|
1665
|
+
* @param options - The language of the message.
|
|
1666
|
+
* @returns How long the application should wait before offering to resend.
|
|
1667
|
+
* @throws {MitraApiError} `RATE_LIMITED` when this address asked too often,
|
|
1668
|
+
* and `RATE_LIMIT_EXCEEDED` when this network did, both with
|
|
1669
|
+
* `retryAfterSeconds` when IAM sends it.
|
|
1670
|
+
*
|
|
1671
|
+
* @example
|
|
1672
|
+
* ```typescript
|
|
1673
|
+
* const { resendAfterSeconds } = await mitra.auth.requestPasswordReset('person@example.com');
|
|
1674
|
+
* ```
|
|
1675
|
+
*/
|
|
1676
|
+
async requestPasswordReset(email, options = {}) {
|
|
1677
|
+
if (globalThis.window === void 0) {
|
|
1678
|
+
throw new Error("Password reset is only available in a browser.");
|
|
1679
|
+
}
|
|
1680
|
+
const browserWindow = globalThis.window;
|
|
1681
|
+
return expectPasswordResetRequestResult(
|
|
1682
|
+
await this.publicClient.post(PASSWORD_RESET_PATH, {
|
|
1683
|
+
email,
|
|
1684
|
+
appId: this.appId,
|
|
1685
|
+
origin: browserWindow.location.origin,
|
|
1686
|
+
language: resolveLanguage(options.language, browserWindow)
|
|
1687
|
+
})
|
|
1688
|
+
);
|
|
1689
|
+
}
|
|
1469
1690
|
/**
|
|
1470
1691
|
* Signs out the current user, clearing all auth state and localStorage.
|
|
1471
1692
|
*
|
|
1693
|
+
* In an app whose accounts are its own, the session also ends on the server:
|
|
1694
|
+
* the refresh token is sent to IAM's logout, so it can no longer be renewed.
|
|
1695
|
+
* The local state is cleared right away whatever IAM answers, and the request
|
|
1696
|
+
* survives a navigation to `redirectUrl`. The access token already issued
|
|
1697
|
+
* stays valid until it expires.
|
|
1698
|
+
*
|
|
1472
1699
|
* @param redirectUrl - Optional URL to navigate to after sign-out.
|
|
1473
1700
|
*
|
|
1474
1701
|
* @example
|
|
@@ -1478,7 +1705,11 @@ var AuthModule = class {
|
|
|
1478
1705
|
* ```
|
|
1479
1706
|
*/
|
|
1480
1707
|
signOut(redirectUrl) {
|
|
1708
|
+
const refreshToken = this.#refreshToken;
|
|
1481
1709
|
this.clearAuthState();
|
|
1710
|
+
if (refreshToken && isExclusiveAppSession(refreshToken)) {
|
|
1711
|
+
this.endServerSession(refreshToken);
|
|
1712
|
+
}
|
|
1482
1713
|
if (globalThis.window !== void 0 && redirectUrl) {
|
|
1483
1714
|
globalThis.window.location.href = redirectUrl;
|
|
1484
1715
|
}
|
|
@@ -1807,6 +2038,10 @@ var AuthModule = class {
|
|
|
1807
2038
|
this.listeners.delete(callback);
|
|
1808
2039
|
};
|
|
1809
2040
|
}
|
|
2041
|
+
/** Best effort: the person asked to leave, and that already happened locally. */
|
|
2042
|
+
endServerSession(refreshToken) {
|
|
2043
|
+
this.publicClient.request(LOGOUT_PATH, { method: "POST", body: { refreshToken }, keepalive: true }).catch(() => void 0);
|
|
2044
|
+
}
|
|
1810
2045
|
async doRefresh(generation, refreshToken) {
|
|
1811
2046
|
let tokenResponse;
|
|
1812
2047
|
try {
|
|
@@ -2665,7 +2900,7 @@ function createBrowserAgentCredentialsModule(httpClient) {
|
|
|
2665
2900
|
|
|
2666
2901
|
// src/client.ts
|
|
2667
2902
|
function expectAppInfoResponse(value) {
|
|
2668
|
-
const response =
|
|
2903
|
+
const response = expectObject4(
|
|
2669
2904
|
value,
|
|
2670
2905
|
"App info response",
|
|
2671
2906
|
coreErrors
|
|
@@ -2688,6 +2923,7 @@ function expectAppInfoResponse(value) {
|
|
|
2688
2923
|
// answers without this field, and an app that cannot prove it is enabled
|
|
2689
2924
|
// should stop offering the option, not stop starting.
|
|
2690
2925
|
emailLoginEnabled: response.emailLoginEnabled === true,
|
|
2926
|
+
passwordLoginEnabled: response.passwordLoginEnabled === true,
|
|
2691
2927
|
// Same reasoning, and the brand is never guessed: without it the headless
|
|
2692
2928
|
// email methods refuse instead of asking IAM to brand a message at random.
|
|
2693
2929
|
brand: typeof response.brand === "string" && response.brand.trim() ? response.brand : null
|
|
@@ -2766,6 +3002,7 @@ function createClient(config) {
|
|
|
2766
3002
|
let initialized = false;
|
|
2767
3003
|
let allowSignup = true;
|
|
2768
3004
|
let emailLoginEnabled = false;
|
|
3005
|
+
let passwordLoginEnabled = false;
|
|
2769
3006
|
async function init() {
|
|
2770
3007
|
if (initialized) return;
|
|
2771
3008
|
const publicClient = new HttpClient({
|
|
@@ -2785,6 +3022,7 @@ function createClient(config) {
|
|
|
2785
3022
|
}
|
|
2786
3023
|
allowSignup = appInfo.allowSignup;
|
|
2787
3024
|
emailLoginEnabled = appInfo.emailLoginEnabled;
|
|
3025
|
+
passwordLoginEnabled = appInfo.passwordLoginEnabled;
|
|
2788
3026
|
initialized = true;
|
|
2789
3027
|
}
|
|
2790
3028
|
return {
|
|
@@ -2803,6 +3041,9 @@ function createClient(config) {
|
|
|
2803
3041
|
get emailLoginEnabled() {
|
|
2804
3042
|
return emailLoginEnabled;
|
|
2805
3043
|
},
|
|
3044
|
+
get passwordLoginEnabled() {
|
|
3045
|
+
return passwordLoginEnabled;
|
|
3046
|
+
},
|
|
2806
3047
|
config
|
|
2807
3048
|
};
|
|
2808
3049
|
}
|