@sparkvault/sdk-js 2.0.3 → 2.0.4
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/identity/api.d.ts +11 -1
- package/dist/identity/handlers/totp-handler.d.ts +8 -0
- package/dist/identity/renderer.d.ts +1 -0
- package/dist/identity/types.d.ts +26 -0
- package/dist/identity/views/index.d.ts +1 -0
- package/dist/identity/views/two-factor.d.ts +37 -0
- package/dist/sparkvault.cjs.js +200 -0
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +200 -0
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/package.json +1 -1
package/dist/sparkvault.esm.js
CHANGED
|
@@ -1064,6 +1064,18 @@ class IdentityApi extends SparkVaultApi {
|
|
|
1064
1064
|
...this.buildAuthContextParams(params.authContext),
|
|
1065
1065
|
});
|
|
1066
1066
|
}
|
|
1067
|
+
/**
|
|
1068
|
+
* Submit a second factor (authenticator-app code or recovery code) to finish
|
|
1069
|
+
* a login that the primary factor left in a `second_factor_required` state.
|
|
1070
|
+
* The ticket is the opaque pending-login handle returned by the primary
|
|
1071
|
+
* verify call.
|
|
1072
|
+
*/
|
|
1073
|
+
async submitSecondFactor(params) {
|
|
1074
|
+
return this.request('POST', '/second-factor/verify', {
|
|
1075
|
+
ticket: params.ticket,
|
|
1076
|
+
code: params.code,
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1067
1079
|
/**
|
|
1068
1080
|
* Start passkey registration from a freshly verified identifier.
|
|
1069
1081
|
*/
|
|
@@ -1808,6 +1820,14 @@ class TotpHandler {
|
|
|
1808
1820
|
recipient: this.state.recipient,
|
|
1809
1821
|
authContext: this.state.authContext,
|
|
1810
1822
|
});
|
|
1823
|
+
// Primary factor accepted, but the account requires a second factor.
|
|
1824
|
+
// Not a success and not a retry — hand the ticket up to drive the 2FA step.
|
|
1825
|
+
if (response.second_factor_required && response.ticket) {
|
|
1826
|
+
return {
|
|
1827
|
+
success: false,
|
|
1828
|
+
secondFactor: { ticket: response.ticket, methods: response.methods ?? ['authenticator'] },
|
|
1829
|
+
};
|
|
1830
|
+
}
|
|
1811
1831
|
if (response.verified && (response.token || response.redirect)) {
|
|
1812
1832
|
return {
|
|
1813
1833
|
success: true,
|
|
@@ -4556,6 +4576,144 @@ class TotpVerifyView {
|
|
|
4556
4576
|
}
|
|
4557
4577
|
}
|
|
4558
4578
|
|
|
4579
|
+
/**
|
|
4580
|
+
* Two-Factor View
|
|
4581
|
+
*
|
|
4582
|
+
* Shown after a primary factor succeeds for an account with 2FA enabled.
|
|
4583
|
+
* Collects a 6-digit authenticator-app code, or (if offered) a recovery code,
|
|
4584
|
+
* and submits it to complete the login.
|
|
4585
|
+
*/
|
|
4586
|
+
class TwoFactorView {
|
|
4587
|
+
constructor(props) {
|
|
4588
|
+
this.input = null;
|
|
4589
|
+
this.submitButton = null;
|
|
4590
|
+
this.toggleButton = null;
|
|
4591
|
+
this.errorContainer = null;
|
|
4592
|
+
this.promptEl = null;
|
|
4593
|
+
this.recoveryMode = false;
|
|
4594
|
+
this.isSubmitting = false;
|
|
4595
|
+
this.focusTimeoutId = null;
|
|
4596
|
+
this.props = props;
|
|
4597
|
+
this.boundSubmit = () => this.handleSubmit();
|
|
4598
|
+
this.boundToggle = () => this.toggleRecovery();
|
|
4599
|
+
this.boundKeydown = (e) => { if (e.key === 'Enter')
|
|
4600
|
+
this.handleSubmit(); };
|
|
4601
|
+
this.boundInput = () => this.clearError();
|
|
4602
|
+
}
|
|
4603
|
+
render() {
|
|
4604
|
+
const container = div();
|
|
4605
|
+
container.style.display = 'flex';
|
|
4606
|
+
container.style.flexDirection = 'column';
|
|
4607
|
+
const title = document.createElement('h3');
|
|
4608
|
+
title.className = 'sv-title';
|
|
4609
|
+
title.textContent = 'Two-step verification';
|
|
4610
|
+
this.promptEl = document.createElement('p');
|
|
4611
|
+
this.promptEl.className = 'sv-subtitle';
|
|
4612
|
+
this.promptEl.innerHTML = 'Enter the 6-digit code from your authenticator app.';
|
|
4613
|
+
this.errorContainer = div();
|
|
4614
|
+
if (this.props.error) {
|
|
4615
|
+
this.errorContainer.appendChild(errorMessage(this.props.error));
|
|
4616
|
+
}
|
|
4617
|
+
this.input = document.createElement('input');
|
|
4618
|
+
this.input.type = 'text';
|
|
4619
|
+
this.input.inputMode = 'numeric';
|
|
4620
|
+
this.input.autocomplete = 'one-time-code';
|
|
4621
|
+
this.input.maxLength = 6;
|
|
4622
|
+
this.input.className = 'sv-input';
|
|
4623
|
+
this.input.setAttribute('aria-label', 'Verification code');
|
|
4624
|
+
this.input.placeholder = '000000';
|
|
4625
|
+
this.input.addEventListener('keydown', this.boundKeydown);
|
|
4626
|
+
this.input.addEventListener('input', this.boundInput);
|
|
4627
|
+
this.submitButton = document.createElement('button');
|
|
4628
|
+
this.submitButton.className = 'sv-btn sv-btn-primary';
|
|
4629
|
+
this.submitButton.textContent = 'Verify';
|
|
4630
|
+
this.submitButton.addEventListener('click', this.boundSubmit);
|
|
4631
|
+
container.appendChild(title);
|
|
4632
|
+
container.appendChild(this.promptEl);
|
|
4633
|
+
container.appendChild(this.errorContainer);
|
|
4634
|
+
container.appendChild(this.input);
|
|
4635
|
+
container.appendChild(this.submitButton);
|
|
4636
|
+
if (this.props.methods?.includes('recovery_code')) {
|
|
4637
|
+
this.toggleButton = document.createElement('button');
|
|
4638
|
+
this.toggleButton.className = 'sv-back-link';
|
|
4639
|
+
this.toggleButton.textContent = 'Use a recovery code instead';
|
|
4640
|
+
this.toggleButton.addEventListener('click', this.boundToggle);
|
|
4641
|
+
container.appendChild(this.toggleButton);
|
|
4642
|
+
}
|
|
4643
|
+
this.focusTimeoutId = setTimeout(() => this.input?.focus(), 50);
|
|
4644
|
+
return container;
|
|
4645
|
+
}
|
|
4646
|
+
toggleRecovery() {
|
|
4647
|
+
if (!this.input || !this.promptEl || !this.toggleButton)
|
|
4648
|
+
return;
|
|
4649
|
+
this.recoveryMode = !this.recoveryMode;
|
|
4650
|
+
this.input.value = '';
|
|
4651
|
+
this.clearError();
|
|
4652
|
+
if (this.recoveryMode) {
|
|
4653
|
+
this.input.inputMode = 'text';
|
|
4654
|
+
this.input.removeAttribute('maxlength');
|
|
4655
|
+
this.input.placeholder = 'XXXX-XXXX-XXXX-XXXX';
|
|
4656
|
+
this.promptEl.innerHTML = 'Enter one of your recovery codes.';
|
|
4657
|
+
this.toggleButton.textContent = 'Use your authenticator app instead';
|
|
4658
|
+
}
|
|
4659
|
+
else {
|
|
4660
|
+
this.input.inputMode = 'numeric';
|
|
4661
|
+
this.input.maxLength = 6;
|
|
4662
|
+
this.input.placeholder = '000000';
|
|
4663
|
+
this.promptEl.innerHTML = 'Enter the 6-digit code from your authenticator app.';
|
|
4664
|
+
this.toggleButton.textContent = 'Use a recovery code instead';
|
|
4665
|
+
}
|
|
4666
|
+
this.input.focus();
|
|
4667
|
+
}
|
|
4668
|
+
clearError() {
|
|
4669
|
+
if (this.errorContainer)
|
|
4670
|
+
clearChildren(this.errorContainer);
|
|
4671
|
+
this.input?.classList.remove('sv-input-error');
|
|
4672
|
+
}
|
|
4673
|
+
/** Surface a server-side error and re-enable the form for another attempt. */
|
|
4674
|
+
showError(message) {
|
|
4675
|
+
if (this.errorContainer) {
|
|
4676
|
+
clearChildren(this.errorContainer);
|
|
4677
|
+
this.errorContainer.appendChild(errorMessage(message));
|
|
4678
|
+
}
|
|
4679
|
+
this.input?.classList.add('sv-input-error');
|
|
4680
|
+
this.isSubmitting = false;
|
|
4681
|
+
if (this.submitButton) {
|
|
4682
|
+
this.submitButton.disabled = false;
|
|
4683
|
+
this.submitButton.textContent = 'Verify';
|
|
4684
|
+
}
|
|
4685
|
+
this.input?.focus();
|
|
4686
|
+
this.input?.select();
|
|
4687
|
+
}
|
|
4688
|
+
handleSubmit() {
|
|
4689
|
+
if (this.isSubmitting || !this.submitButton || !this.input)
|
|
4690
|
+
return;
|
|
4691
|
+
const code = this.input.value.trim();
|
|
4692
|
+
if (!code) {
|
|
4693
|
+
this.showError(this.recoveryMode ? 'Enter a recovery code' : 'Enter the 6-digit code');
|
|
4694
|
+
return;
|
|
4695
|
+
}
|
|
4696
|
+
if (!this.recoveryMode && !/^\d{6}$/.test(code)) {
|
|
4697
|
+
this.showError('Enter the complete 6-digit code');
|
|
4698
|
+
return;
|
|
4699
|
+
}
|
|
4700
|
+
this.isSubmitting = true;
|
|
4701
|
+
this.submitButton.disabled = true;
|
|
4702
|
+
this.submitButton.textContent = 'Verifying...';
|
|
4703
|
+
this.props.onSubmit(code);
|
|
4704
|
+
}
|
|
4705
|
+
destroy() {
|
|
4706
|
+
if (this.focusTimeoutId !== null) {
|
|
4707
|
+
clearTimeout(this.focusTimeoutId);
|
|
4708
|
+
this.focusTimeoutId = null;
|
|
4709
|
+
}
|
|
4710
|
+
this.input?.removeEventListener('keydown', this.boundKeydown);
|
|
4711
|
+
this.input?.removeEventListener('input', this.boundInput);
|
|
4712
|
+
this.submitButton?.removeEventListener('click', this.boundSubmit);
|
|
4713
|
+
this.toggleButton?.removeEventListener('click', this.boundToggle);
|
|
4714
|
+
}
|
|
4715
|
+
}
|
|
4716
|
+
|
|
4559
4717
|
/**
|
|
4560
4718
|
* Passkey View
|
|
4561
4719
|
*
|
|
@@ -5724,6 +5882,12 @@ class IdentityRenderer {
|
|
|
5724
5882
|
onResend: () => this.handleTotpResend(),
|
|
5725
5883
|
onBack: () => this.showMethodSelect(),
|
|
5726
5884
|
});
|
|
5885
|
+
case 'two-factor':
|
|
5886
|
+
return new TwoFactorView({
|
|
5887
|
+
methods: state.methods,
|
|
5888
|
+
error: state.error,
|
|
5889
|
+
onSubmit: (code) => this.handleSecondFactorSubmit(code),
|
|
5890
|
+
});
|
|
5727
5891
|
case 'passkey': {
|
|
5728
5892
|
// If coming from passkey prompt with a pending result, "back" should skip registration
|
|
5729
5893
|
const { passkey } = this.verificationState;
|
|
@@ -5981,6 +6145,14 @@ class IdentityRenderer {
|
|
|
5981
6145
|
return;
|
|
5982
6146
|
const currentMethod = this.verificationState.totp.method ?? 'email';
|
|
5983
6147
|
const result = await this.totpHandler.verify(code);
|
|
6148
|
+
if (result.secondFactor) {
|
|
6149
|
+
this.setState({
|
|
6150
|
+
view: 'two-factor',
|
|
6151
|
+
ticket: result.secondFactor.ticket,
|
|
6152
|
+
methods: result.secondFactor.methods,
|
|
6153
|
+
});
|
|
6154
|
+
return;
|
|
6155
|
+
}
|
|
5984
6156
|
if (result.success && result.result) {
|
|
5985
6157
|
// Passkey upsell only when there's no redirect target — with one,
|
|
5986
6158
|
// we head to the destination immediately.
|
|
@@ -6046,6 +6218,34 @@ class IdentityRenderer {
|
|
|
6046
6218
|
});
|
|
6047
6219
|
}
|
|
6048
6220
|
}
|
|
6221
|
+
async handleSecondFactorSubmit(code) {
|
|
6222
|
+
if (this.viewState.view !== 'two-factor')
|
|
6223
|
+
return;
|
|
6224
|
+
const { ticket, methods } = this.viewState;
|
|
6225
|
+
try {
|
|
6226
|
+
const response = await this.api.submitSecondFactor({ ticket, code });
|
|
6227
|
+
const result = {
|
|
6228
|
+
token: response.token ?? '',
|
|
6229
|
+
identity: response.identity ?? this.recipient,
|
|
6230
|
+
identityType: response.identity_type ?? this.identityType,
|
|
6231
|
+
svid: response.svid,
|
|
6232
|
+
sessionId: response.session_id,
|
|
6233
|
+
refreshToken: response.refresh_token,
|
|
6234
|
+
redirect: response.redirect,
|
|
6235
|
+
};
|
|
6236
|
+
this.close();
|
|
6237
|
+
this.safeOnSuccess(result);
|
|
6238
|
+
if (typeof result.redirect === 'string' && result.redirect) {
|
|
6239
|
+
window.location.href = result.redirect;
|
|
6240
|
+
}
|
|
6241
|
+
}
|
|
6242
|
+
catch (error) {
|
|
6243
|
+
const message = error instanceof Error ? error.message : 'That code is not valid. Please try again.';
|
|
6244
|
+
// The login ticket is not consumed on a wrong code, so the user can retry
|
|
6245
|
+
// against the same ticket — re-render the view carrying the error.
|
|
6246
|
+
this.setState({ view: 'two-factor', ticket, methods, error: message });
|
|
6247
|
+
}
|
|
6248
|
+
}
|
|
6049
6249
|
async handleTotpResend() {
|
|
6050
6250
|
if (this.viewState.view !== 'totp-verify')
|
|
6051
6251
|
return;
|