@mitralab.io/platform-sdk 1.7.0 → 1.8.0-beta.1
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 +63 -0
- package/README.md +53 -4
- package/dist/index.cjs +632 -48
- package/dist/index.d.cts +340 -7
- package/dist/index.d.ts +340 -7
- package/dist/index.js +635 -51
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -150,7 +150,7 @@ var HttpClient = class {
|
|
|
150
150
|
* ```
|
|
151
151
|
*/
|
|
152
152
|
async request(path, options = {}) {
|
|
153
|
-
const { method = "GET", body, headers = {}, params, isRetry } = options;
|
|
153
|
+
const { method = "GET", body, headers = {}, params, isRetry, keepalive } = options;
|
|
154
154
|
const url = buildRequestUrl(this.baseUrl, path, params);
|
|
155
155
|
if (!isRetry && this.tokenGetter() && this.beforeAuthenticatedRequest) {
|
|
156
156
|
await this.beforeAuthenticatedRequest();
|
|
@@ -168,7 +168,8 @@ var HttpClient = class {
|
|
|
168
168
|
method,
|
|
169
169
|
headers: requestHeaders,
|
|
170
170
|
body: body ? JSON.stringify(body) : void 0,
|
|
171
|
-
redirect: "manual"
|
|
171
|
+
redirect: "manual",
|
|
172
|
+
...keepalive ? { keepalive } : {}
|
|
172
173
|
});
|
|
173
174
|
if (response.redirected || response.type === "opaqueredirect") {
|
|
174
175
|
const error = new MitraApiError(
|
|
@@ -456,6 +457,9 @@ async function resolveApiKeySession(publicClient, appId, apiKey) {
|
|
|
456
457
|
// src/modules/auth-page-flow.ts
|
|
457
458
|
var import_sdk_core2 = require("@mitralab.io/sdk-core");
|
|
458
459
|
var RESULT_TYPE = "mitra-oauth-result";
|
|
460
|
+
var SESSION_REQUEST_TYPE = "mitra-auth-session-request";
|
|
461
|
+
var SESSION_TYPE = "mitra-auth-session";
|
|
462
|
+
var PASSWORD_CHANGED_SIGN_IN_AGAIN = "PASSWORD_CHANGED_SIGN_IN_AGAIN";
|
|
459
463
|
var FIVE_MINUTES_MS = 5 * 60 * 1e3;
|
|
460
464
|
var TEN_MINUTES_MS = 10 * 60 * 1e3;
|
|
461
465
|
var PROVIDERS = {
|
|
@@ -480,6 +484,17 @@ var PROVIDERS = {
|
|
|
480
484
|
redirectStorage: "localStorage",
|
|
481
485
|
pendingRequestTtlMs: TEN_MINUTES_MS,
|
|
482
486
|
popupTimeoutMs: TEN_MINUTES_MS
|
|
487
|
+
},
|
|
488
|
+
// IAM answers a password login, signup or change with the same single-use
|
|
489
|
+
// exchange code the email flow gets. Signing up waits on a code read from a
|
|
490
|
+
// mailbox, hence the email timeout; there is no link, so nothing pending is
|
|
491
|
+
// written for another tab.
|
|
492
|
+
password: {
|
|
493
|
+
label: "Password",
|
|
494
|
+
exchangePath: "/api/v1/auth/magic-link/exchange",
|
|
495
|
+
sendsRedirectUri: false,
|
|
496
|
+
redirectStorage: "sessionStorage",
|
|
497
|
+
popupTimeoutMs: TEN_MINUTES_MS
|
|
483
498
|
}
|
|
484
499
|
};
|
|
485
500
|
var POPUP_WIDTH = 480;
|
|
@@ -552,6 +567,7 @@ var AuthPageFlow = class {
|
|
|
552
567
|
providerLabel;
|
|
553
568
|
redirectStorageKey;
|
|
554
569
|
popupPromise = null;
|
|
570
|
+
popupView;
|
|
555
571
|
constructor(config) {
|
|
556
572
|
this.appId = config.appId;
|
|
557
573
|
this.apiUrl = stripTrailingSlashes(config.apiUrl);
|
|
@@ -570,9 +586,24 @@ var AuthPageFlow = class {
|
|
|
570
586
|
if (options.mode !== void 0 && options.mode !== "popup") {
|
|
571
587
|
return Promise.reject(new Error(`Unsupported ${this.providerLabel} sign-in mode: ${String(options.mode)}`));
|
|
572
588
|
}
|
|
573
|
-
|
|
574
|
-
|
|
589
|
+
return this.signInWithPopup({});
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Opens the auth page in a popup on the given screen. A second call while the
|
|
593
|
+
* popup is open joins it when it asks for the same screen, and is refused
|
|
594
|
+
* otherwise: both would share one browser window, and the second would
|
|
595
|
+
* navigate it away from the first.
|
|
596
|
+
*/
|
|
597
|
+
signInWithPopup(request) {
|
|
598
|
+
const browserWindow = this.requireBrowser();
|
|
599
|
+
if (this.popupPromise) {
|
|
600
|
+
if (this.popupView === request.view) return this.popupPromise;
|
|
601
|
+
return Promise.reject(new Error(`Another ${this.providerLabel} popup is already open.`));
|
|
602
|
+
}
|
|
603
|
+
this.popupView = request.view;
|
|
604
|
+
this.popupPromise = this.startPopup(browserWindow, request).finally(() => {
|
|
575
605
|
this.popupPromise = null;
|
|
606
|
+
this.popupView = void 0;
|
|
576
607
|
});
|
|
577
608
|
return this.popupPromise;
|
|
578
609
|
}
|
|
@@ -638,7 +669,7 @@ var AuthPageFlow = class {
|
|
|
638
669
|
}
|
|
639
670
|
return this.exchangeCode(code, context.redirectUri);
|
|
640
671
|
}
|
|
641
|
-
async startPopup(browserWindow) {
|
|
672
|
+
async startPopup(browserWindow, request) {
|
|
642
673
|
const state = this.generateState();
|
|
643
674
|
const authPageUrl = resolveAuthPageUrl(
|
|
644
675
|
this.apiUrl,
|
|
@@ -652,8 +683,17 @@ var AuthPageFlow = class {
|
|
|
652
683
|
this.newRedirectContext(state, this.getRedirectUri(authPageUrl))
|
|
653
684
|
);
|
|
654
685
|
}
|
|
655
|
-
const popup = this.openPopup(
|
|
656
|
-
|
|
686
|
+
const popup = this.openPopup(
|
|
687
|
+
browserWindow,
|
|
688
|
+
this.buildStartUrl(browserWindow, authPageUrl, state, request.view)
|
|
689
|
+
);
|
|
690
|
+
const result = await this.waitForPopupResult(
|
|
691
|
+
browserWindow,
|
|
692
|
+
popup,
|
|
693
|
+
authPageUrl.origin,
|
|
694
|
+
state,
|
|
695
|
+
request.sessionToken
|
|
696
|
+
);
|
|
657
697
|
if (completedInAnotherTab) this.clearRedirectContext(browserWindow);
|
|
658
698
|
if (result.code) return this.exchangeCode(result.code, this.getRedirectUri(authPageUrl));
|
|
659
699
|
return expectAuthTokenResponse(result.token);
|
|
@@ -734,7 +774,7 @@ var AuthPageFlow = class {
|
|
|
734
774
|
});
|
|
735
775
|
return expectAuthTokenResponse(response);
|
|
736
776
|
}
|
|
737
|
-
buildStartUrl(browserWindow, authPageUrl, state) {
|
|
777
|
+
buildStartUrl(browserWindow, authPageUrl, state, view) {
|
|
738
778
|
const startUrl = new URL(authPageUrl);
|
|
739
779
|
startUrl.searchParams.set("provider", this.provider);
|
|
740
780
|
startUrl.searchParams.set("state", state);
|
|
@@ -742,6 +782,7 @@ var AuthPageFlow = class {
|
|
|
742
782
|
startUrl.searchParams.set("apiUrl", this.apiUrl);
|
|
743
783
|
startUrl.searchParams.set("origin", browserWindow.location.origin);
|
|
744
784
|
startUrl.searchParams.set("responseType", "code");
|
|
785
|
+
if (view) startUrl.searchParams.set("view", view);
|
|
745
786
|
return startUrl;
|
|
746
787
|
}
|
|
747
788
|
getRedirectUri(authPageUrl) {
|
|
@@ -798,7 +839,7 @@ var AuthPageFlow = class {
|
|
|
798
839
|
}
|
|
799
840
|
return popup;
|
|
800
841
|
}
|
|
801
|
-
waitForPopupResult(browserWindow, popup, expectedOrigin, expectedState) {
|
|
842
|
+
waitForPopupResult(browserWindow, popup, expectedOrigin, expectedState, sessionToken) {
|
|
802
843
|
return new Promise((resolve, reject) => {
|
|
803
844
|
const timeout = globalThis.setTimeout(() => {
|
|
804
845
|
cleanup();
|
|
@@ -810,31 +851,33 @@ var AuthPageFlow = class {
|
|
|
810
851
|
reject(new Error(`${this.providerLabel} sign-in was cancelled.`));
|
|
811
852
|
}
|
|
812
853
|
}, POPUP_CLOSED_POLL_MS);
|
|
854
|
+
let settled = false;
|
|
855
|
+
const handOverSession = async (readToken) => {
|
|
856
|
+
const accessToken = await readToken().catch(() => null);
|
|
857
|
+
if (settled) return;
|
|
858
|
+
if (!accessToken) {
|
|
859
|
+
cleanup();
|
|
860
|
+
reject(new Error(`${this.providerLabel} popup needs a signed-in session.`));
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
popup.postMessage({ type: SESSION_TYPE, state: expectedState, accessToken }, expectedOrigin);
|
|
864
|
+
};
|
|
813
865
|
const onMessage = (event) => {
|
|
814
866
|
if (event.origin !== expectedOrigin || event.source !== popup) return;
|
|
815
867
|
if (!event.data || typeof event.data !== "object") return;
|
|
816
868
|
const data = event.data;
|
|
817
|
-
if (data.type
|
|
818
|
-
|
|
819
|
-
cleanup();
|
|
820
|
-
reject(new Error(`Invalid ${this.providerLabel} sign-in state (possible CSRF).`));
|
|
821
|
-
return;
|
|
822
|
-
}
|
|
823
|
-
if (data.success !== true) {
|
|
824
|
-
cleanup();
|
|
825
|
-
reject(new Error(typeof data.error === "string" && data.error.trim() ? data.error : `${this.providerLabel} sign-in failed.`));
|
|
826
|
-
return;
|
|
827
|
-
}
|
|
828
|
-
const code = typeof data.code === "string" && data.code.trim() ? data.code : void 0;
|
|
829
|
-
if (!code && data.token === void 0) {
|
|
830
|
-
cleanup();
|
|
831
|
-
reject(new Error(`${this.providerLabel} auth page returned neither code nor token.`));
|
|
869
|
+
if (data.type === SESSION_REQUEST_TYPE) {
|
|
870
|
+
if (sessionToken && data.state === expectedState) void handOverSession(sessionToken);
|
|
832
871
|
return;
|
|
833
872
|
}
|
|
873
|
+
if (data.type !== RESULT_TYPE) return;
|
|
874
|
+
const outcome = readPopupResult(data, expectedState, this.providerLabel);
|
|
834
875
|
cleanup();
|
|
835
|
-
|
|
876
|
+
if ("error" in outcome) reject(outcome.error);
|
|
877
|
+
else resolve(outcome.value);
|
|
836
878
|
};
|
|
837
879
|
const cleanup = () => {
|
|
880
|
+
settled = true;
|
|
838
881
|
globalThis.clearTimeout(timeout);
|
|
839
882
|
globalThis.clearInterval(closedPoll);
|
|
840
883
|
browserWindow.removeEventListener("message", onMessage);
|
|
@@ -890,6 +933,30 @@ var AuthPageFlow = class {
|
|
|
890
933
|
return globalThis.window;
|
|
891
934
|
}
|
|
892
935
|
};
|
|
936
|
+
function readPopupResult(data, expectedState, providerLabel) {
|
|
937
|
+
if (data.state !== expectedState) {
|
|
938
|
+
return { error: new Error(`Invalid ${providerLabel} sign-in state (possible CSRF).`) };
|
|
939
|
+
}
|
|
940
|
+
if (data.success !== true && data.passwordChanged === true) {
|
|
941
|
+
return {
|
|
942
|
+
error: new MitraApiError(
|
|
943
|
+
"The password was changed, but signing in again failed. Sign in with the new password.",
|
|
944
|
+
401,
|
|
945
|
+
PASSWORD_CHANGED_SIGN_IN_AGAIN
|
|
946
|
+
)
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
if (data.success !== true) {
|
|
950
|
+
return {
|
|
951
|
+
error: new Error(typeof data.error === "string" && data.error.trim() ? data.error : `${providerLabel} sign-in failed.`)
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
const code = typeof data.code === "string" && data.code.trim() ? data.code : void 0;
|
|
955
|
+
if (!code && data.token === void 0) {
|
|
956
|
+
return { error: new Error(`${providerLabel} auth page returned neither code nor token.`) };
|
|
957
|
+
}
|
|
958
|
+
return { value: { ...code ? { code } : {}, ...data.token !== void 0 ? { token: data.token } : {} } };
|
|
959
|
+
}
|
|
893
960
|
|
|
894
961
|
// src/modules/email-code-flow.ts
|
|
895
962
|
var import_sdk_core3 = require("@mitralab.io/sdk-core");
|
|
@@ -925,32 +992,20 @@ function optionalString2(value) {
|
|
|
925
992
|
function isMagicLinkState(value) {
|
|
926
993
|
return value === "OK" || value === "EXPIRED" || value === "USED" || value === "INVALID";
|
|
927
994
|
}
|
|
928
|
-
function expectEmailCodeRequestResult(value) {
|
|
929
|
-
const response = (0, import_sdk_core3.expectObject)(
|
|
930
|
-
value,
|
|
931
|
-
"Email code request response",
|
|
932
|
-
coreErrors
|
|
933
|
-
);
|
|
995
|
+
function expectEmailCodeRequestResult(value, label = "Email code request response") {
|
|
996
|
+
const response = (0, import_sdk_core3.expectObject)(value, label, coreErrors);
|
|
934
997
|
if (typeof response.receipt !== "string" || !response.receipt.trim()) {
|
|
935
|
-
throw coreErrors.invalidResponse(
|
|
998
|
+
throw coreErrors.invalidResponse(`${label} has an invalid receipt field`);
|
|
936
999
|
}
|
|
937
1000
|
if (typeof response.resendAfterSeconds !== "number" || !Number.isFinite(response.resendAfterSeconds)) {
|
|
938
|
-
throw coreErrors.invalidResponse(
|
|
939
|
-
"Email code request response has an invalid resendAfterSeconds field"
|
|
940
|
-
);
|
|
1001
|
+
throw coreErrors.invalidResponse(`${label} has an invalid resendAfterSeconds field`);
|
|
941
1002
|
}
|
|
942
1003
|
return { receipt: response.receipt, resendAfterSeconds: response.resendAfterSeconds };
|
|
943
1004
|
}
|
|
944
|
-
function expectExchangeCode(value) {
|
|
945
|
-
const response = (0, import_sdk_core3.expectObject)(
|
|
946
|
-
value,
|
|
947
|
-
"Email sign-in exchange code response",
|
|
948
|
-
coreErrors
|
|
949
|
-
);
|
|
1005
|
+
function expectExchangeCode(value, label = "Email sign-in exchange code response") {
|
|
1006
|
+
const response = (0, import_sdk_core3.expectObject)(value, label, coreErrors);
|
|
950
1007
|
if (typeof response.exchangeCode !== "string" || !response.exchangeCode.trim()) {
|
|
951
|
-
throw coreErrors.invalidResponse(
|
|
952
|
-
"Email sign-in exchange code response has an invalid exchangeCode field"
|
|
953
|
-
);
|
|
1008
|
+
throw coreErrors.invalidResponse(`${label} has an invalid exchangeCode field`);
|
|
954
1009
|
}
|
|
955
1010
|
return response.exchangeCode;
|
|
956
1011
|
}
|
|
@@ -1098,8 +1153,148 @@ var EmailCodeFlow = class {
|
|
|
1098
1153
|
}
|
|
1099
1154
|
};
|
|
1100
1155
|
|
|
1156
|
+
// src/modules/password-form-flow.ts
|
|
1157
|
+
var PASSWORD_PATH = "/api/v1/auth/password";
|
|
1158
|
+
var LOGIN_PATH = `${PASSWORD_PATH}/login`;
|
|
1159
|
+
var SIGNUP_PATH = `${PASSWORD_PATH}/signup`;
|
|
1160
|
+
var SIGNUP_VERIFY_PATH = `${SIGNUP_PATH}/verify`;
|
|
1161
|
+
var RESET_CONFIRM_PATH = `${PASSWORD_PATH}/reset/confirm`;
|
|
1162
|
+
var RESET_TOKEN_KEY = "resetToken";
|
|
1163
|
+
var PASSWORD_FORM_NOT_ENABLED = "PASSWORD_FORM_NOT_ENABLED";
|
|
1164
|
+
function passwordChangedSignInAgain(cause) {
|
|
1165
|
+
const error = new MitraApiError(
|
|
1166
|
+
"The password was changed, but signing in again failed. Sign in with the new password.",
|
|
1167
|
+
401,
|
|
1168
|
+
PASSWORD_CHANGED_SIGN_IN_AGAIN
|
|
1169
|
+
);
|
|
1170
|
+
error.cause = cause;
|
|
1171
|
+
return error;
|
|
1172
|
+
}
|
|
1173
|
+
function explainFormNotEnabled(error) {
|
|
1174
|
+
if (!(error instanceof MitraApiError) || error.code !== PASSWORD_FORM_NOT_ENABLED) return error;
|
|
1175
|
+
return new MitraApiError(
|
|
1176
|
+
'This app does not accept the password from its own form. Turn on the "form in the app itself" mode of the password login in the app, or use the methods of the Mitra auth page: signInWithPassword(), signUpWithPassword() and changePassword().',
|
|
1177
|
+
error.status,
|
|
1178
|
+
error.code,
|
|
1179
|
+
error.details,
|
|
1180
|
+
error.retryAfterSeconds
|
|
1181
|
+
);
|
|
1182
|
+
}
|
|
1183
|
+
var PasswordFormFlow = class {
|
|
1184
|
+
appId;
|
|
1185
|
+
client;
|
|
1186
|
+
exchange;
|
|
1187
|
+
constructor(config) {
|
|
1188
|
+
this.appId = config.appId;
|
|
1189
|
+
this.client = config.client;
|
|
1190
|
+
this.exchange = config.exchange;
|
|
1191
|
+
}
|
|
1192
|
+
async signIn(credentials) {
|
|
1193
|
+
const browserWindow = this.requireBrowser();
|
|
1194
|
+
return this.formRequest(async () => {
|
|
1195
|
+
const exchangeCode = expectExchangeCode(
|
|
1196
|
+
await this.client.post(LOGIN_PATH, {
|
|
1197
|
+
email: credentials.email,
|
|
1198
|
+
password: credentials.password,
|
|
1199
|
+
appId: this.appId,
|
|
1200
|
+
origin: browserWindow.location.origin
|
|
1201
|
+
}),
|
|
1202
|
+
"Password login response"
|
|
1203
|
+
);
|
|
1204
|
+
return this.exchange.redeemExchangeCode(exchangeCode);
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
async startSignUp(request) {
|
|
1208
|
+
const browserWindow = this.requireBrowser();
|
|
1209
|
+
return expectEmailCodeRequestResult(
|
|
1210
|
+
await this.client.post(SIGNUP_PATH, {
|
|
1211
|
+
email: request.email,
|
|
1212
|
+
appId: this.appId,
|
|
1213
|
+
origin: browserWindow.location.origin,
|
|
1214
|
+
language: resolveLanguage(request.language, browserWindow)
|
|
1215
|
+
}),
|
|
1216
|
+
"Password sign-up request response"
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
async completeSignUp(completion) {
|
|
1220
|
+
this.requireBrowser();
|
|
1221
|
+
return this.formRequest(async () => {
|
|
1222
|
+
const exchangeCode = expectExchangeCode(
|
|
1223
|
+
await this.client.post(SIGNUP_VERIFY_PATH, {
|
|
1224
|
+
receipt: completion.receipt,
|
|
1225
|
+
code: completion.code,
|
|
1226
|
+
password: completion.password
|
|
1227
|
+
}),
|
|
1228
|
+
"Password sign-up verification response"
|
|
1229
|
+
);
|
|
1230
|
+
return this.exchange.redeemExchangeCode(exchangeCode);
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* The token goes in the header by hand, on a client that never retries. IAM
|
|
1235
|
+
* answers a wrong current password with `401`, and a client that refreshes
|
|
1236
|
+
* and retries on `401` would spend a second guess against the account's
|
|
1237
|
+
* lockout with the same password.
|
|
1238
|
+
*/
|
|
1239
|
+
async changePassword(change, accessToken) {
|
|
1240
|
+
this.requireBrowser();
|
|
1241
|
+
return this.formRequest(async () => {
|
|
1242
|
+
await this.client.request(PASSWORD_PATH, {
|
|
1243
|
+
method: "PUT",
|
|
1244
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
1245
|
+
body: {
|
|
1246
|
+
...change.currentPassword === void 0 ? {} : { currentPassword: change.currentPassword },
|
|
1247
|
+
newPassword: change.newPassword
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* The whole fragment leaves the URL, not only the token: the link IAM sends
|
|
1254
|
+
* to `/reset-password` carries only the token and the language of the
|
|
1255
|
+
* message, and the token would otherwise stay in the history and in every
|
|
1256
|
+
* reload.
|
|
1257
|
+
*/
|
|
1258
|
+
readResetToken() {
|
|
1259
|
+
const browserWindow = this.requireBrowser();
|
|
1260
|
+
const params = new URLSearchParams(browserWindow.location.hash.replace(/^#/, ""));
|
|
1261
|
+
if (!params.has(RESET_TOKEN_KEY)) return null;
|
|
1262
|
+
browserWindow.history.replaceState(
|
|
1263
|
+
{},
|
|
1264
|
+
"",
|
|
1265
|
+
`${browserWindow.location.pathname}${browserWindow.location.search}`
|
|
1266
|
+
);
|
|
1267
|
+
const token = params.get(RESET_TOKEN_KEY)?.trim();
|
|
1268
|
+
return token || null;
|
|
1269
|
+
}
|
|
1270
|
+
async confirmReset(confirmation) {
|
|
1271
|
+
this.requireBrowser();
|
|
1272
|
+
return this.formRequest(async () => {
|
|
1273
|
+
await this.client.post(RESET_CONFIRM_PATH, {
|
|
1274
|
+
token: confirmation.token,
|
|
1275
|
+
newPassword: confirmation.newPassword
|
|
1276
|
+
});
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
async formRequest(request) {
|
|
1280
|
+
try {
|
|
1281
|
+
return await request();
|
|
1282
|
+
} catch (error) {
|
|
1283
|
+
throw explainFormNotEnabled(error);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
requireBrowser() {
|
|
1287
|
+
if (globalThis.window === void 0) {
|
|
1288
|
+
throw new Error("Password login is only available in a browser.");
|
|
1289
|
+
}
|
|
1290
|
+
return globalThis.window;
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
|
|
1101
1294
|
// src/modules/auth.ts
|
|
1102
1295
|
var DEFAULT_TOKEN_VALIDITY_MS = 3e4;
|
|
1296
|
+
var LOGOUT_PATH = "/api/v1/auth/logout";
|
|
1297
|
+
var PASSWORD_RESET_PATH = "/api/v1/auth/password/reset";
|
|
1103
1298
|
var DEFAULT_BACKOFFICE_TOKEN_VALIDITY_MS = 6e4;
|
|
1104
1299
|
var sessionPorts = /* @__PURE__ */ new WeakMap();
|
|
1105
1300
|
function getAuthSessionPort(auth) {
|
|
@@ -1129,6 +1324,22 @@ function subjectOf(token) {
|
|
|
1129
1324
|
const sub = decodeJwtPayload(token)?.sub;
|
|
1130
1325
|
return typeof sub === "string" && sub !== "" ? sub : null;
|
|
1131
1326
|
}
|
|
1327
|
+
function isExclusiveAppSession(refreshToken) {
|
|
1328
|
+
return decodeJwtPayload(refreshToken)?.login_mode === "exclusive";
|
|
1329
|
+
}
|
|
1330
|
+
function expectPasswordResetRequestResult(value) {
|
|
1331
|
+
const response = (0, import_sdk_core4.expectObject)(
|
|
1332
|
+
value,
|
|
1333
|
+
"Password reset request response",
|
|
1334
|
+
coreErrors
|
|
1335
|
+
);
|
|
1336
|
+
if (typeof response.resendAfterSeconds !== "number" || !Number.isFinite(response.resendAfterSeconds)) {
|
|
1337
|
+
throw coreErrors.invalidResponse(
|
|
1338
|
+
"Password reset request response has an invalid resendAfterSeconds field"
|
|
1339
|
+
);
|
|
1340
|
+
}
|
|
1341
|
+
return { resendAfterSeconds: response.resendAfterSeconds };
|
|
1342
|
+
}
|
|
1132
1343
|
function isTokenExpiring(token, minValidityMs) {
|
|
1133
1344
|
const exp = decodeJwtPayload(token)?.exp;
|
|
1134
1345
|
if (typeof exp !== "number" || !Number.isFinite(exp)) return false;
|
|
@@ -1171,7 +1382,9 @@ var AuthModule = class {
|
|
|
1171
1382
|
googleAuth;
|
|
1172
1383
|
microsoftAuth;
|
|
1173
1384
|
emailAuth;
|
|
1385
|
+
passwordAuth;
|
|
1174
1386
|
emailCode;
|
|
1387
|
+
passwordForm;
|
|
1175
1388
|
brand = null;
|
|
1176
1389
|
constructor(appId, iamBaseUrl, options = {}) {
|
|
1177
1390
|
this.appId = appId;
|
|
@@ -1209,11 +1422,23 @@ var AuthModule = class {
|
|
|
1209
1422
|
client: this.publicClient,
|
|
1210
1423
|
provider: "email"
|
|
1211
1424
|
});
|
|
1425
|
+
this.passwordAuth = new AuthPageFlow({
|
|
1426
|
+
appId,
|
|
1427
|
+
apiUrl,
|
|
1428
|
+
authPageUrl: options.authPageUrl,
|
|
1429
|
+
client: this.publicClient,
|
|
1430
|
+
provider: "password"
|
|
1431
|
+
});
|
|
1212
1432
|
this.emailCode = new EmailCodeFlow({
|
|
1213
1433
|
appId,
|
|
1214
1434
|
client: this.publicClient,
|
|
1215
1435
|
pendingRequest: this.emailAuth
|
|
1216
1436
|
});
|
|
1437
|
+
this.passwordForm = new PasswordFormFlow({
|
|
1438
|
+
appId,
|
|
1439
|
+
client: this.publicClient,
|
|
1440
|
+
exchange: this.passwordAuth
|
|
1441
|
+
});
|
|
1217
1442
|
this.loadFromStorage();
|
|
1218
1443
|
const readAccessToken = () => this.#accessToken;
|
|
1219
1444
|
sessionPorts.set(this, {
|
|
@@ -1258,11 +1483,14 @@ var AuthModule = class {
|
|
|
1258
1483
|
get isAuthenticated() {
|
|
1259
1484
|
return this._currentUser !== null && this.#accessToken !== null;
|
|
1260
1485
|
}
|
|
1261
|
-
/**
|
|
1486
|
+
/**
|
|
1487
|
+
* @deprecated Use signInWithPassword(), which opens the Mitra auth page,
|
|
1488
|
+
* signInWithPasswordForm() in an app with the form mode on, signInWithEmail() or SSO.
|
|
1489
|
+
*/
|
|
1262
1490
|
signIn(_credentials) {
|
|
1263
1491
|
return Promise.reject(
|
|
1264
1492
|
new MitraApiError(
|
|
1265
|
-
"Email/password authentication is not available. Use signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1493
|
+
"Email/password authentication is not available. Use signInWithPassword(), signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1266
1494
|
0,
|
|
1267
1495
|
"UNSUPPORTED_AUTH_METHOD"
|
|
1268
1496
|
)
|
|
@@ -1506,19 +1734,361 @@ var AuthModule = class {
|
|
|
1506
1734
|
const tokenResponse = await this.emailAuth.completeRedirect();
|
|
1507
1735
|
return tokenResponse ? this.establishSession(tokenResponse) : null;
|
|
1508
1736
|
}
|
|
1509
|
-
/**
|
|
1737
|
+
/**
|
|
1738
|
+
* @deprecated Use signUpWithPassword(), which opens the Mitra auth page,
|
|
1739
|
+
* startPasswordSignUp() in an app with the form mode on, signInWithEmail() or SSO.
|
|
1740
|
+
*/
|
|
1510
1741
|
signUp(_data) {
|
|
1511
1742
|
return Promise.reject(
|
|
1512
1743
|
new MitraApiError(
|
|
1513
|
-
"Email/password registration is not available. Use signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1744
|
+
"Email/password registration is not available. Use signUpWithPassword(), signInWithEmail(), signInWithGoogle() or signInWithMicrosoft().",
|
|
1514
1745
|
0,
|
|
1515
1746
|
"UNSUPPORTED_AUTH_METHOD"
|
|
1516
1747
|
)
|
|
1517
1748
|
);
|
|
1518
1749
|
}
|
|
1750
|
+
/**
|
|
1751
|
+
* Signs in with email and password, typed into the Mitra auth page in a popup.
|
|
1752
|
+
* The password never reaches the app's code: the page sends it to IAM and
|
|
1753
|
+
* hands back a single-use exchange code, redeemed like the email sign-in.
|
|
1754
|
+
*
|
|
1755
|
+
* Offer it only when `mitra.passwordLoginEnabled` is `true`, which needs an
|
|
1756
|
+
* app whose accounts are its own (exclusive login mode). Call it straight
|
|
1757
|
+
* from the click handler, before any `await`, or the browser blocks the popup.
|
|
1758
|
+
*
|
|
1759
|
+
* @returns The authenticated and hydrated user.
|
|
1760
|
+
* @throws {MitraApiError} When IAM rejects the exchange code.
|
|
1761
|
+
* @throws {Error} When the browser blocks or cancels the popup, the flow times
|
|
1762
|
+
* out, or the response fails origin, source, state, or shape validation. Wrong
|
|
1763
|
+
* credentials are answered on the auth page itself, not here.
|
|
1764
|
+
*
|
|
1765
|
+
* @example
|
|
1766
|
+
* ```typescript
|
|
1767
|
+
* const user = await mitra.auth.signInWithPassword();
|
|
1768
|
+
* ```
|
|
1769
|
+
*/
|
|
1770
|
+
async signInWithPassword() {
|
|
1771
|
+
return this.establishSession(await this.passwordAuth.signInWithPopup({ view: "login" }));
|
|
1772
|
+
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Creates a password for an app account, on the Mitra auth page in a popup:
|
|
1775
|
+
* the page asks for the address, sends a code to it to prove the mailbox is
|
|
1776
|
+
* the person's, and takes the password with that code. The person ends signed
|
|
1777
|
+
* in, as with {@link signInWithPassword}.
|
|
1778
|
+
*
|
|
1779
|
+
* @returns The authenticated and hydrated user.
|
|
1780
|
+
* @throws The same errors as {@link signInWithPassword}.
|
|
1781
|
+
*
|
|
1782
|
+
* @example
|
|
1783
|
+
* ```typescript
|
|
1784
|
+
* const user = await mitra.auth.signUpWithPassword();
|
|
1785
|
+
* ```
|
|
1786
|
+
*/
|
|
1787
|
+
async signUpWithPassword() {
|
|
1788
|
+
return this.establishSession(await this.passwordAuth.signInWithPopup({ view: "signup" }));
|
|
1789
|
+
}
|
|
1790
|
+
/**
|
|
1791
|
+
* Changes the password of the signed-in person, on the Mitra auth page in a
|
|
1792
|
+
* popup. The page asks for the current and the new password itself, so the
|
|
1793
|
+
* app never sees either one.
|
|
1794
|
+
*
|
|
1795
|
+
* The page acts on this session, so it asks for the access token once it
|
|
1796
|
+
* loads, and the SDK answers with `postMessage` to the auth page origin only:
|
|
1797
|
+
* the token never goes in the popup URL.
|
|
1798
|
+
*
|
|
1799
|
+
* IAM ends every session of the account when the password changes, this one
|
|
1800
|
+
* included, so the page signs in again with the new password and hands back a
|
|
1801
|
+
* new session, which replaces the current one.
|
|
1802
|
+
*
|
|
1803
|
+
* When the change goes through but that new sign-in fails, the call rejects
|
|
1804
|
+
* with `PASSWORD_CHANGED_SIGN_IN_AGAIN` and the local session is cleared,
|
|
1805
|
+
* since IAM already ended it: send the person to sign in with the new password.
|
|
1806
|
+
*
|
|
1807
|
+
* @returns The user of the new session.
|
|
1808
|
+
* @throws {MitraApiError} `401` when nobody is signed in, and
|
|
1809
|
+
* `PASSWORD_CHANGED_SIGN_IN_AGAIN` (status `401`) as above.
|
|
1810
|
+
* @throws The same errors as {@link signInWithPassword}, and an error when the
|
|
1811
|
+
* session ends while the popup is open.
|
|
1812
|
+
*
|
|
1813
|
+
* @example
|
|
1814
|
+
* ```typescript
|
|
1815
|
+
* await mitra.auth.changePassword();
|
|
1816
|
+
* ```
|
|
1817
|
+
*/
|
|
1818
|
+
async changePassword() {
|
|
1819
|
+
if (!this.#accessToken) {
|
|
1820
|
+
throw new MitraApiError("Sign in before changing the password.", 401);
|
|
1821
|
+
}
|
|
1822
|
+
const sessionToken = async () => {
|
|
1823
|
+
await this.ensureFreshSession();
|
|
1824
|
+
return this.#accessToken;
|
|
1825
|
+
};
|
|
1826
|
+
const generation = this.sessionGeneration;
|
|
1827
|
+
let tokenResponse;
|
|
1828
|
+
try {
|
|
1829
|
+
tokenResponse = await this.passwordAuth.signInWithPopup({ view: "change", sessionToken });
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
if (error instanceof MitraApiError && error.code === PASSWORD_CHANGED_SIGN_IN_AGAIN && generation === this.sessionGeneration) {
|
|
1832
|
+
this.clearAuthState();
|
|
1833
|
+
}
|
|
1834
|
+
throw error;
|
|
1835
|
+
}
|
|
1836
|
+
return this.establishSession(tokenResponse);
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Asks IAM to email a link to set a new password. Only the address goes.
|
|
1840
|
+
* Where the link opens depends on the app's mode of the password login: with
|
|
1841
|
+
* the Mitra auth page, the default, the new password is typed there; with the
|
|
1842
|
+
* form in the app itself, the link opens `/reset-password` on this app's
|
|
1843
|
+
* origin, which reads it with {@link readPasswordResetToken} and finishes
|
|
1844
|
+
* with {@link confirmPasswordReset}.
|
|
1845
|
+
*
|
|
1846
|
+
* IAM answers the same whether or not the address has an account, so this
|
|
1847
|
+
* resolves without saying which.
|
|
1848
|
+
*
|
|
1849
|
+
* @param email - The address of the account.
|
|
1850
|
+
* @param options - The language of the message.
|
|
1851
|
+
* @returns How long the application should wait before offering to resend.
|
|
1852
|
+
* @throws {MitraApiError} `RATE_LIMITED` when this address asked too often,
|
|
1853
|
+
* and `RATE_LIMIT_EXCEEDED` when this network did, both with
|
|
1854
|
+
* `retryAfterSeconds` when IAM sends it.
|
|
1855
|
+
*
|
|
1856
|
+
* @example
|
|
1857
|
+
* ```typescript
|
|
1858
|
+
* const { resendAfterSeconds } = await mitra.auth.requestPasswordReset('person@example.com');
|
|
1859
|
+
* ```
|
|
1860
|
+
*/
|
|
1861
|
+
async requestPasswordReset(email, options = {}) {
|
|
1862
|
+
if (globalThis.window === void 0) {
|
|
1863
|
+
throw new Error("Password reset is only available in a browser.");
|
|
1864
|
+
}
|
|
1865
|
+
const browserWindow = globalThis.window;
|
|
1866
|
+
return expectPasswordResetRequestResult(
|
|
1867
|
+
await this.publicClient.post(PASSWORD_RESET_PATH, {
|
|
1868
|
+
email,
|
|
1869
|
+
appId: this.appId,
|
|
1870
|
+
origin: browserWindow.location.origin,
|
|
1871
|
+
language: resolveLanguage(options.language, browserWindow)
|
|
1872
|
+
})
|
|
1873
|
+
);
|
|
1874
|
+
}
|
|
1875
|
+
/**
|
|
1876
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
1877
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
1878
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
1879
|
+
*
|
|
1880
|
+
* Works only when the app has the "form in the app itself" mode of the
|
|
1881
|
+
* password login turned on. Otherwise IAM refuses it with
|
|
1882
|
+
* `PASSWORD_FORM_NOT_ENABLED` (status `403`). Browser only.
|
|
1883
|
+
*
|
|
1884
|
+
* Signs in with the address and password typed into the app's own form.
|
|
1885
|
+
*
|
|
1886
|
+
* @returns The authenticated and hydrated user.
|
|
1887
|
+
* @throws {MitraApiError} `INVALID_CREDENTIALS` (status `401`) for a wrong
|
|
1888
|
+
* address or password, a locked account or one without a password, all
|
|
1889
|
+
* answered alike, `SIGNUP_NOT_ALLOWED` (status `403`) when the account lost
|
|
1890
|
+
* its access to an app that takes no new accounts, `RATE_LIMIT_EXCEEDED`
|
|
1891
|
+
* (status `429`, with `retryAfterSeconds` when IAM sends it) when this
|
|
1892
|
+
* network called the password routes too often or IAM is at its limit of
|
|
1893
|
+
* passwords being hashed, so wait `retryAfterSeconds`, and
|
|
1894
|
+
* `PASSWORD_FORM_NOT_ENABLED` as above.
|
|
1895
|
+
*
|
|
1896
|
+
* @example
|
|
1897
|
+
* ```typescript
|
|
1898
|
+
* const user = await mitra.auth.signInWithPasswordForm({ email, password });
|
|
1899
|
+
* ```
|
|
1900
|
+
*/
|
|
1901
|
+
async signInWithPasswordForm(credentials) {
|
|
1902
|
+
return this.establishSession(await this.passwordForm.signIn(credentials));
|
|
1903
|
+
}
|
|
1904
|
+
/**
|
|
1905
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
1906
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
1907
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
1908
|
+
*
|
|
1909
|
+
* First step of creating a password from the app's own form: IAM emails a
|
|
1910
|
+
* six-digit code to the address, to prove the mailbox is the person's. No
|
|
1911
|
+
* password goes in this step, which IAM takes in either mode of the app; the
|
|
1912
|
+
* "form in the app itself" mode is required at {@link completePasswordSignUp},
|
|
1913
|
+
* where the password goes. Browser only.
|
|
1914
|
+
*
|
|
1915
|
+
* IAM answers the same whether or not the address has an account, so this
|
|
1916
|
+
* resolves without saying which.
|
|
1917
|
+
*
|
|
1918
|
+
* @param request - The address, and the language of the message.
|
|
1919
|
+
* @returns The receipt that names this request, and the resend delay.
|
|
1920
|
+
* @throws {MitraApiError} `RATE_LIMITED` (with `retryAfterSeconds`) when this
|
|
1921
|
+
* address asked too often, and `RATE_LIMIT_EXCEEDED` (status `429`, with
|
|
1922
|
+
* `retryAfterSeconds` when IAM sends it) when this network called the
|
|
1923
|
+
* password routes too often.
|
|
1924
|
+
*
|
|
1925
|
+
* @example
|
|
1926
|
+
* ```typescript
|
|
1927
|
+
* const { receipt, resendAfterSeconds } = await mitra.auth.startPasswordSignUp({ email });
|
|
1928
|
+
* ```
|
|
1929
|
+
*/
|
|
1930
|
+
async startPasswordSignUp(request) {
|
|
1931
|
+
return this.passwordForm.startSignUp(request);
|
|
1932
|
+
}
|
|
1933
|
+
/**
|
|
1934
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
1935
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
1936
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
1937
|
+
*
|
|
1938
|
+
* Works only when the app has the "form in the app itself" mode of the
|
|
1939
|
+
* password login turned on. Otherwise IAM refuses it with
|
|
1940
|
+
* `PASSWORD_FORM_NOT_ENABLED` (status `403`). Browser only.
|
|
1941
|
+
*
|
|
1942
|
+
* Second step of {@link startPasswordSignUp}: the code from the message and
|
|
1943
|
+
* the chosen password. The person ends signed in, as with
|
|
1944
|
+
* {@link signInWithPasswordForm}.
|
|
1945
|
+
*
|
|
1946
|
+
* @param completion - The receipt, the code and the password.
|
|
1947
|
+
* @returns The authenticated and hydrated user.
|
|
1948
|
+
* @throws {MitraApiError} `INVALID_CODE` when the code does not match or the
|
|
1949
|
+
* request expired, `MAGIC_LINK_USED` when it was already completed,
|
|
1950
|
+
* `PASSWORD_ALREADY_SET` when the account has a password (the way to replace
|
|
1951
|
+
* it is the reset), `SSO_ACCOUNT` (status `409`) when the account signs in
|
|
1952
|
+
* with Google or Microsoft, `SIGNUP_NOT_ALLOWED` when the app takes no new
|
|
1953
|
+
* accounts, `WEAK_PASSWORD` (status `400`) with the rule the password broke,
|
|
1954
|
+
* `RATE_LIMIT_EXCEEDED` (status `429`, with `retryAfterSeconds` when IAM
|
|
1955
|
+
* sends it) when this network called the password routes too often or IAM is
|
|
1956
|
+
* at its limit of passwords being hashed, so wait `retryAfterSeconds`, and
|
|
1957
|
+
* `PASSWORD_FORM_NOT_ENABLED` as above.
|
|
1958
|
+
*
|
|
1959
|
+
* @example
|
|
1960
|
+
* ```typescript
|
|
1961
|
+
* const user = await mitra.auth.completePasswordSignUp({ receipt, code, password });
|
|
1962
|
+
* ```
|
|
1963
|
+
*/
|
|
1964
|
+
async completePasswordSignUp(completion) {
|
|
1965
|
+
return this.establishSession(await this.passwordForm.completeSignUp(completion));
|
|
1966
|
+
}
|
|
1967
|
+
/**
|
|
1968
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
1969
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
1970
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
1971
|
+
*
|
|
1972
|
+
* Works only when the app has the "form in the app itself" mode of the
|
|
1973
|
+
* password login turned on. Otherwise IAM refuses it with
|
|
1974
|
+
* `PASSWORD_FORM_NOT_ENABLED` (status `403`). Browser only.
|
|
1975
|
+
*
|
|
1976
|
+
* Changes the password of the signed-in person from the app's own form. IAM
|
|
1977
|
+
* ends every session of the account when the password changes, this one
|
|
1978
|
+
* included, so the SDK signs in again with the address of this session and
|
|
1979
|
+
* the new password, and that session replaces the current one.
|
|
1980
|
+
*
|
|
1981
|
+
* When the change goes through but that new sign-in fails, the call rejects
|
|
1982
|
+
* with `PASSWORD_CHANGED_SIGN_IN_AGAIN` and the local session is cleared,
|
|
1983
|
+
* since IAM already ended it: send the person to sign in with the new password.
|
|
1984
|
+
*
|
|
1985
|
+
* @param change - The current password, required whenever the account
|
|
1986
|
+
* already has one, and the new one.
|
|
1987
|
+
* @returns The user of the new session.
|
|
1988
|
+
* @throws {MitraApiError} `401` when nobody is signed in,
|
|
1989
|
+
* `PASSWORD_CHANGED_SIGN_IN_AGAIN` (status `401`) as above,
|
|
1990
|
+
* `INVALID_CREDENTIALS` (status `401`) for a wrong current password,
|
|
1991
|
+
* `CURRENT_PASSWORD_REQUIRED` and `WEAK_PASSWORD` (status `400`),
|
|
1992
|
+
* `SSO_ACCOUNT` (status `409`) when the account signs in with Google or
|
|
1993
|
+
* Microsoft, `RATE_LIMIT_EXCEEDED` (status `429`, with `retryAfterSeconds`
|
|
1994
|
+
* when IAM sends it) when this network called the password routes too often
|
|
1995
|
+
* or IAM is at its limit of passwords being hashed, so wait
|
|
1996
|
+
* `retryAfterSeconds`, and `PASSWORD_FORM_NOT_ENABLED` as above.
|
|
1997
|
+
*
|
|
1998
|
+
* @example
|
|
1999
|
+
* ```typescript
|
|
2000
|
+
* await mitra.auth.changePasswordForm({ currentPassword, newPassword });
|
|
2001
|
+
* ```
|
|
2002
|
+
*/
|
|
2003
|
+
async changePasswordForm(change) {
|
|
2004
|
+
if (!this.#accessToken) {
|
|
2005
|
+
throw new MitraApiError("Sign in before changing the password.", 401);
|
|
2006
|
+
}
|
|
2007
|
+
const email = this._currentUser?.email ?? (await this.getCurrentUser()).email;
|
|
2008
|
+
await this.ensureFreshSession();
|
|
2009
|
+
const accessToken = this.#accessToken;
|
|
2010
|
+
if (!accessToken) {
|
|
2011
|
+
throw new MitraApiError("Sign in before changing the password.", 401);
|
|
2012
|
+
}
|
|
2013
|
+
const generation = this.sessionGeneration;
|
|
2014
|
+
await this.passwordForm.changePassword(change, accessToken);
|
|
2015
|
+
try {
|
|
2016
|
+
return await this.signInWithPasswordForm({ email, password: change.newPassword });
|
|
2017
|
+
} catch (error) {
|
|
2018
|
+
if (generation === this.sessionGeneration) {
|
|
2019
|
+
this.clearAuthState();
|
|
2020
|
+
}
|
|
2021
|
+
throw passwordChangedSignInAgain(error);
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
/**
|
|
2025
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
2026
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
2027
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
2028
|
+
*
|
|
2029
|
+
* The reset token is single use and works as a credential until it is spent:
|
|
2030
|
+
* do not log it either.
|
|
2031
|
+
*
|
|
2032
|
+
* Reads the token of a password reset link that landed on this app, and
|
|
2033
|
+
* removes the fragment from the URL so the token does not stay in the
|
|
2034
|
+
* history. With the form in the app itself turned on, IAM sends the link to
|
|
2035
|
+
* `/reset-password#resetToken=...` on the origin the reset was asked from:
|
|
2036
|
+
* call this when that route loads, then {@link confirmPasswordReset}.
|
|
2037
|
+
*
|
|
2038
|
+
* Nothing is sent to IAM here, and a URL without the link is left as it is.
|
|
2039
|
+
* Browser only.
|
|
2040
|
+
*
|
|
2041
|
+
* @returns The token, or `null` when the URL carries none.
|
|
2042
|
+
*
|
|
2043
|
+
* @example
|
|
2044
|
+
* ```typescript
|
|
2045
|
+
* const token = mitra.auth.readPasswordResetToken();
|
|
2046
|
+
* ```
|
|
2047
|
+
*/
|
|
2048
|
+
readPasswordResetToken() {
|
|
2049
|
+
return this.passwordForm.readResetToken();
|
|
2050
|
+
}
|
|
2051
|
+
/**
|
|
2052
|
+
* The password goes through the app's code. Do not store it, do not log it
|
|
2053
|
+
* and do not send it anywhere other than this method. The safer default is
|
|
2054
|
+
* signInWithPassword(), which uses the Mitra auth page.
|
|
2055
|
+
*
|
|
2056
|
+
* Works only when the app has the "form in the app itself" mode of the
|
|
2057
|
+
* password login turned on. Otherwise IAM refuses it with
|
|
2058
|
+
* `PASSWORD_FORM_NOT_ENABLED` (status `403`). Browser only.
|
|
2059
|
+
*
|
|
2060
|
+
* Sets the new password with the token from the reset link. The link is
|
|
2061
|
+
* single use, and IAM ends every session of the account, so the person signs
|
|
2062
|
+
* in afterwards with the new password; this method signs nobody in.
|
|
2063
|
+
*
|
|
2064
|
+
* @param confirmation - The token from {@link readPasswordResetToken} and the
|
|
2065
|
+
* new password.
|
|
2066
|
+
* @throws {MitraApiError} `RESET_TOKEN_USED` or `RESET_TOKEN_EXPIRED` (status
|
|
2067
|
+
* `409`) when the link can no longer be used; a token IAM does not know also
|
|
2068
|
+
* answers `RESET_TOKEN_EXPIRED`. Also `WEAK_PASSWORD` (status `400`),
|
|
2069
|
+
* `SSO_ACCOUNT` (status `409`) when the account signs in with Google or
|
|
2070
|
+
* Microsoft, `RATE_LIMIT_EXCEEDED` (status `429`, with `retryAfterSeconds`
|
|
2071
|
+
* when IAM sends it) when this network called the password routes too often
|
|
2072
|
+
* or IAM is at its limit of passwords being hashed, so wait
|
|
2073
|
+
* `retryAfterSeconds`, and `PASSWORD_FORM_NOT_ENABLED` as above.
|
|
2074
|
+
*
|
|
2075
|
+
* @example
|
|
2076
|
+
* ```typescript
|
|
2077
|
+
* await mitra.auth.confirmPasswordReset({ token, newPassword });
|
|
2078
|
+
* ```
|
|
2079
|
+
*/
|
|
2080
|
+
async confirmPasswordReset(confirmation) {
|
|
2081
|
+
return this.passwordForm.confirmReset(confirmation);
|
|
2082
|
+
}
|
|
1519
2083
|
/**
|
|
1520
2084
|
* Signs out the current user, clearing all auth state and localStorage.
|
|
1521
2085
|
*
|
|
2086
|
+
* In an app whose accounts are its own, the session also ends on the server:
|
|
2087
|
+
* the refresh token is sent to IAM's logout, so it can no longer be renewed.
|
|
2088
|
+
* The local state is cleared right away whatever IAM answers, and the request
|
|
2089
|
+
* survives a navigation to `redirectUrl`. The access token already issued
|
|
2090
|
+
* stays valid until it expires.
|
|
2091
|
+
*
|
|
1522
2092
|
* @param redirectUrl - Optional URL to navigate to after sign-out.
|
|
1523
2093
|
*
|
|
1524
2094
|
* @example
|
|
@@ -1528,7 +2098,11 @@ var AuthModule = class {
|
|
|
1528
2098
|
* ```
|
|
1529
2099
|
*/
|
|
1530
2100
|
signOut(redirectUrl) {
|
|
2101
|
+
const refreshToken = this.#refreshToken;
|
|
1531
2102
|
this.clearAuthState();
|
|
2103
|
+
if (refreshToken && isExclusiveAppSession(refreshToken)) {
|
|
2104
|
+
this.endServerSession(refreshToken);
|
|
2105
|
+
}
|
|
1532
2106
|
if (globalThis.window !== void 0 && redirectUrl) {
|
|
1533
2107
|
globalThis.window.location.href = redirectUrl;
|
|
1534
2108
|
}
|
|
@@ -1857,6 +2431,10 @@ var AuthModule = class {
|
|
|
1857
2431
|
this.listeners.delete(callback);
|
|
1858
2432
|
};
|
|
1859
2433
|
}
|
|
2434
|
+
/** Best effort: the person asked to leave, and that already happened locally. */
|
|
2435
|
+
endServerSession(refreshToken) {
|
|
2436
|
+
this.publicClient.request(LOGOUT_PATH, { method: "POST", body: { refreshToken }, keepalive: true }).catch(() => void 0);
|
|
2437
|
+
}
|
|
1860
2438
|
async doRefresh(generation, refreshToken) {
|
|
1861
2439
|
let tokenResponse;
|
|
1862
2440
|
try {
|
|
@@ -2723,6 +3301,7 @@ function expectAppInfoResponse(value) {
|
|
|
2723
3301
|
// answers without this field, and an app that cannot prove it is enabled
|
|
2724
3302
|
// should stop offering the option, not stop starting.
|
|
2725
3303
|
emailLoginEnabled: response.emailLoginEnabled === true,
|
|
3304
|
+
passwordLoginEnabled: response.passwordLoginEnabled === true,
|
|
2726
3305
|
// Same reasoning, and the brand is never guessed: without it the headless
|
|
2727
3306
|
// email methods refuse instead of asking IAM to brand a message at random.
|
|
2728
3307
|
brand: typeof response.brand === "string" && response.brand.trim() ? response.brand : null
|
|
@@ -2801,6 +3380,7 @@ function createClient(config) {
|
|
|
2801
3380
|
let initialized = false;
|
|
2802
3381
|
let allowSignup = true;
|
|
2803
3382
|
let emailLoginEnabled = false;
|
|
3383
|
+
let passwordLoginEnabled = false;
|
|
2804
3384
|
async function init() {
|
|
2805
3385
|
if (initialized) return;
|
|
2806
3386
|
const publicClient = new HttpClient({
|
|
@@ -2820,6 +3400,7 @@ function createClient(config) {
|
|
|
2820
3400
|
}
|
|
2821
3401
|
allowSignup = appInfo.allowSignup;
|
|
2822
3402
|
emailLoginEnabled = appInfo.emailLoginEnabled;
|
|
3403
|
+
passwordLoginEnabled = appInfo.passwordLoginEnabled;
|
|
2823
3404
|
initialized = true;
|
|
2824
3405
|
}
|
|
2825
3406
|
return {
|
|
@@ -2838,6 +3419,9 @@ function createClient(config) {
|
|
|
2838
3419
|
get emailLoginEnabled() {
|
|
2839
3420
|
return emailLoginEnabled;
|
|
2840
3421
|
},
|
|
3422
|
+
get passwordLoginEnabled() {
|
|
3423
|
+
return passwordLoginEnabled;
|
|
3424
|
+
},
|
|
2841
3425
|
config
|
|
2842
3426
|
};
|
|
2843
3427
|
}
|