@jskit-ai/auth-web 0.2.33 → 0.2.35
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 +2 -0
- package/fixtures/sign-in/main.js +27 -1
- package/fixtures/sign-in/vite.config.mjs +6 -1
- package/locales/en.json +51 -0
- package/package.json +42 -6
- package/src/client/browser.js +6 -0
- package/src/client/composables/secondFactor/useSecondFactorProof.js +79 -0
- package/src/client/composables/secondFactor/useSecondFactorSettings.js +122 -0
- package/src/client/composables/secondFactor/useSecondFactorStepUp.js +51 -0
- package/src/client/index.js +1 -0
- package/src/client/lib/qrCode.js +32 -0
- package/src/client/runtime/secondFactorRequests.js +56 -0
- package/src/client/types.js +32 -0
- package/src/client/views/SecondFactorDialog.vue +37 -0
- package/src/client/views/SecondFactorPrompt.vue +27 -0
- package/src/client/views/SecondFactorProofForm.vue +78 -0
- package/src/client/views/SecondFactorSettings.vue +212 -0
- package/src/server/AuthWebFeature.js +2 -0
- package/src/server/authRateLimits.js +9 -4
- package/src/server/authWeb.js +7 -5
- package/src/server/routes/authRoutes.js +1 -1
- package/src/server/routes/secondFactorRoutes.js +134 -0
- package/src/server/types.js +6 -1
- package/test/sameSiteStrict.browser.test.js +220 -0
- package/test/secondFactor.browser.test.js +219 -0
- package/test/secondFactorClient.test.js +140 -0
- package/test/secondFactorRoutes.test.js +152 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<section class="second-factor-prompt" :aria-labelledby="titleId">
|
|
3
|
+
<h2 :id="titleId" class="text-title-large mb-2">{{ t("jskit.auth.secondFactor.prompt.title") }}</h2>
|
|
4
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.prompt.explain") }}</p>
|
|
5
|
+
<SecondFactorProofForm
|
|
6
|
+
:submit="verifySecondFactor"
|
|
7
|
+
:submit-label="t('jskit.auth.secondFactor.confirm')"
|
|
8
|
+
@done="(result) => emit('verified', result)"
|
|
9
|
+
>
|
|
10
|
+
<template #actions>
|
|
11
|
+
<slot name="actions" />
|
|
12
|
+
</template>
|
|
13
|
+
</SecondFactorProofForm>
|
|
14
|
+
</section>
|
|
15
|
+
</template>
|
|
16
|
+
|
|
17
|
+
<script setup>
|
|
18
|
+
import { useId } from "vue";
|
|
19
|
+
import SecondFactorProofForm from "./SecondFactorProofForm.vue";
|
|
20
|
+
import { useJskitText } from "../i18n.js";
|
|
21
|
+
import { verifySecondFactor } from "../runtime/secondFactorRequests.js";
|
|
22
|
+
|
|
23
|
+
// Proves this session's second factor with a code from the authenticator app or a recovery code.
|
|
24
|
+
const emit = defineEmits(["verified"]);
|
|
25
|
+
const { t } = useJskitText();
|
|
26
|
+
const titleId = useId();
|
|
27
|
+
</script>
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<v-form novalidate @submit.prevent="submitProof">
|
|
3
|
+
<v-text-field
|
|
4
|
+
v-if="usingRecoveryCode"
|
|
5
|
+
key="recovery-code"
|
|
6
|
+
v-model="recoveryCode"
|
|
7
|
+
:label="t('jskit.auth.secondFactor.form.recoveryCode')"
|
|
8
|
+
variant="outlined"
|
|
9
|
+
density="comfortable"
|
|
10
|
+
autocomplete="off"
|
|
11
|
+
autocapitalize="off"
|
|
12
|
+
spellcheck="false"
|
|
13
|
+
autofocus
|
|
14
|
+
:error-messages="fieldError"
|
|
15
|
+
/>
|
|
16
|
+
<v-text-field
|
|
17
|
+
v-else
|
|
18
|
+
key="code"
|
|
19
|
+
v-model="code"
|
|
20
|
+
:label="t('jskit.auth.secondFactor.form.code')"
|
|
21
|
+
variant="outlined"
|
|
22
|
+
density="comfortable"
|
|
23
|
+
inputmode="numeric"
|
|
24
|
+
autocomplete="one-time-code"
|
|
25
|
+
autofocus
|
|
26
|
+
:error-messages="fieldError"
|
|
27
|
+
/>
|
|
28
|
+
<v-btn v-if="allowRecoveryCode" variant="text" class="mb-2" @click="toggleRecoveryCode">
|
|
29
|
+
{{ usingRecoveryCode ? t("jskit.auth.secondFactor.form.useCode") : t("jskit.auth.secondFactor.form.useRecoveryCode") }}
|
|
30
|
+
</v-btn>
|
|
31
|
+
<div class="second-factor-proof__actions">
|
|
32
|
+
<slot name="actions" />
|
|
33
|
+
<v-btn type="submit" variant="flat" :color="submitColor" :disabled="!canSubmit">
|
|
34
|
+
{{ submitLabel }}
|
|
35
|
+
</v-btn>
|
|
36
|
+
</div>
|
|
37
|
+
</v-form>
|
|
38
|
+
</template>
|
|
39
|
+
|
|
40
|
+
<script setup>
|
|
41
|
+
import { useSecondFactorProof } from "../composables/secondFactor/useSecondFactorProof.js";
|
|
42
|
+
import { useJskitText } from "../i18n.js";
|
|
43
|
+
|
|
44
|
+
const props = defineProps({
|
|
45
|
+
submit: {
|
|
46
|
+
type: /** @type {import("vue").PropType<(proof: import("../types.js").SecondFactorProof) => Promise<unknown>>} */ (Function),
|
|
47
|
+
required: true
|
|
48
|
+
},
|
|
49
|
+
submitLabel: { type: String, required: true },
|
|
50
|
+
submitColor: { type: String, default: "primary" },
|
|
51
|
+
// Setting up an authenticator takes its first code only; every other proof may be a recovery code.
|
|
52
|
+
allowRecoveryCode: { type: Boolean, default: true }
|
|
53
|
+
});
|
|
54
|
+
const emit = defineEmits(["done"]);
|
|
55
|
+
|
|
56
|
+
const { t } = useJskitText();
|
|
57
|
+
const {
|
|
58
|
+
usingRecoveryCode,
|
|
59
|
+
code,
|
|
60
|
+
recoveryCode,
|
|
61
|
+
fieldError,
|
|
62
|
+
canSubmit,
|
|
63
|
+
toggleRecoveryCode,
|
|
64
|
+
submitProof
|
|
65
|
+
} = useSecondFactorProof({
|
|
66
|
+
submit: (proof) => props.submit(proof),
|
|
67
|
+
onDone: (result) => emit("done", result)
|
|
68
|
+
});
|
|
69
|
+
</script>
|
|
70
|
+
|
|
71
|
+
<style scoped>
|
|
72
|
+
.second-factor-proof__actions {
|
|
73
|
+
display: flex;
|
|
74
|
+
flex-wrap: wrap;
|
|
75
|
+
justify-content: flex-end;
|
|
76
|
+
gap: 8px;
|
|
77
|
+
}
|
|
78
|
+
</style>
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="second-factor-settings">
|
|
3
|
+
<div v-if="step === 'codes'">
|
|
4
|
+
<h2 ref="stepStart" tabindex="-1" class="text-title-medium mb-2">{{ t("jskit.auth.secondFactor.codes.title") }}</h2>
|
|
5
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.codes.explain") }}</p>
|
|
6
|
+
<ul class="second-factor-settings__codes text-body-large mb-4" :aria-label="t('jskit.auth.secondFactor.codes.listLabel')">
|
|
7
|
+
<li v-for="recoveryCode in recoveryCodes" :key="recoveryCode">{{ recoveryCode }}</li>
|
|
8
|
+
</ul>
|
|
9
|
+
<div class="second-factor-settings__actions">
|
|
10
|
+
<v-btn v-if="canCopy" variant="text" prepend-icon="$contentCopy" @click="copy(recoveryCodes.join('\n'))">
|
|
11
|
+
{{ t("jskit.auth.secondFactor.codes.copy") }}
|
|
12
|
+
</v-btn>
|
|
13
|
+
<v-btn color="primary" variant="flat" @click="onCodesSaved">{{ t("jskit.auth.secondFactor.codes.saved") }}</v-btn>
|
|
14
|
+
</div>
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
<v-skeleton-loader v-else-if="load === 'loading'" type="list-item-two-line, actions" />
|
|
18
|
+
|
|
19
|
+
<div v-else-if="load === 'error'">
|
|
20
|
+
<v-icon icon="$error" color="error" class="mb-2" />
|
|
21
|
+
<p class="text-body-medium mb-4">{{ t("jskit.auth.secondFactor.settings.loadFailed") }}</p>
|
|
22
|
+
<v-btn color="primary" variant="tonal" prepend-icon="$refresh" @click="reload">
|
|
23
|
+
{{ t("jskit.auth.secondFactor.settings.retry") }}
|
|
24
|
+
</v-btn>
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<div v-else-if="step === 'setup'">
|
|
28
|
+
<h2 class="text-title-medium mb-2">{{ t("jskit.auth.secondFactor.setup.title") }}</h2>
|
|
29
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.setup.scan") }}</p>
|
|
30
|
+
<!-- A QR code is content, not chrome: dark modules on white in both schemes, as scanners read it. -->
|
|
31
|
+
<svg
|
|
32
|
+
class="second-factor-settings__qr mb-4"
|
|
33
|
+
role="img"
|
|
34
|
+
:aria-label="t('jskit.auth.secondFactor.setup.qrLabel', { account: setup.accountName })"
|
|
35
|
+
:viewBox="`0 0 ${qrCode.size} ${qrCode.size}`"
|
|
36
|
+
shape-rendering="crispEdges"
|
|
37
|
+
>
|
|
38
|
+
<rect :width="qrCode.size" :height="qrCode.size" fill="white" />
|
|
39
|
+
<path :d="qrCode.path" fill="black" />
|
|
40
|
+
</svg>
|
|
41
|
+
<p class="text-body-medium text-on-surface-variant mb-1">{{ t("jskit.auth.secondFactor.setup.typeKey") }}</p>
|
|
42
|
+
<div class="second-factor-settings__secret-row mb-4">
|
|
43
|
+
<code class="second-factor-settings__secret text-body-large">{{ secretText }}</code>
|
|
44
|
+
<v-btn v-if="canCopy" variant="text" prepend-icon="$contentCopy" @click="copy(setup.secret)">
|
|
45
|
+
{{ t("jskit.auth.secondFactor.setup.copyKey") }}
|
|
46
|
+
</v-btn>
|
|
47
|
+
</div>
|
|
48
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.setup.enterCode") }}</p>
|
|
49
|
+
<SecondFactorProofForm
|
|
50
|
+
:submit="confirmSecondFactorSetup"
|
|
51
|
+
:submit-label="t('jskit.auth.secondFactor.confirm')"
|
|
52
|
+
:allow-recovery-code="false"
|
|
53
|
+
@done="onRecoveryCodes"
|
|
54
|
+
>
|
|
55
|
+
<template #actions>
|
|
56
|
+
<v-btn variant="text" @click="backToStatus">{{ t("jskit.auth.secondFactor.cancel") }}</v-btn>
|
|
57
|
+
</template>
|
|
58
|
+
</SecondFactorProofForm>
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<div v-else-if="step === 'replaceCodes'">
|
|
62
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.replace.explain") }}</p>
|
|
63
|
+
<SecondFactorProofForm
|
|
64
|
+
:submit="replaceSecondFactorRecoveryCodes"
|
|
65
|
+
:submit-label="t('jskit.auth.secondFactor.replace.submit')"
|
|
66
|
+
@done="onRecoveryCodes"
|
|
67
|
+
>
|
|
68
|
+
<template #actions>
|
|
69
|
+
<v-btn variant="text" @click="backToStatus">{{ t("jskit.auth.secondFactor.cancel") }}</v-btn>
|
|
70
|
+
</template>
|
|
71
|
+
</SecondFactorProofForm>
|
|
72
|
+
</div>
|
|
73
|
+
|
|
74
|
+
<div v-else-if="step === 'remove'">
|
|
75
|
+
<p class="text-body-medium text-on-surface-variant mb-4">{{ t("jskit.auth.secondFactor.remove.explain") }}</p>
|
|
76
|
+
<SecondFactorProofForm
|
|
77
|
+
:submit="removeSecondFactor"
|
|
78
|
+
submit-color="error"
|
|
79
|
+
:submit-label="t('jskit.auth.secondFactor.settings.remove')"
|
|
80
|
+
@done="removed"
|
|
81
|
+
>
|
|
82
|
+
<template #actions>
|
|
83
|
+
<v-btn variant="text" @click="backToStatus">{{ t("jskit.auth.secondFactor.cancel") }}</v-btn>
|
|
84
|
+
</template>
|
|
85
|
+
</SecondFactorProofForm>
|
|
86
|
+
</div>
|
|
87
|
+
|
|
88
|
+
<div v-else-if="status.enrolled">
|
|
89
|
+
<p ref="stepStart" tabindex="-1" class="text-body-large mb-1">
|
|
90
|
+
{{ t("jskit.auth.secondFactor.settings.on", { date: formatDate(status.enrolledAt) }) }}
|
|
91
|
+
</p>
|
|
92
|
+
<p
|
|
93
|
+
class="text-body-medium mb-4"
|
|
94
|
+
:class="status.recoveryCodesLeft > 0 ? 'text-on-surface-variant' : 'text-error'"
|
|
95
|
+
>
|
|
96
|
+
{{ t("jskit.auth.secondFactor.settings.codesLeft", { count: status.recoveryCodesLeft }) }}
|
|
97
|
+
</p>
|
|
98
|
+
<div class="second-factor-settings__actions">
|
|
99
|
+
<v-btn variant="text" color="error" @click="showStep('remove')">{{ t("jskit.auth.secondFactor.settings.remove") }}</v-btn>
|
|
100
|
+
<v-btn variant="tonal" color="primary" @click="showStep('replaceCodes')">
|
|
101
|
+
{{ t("jskit.auth.secondFactor.settings.newCodes") }}
|
|
102
|
+
</v-btn>
|
|
103
|
+
</div>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
<div v-else>
|
|
107
|
+
<p ref="stepStart" tabindex="-1" class="text-body-medium text-on-surface-variant mb-4">
|
|
108
|
+
{{ t("jskit.auth.secondFactor.settings.off") }}
|
|
109
|
+
</p>
|
|
110
|
+
<v-btn color="primary" variant="flat" :disabled="starting" @click="startSetup">
|
|
111
|
+
{{ t("jskit.auth.secondFactor.settings.setUp") }}
|
|
112
|
+
</v-btn>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
</template>
|
|
116
|
+
|
|
117
|
+
<script setup>
|
|
118
|
+
import { nextTick, ref } from "vue";
|
|
119
|
+
import SecondFactorProofForm from "./SecondFactorProofForm.vue";
|
|
120
|
+
import { useSecondFactorSettings } from "../composables/secondFactor/useSecondFactorSettings.js";
|
|
121
|
+
import { useJskitText } from "../i18n.js";
|
|
122
|
+
import {
|
|
123
|
+
confirmSecondFactorSetup,
|
|
124
|
+
removeSecondFactor,
|
|
125
|
+
replaceSecondFactorRecoveryCodes
|
|
126
|
+
} from "../runtime/secondFactorRequests.js";
|
|
127
|
+
|
|
128
|
+
// The signed-in person's authenticator app: setting one up, its recovery codes, and removing it. An account settings
|
|
129
|
+
// section places it; the section's title names it.
|
|
130
|
+
const { formatDate, t } = useJskitText();
|
|
131
|
+
const {
|
|
132
|
+
load,
|
|
133
|
+
status,
|
|
134
|
+
step,
|
|
135
|
+
setup,
|
|
136
|
+
qrCode,
|
|
137
|
+
secretText,
|
|
138
|
+
recoveryCodes,
|
|
139
|
+
starting,
|
|
140
|
+
canCopy,
|
|
141
|
+
reload,
|
|
142
|
+
startSetup,
|
|
143
|
+
showRecoveryCodes,
|
|
144
|
+
codesSaved,
|
|
145
|
+
removed,
|
|
146
|
+
showStep,
|
|
147
|
+
copy
|
|
148
|
+
} = useSecondFactorSettings();
|
|
149
|
+
|
|
150
|
+
// After a step ends, focus moves to the start of the next, so a keyboard or screen reader carries on from there.
|
|
151
|
+
const stepStart = ref(null);
|
|
152
|
+
async function focusStep() {
|
|
153
|
+
await nextTick();
|
|
154
|
+
if (stepStart.value) {
|
|
155
|
+
stepStart.value.focus();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function onRecoveryCodes(result) {
|
|
160
|
+
showRecoveryCodes(result);
|
|
161
|
+
void focusStep();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function onCodesSaved() {
|
|
165
|
+
codesSaved();
|
|
166
|
+
void focusStep();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function backToStatus() {
|
|
170
|
+
showStep("status");
|
|
171
|
+
void focusStep();
|
|
172
|
+
}
|
|
173
|
+
</script>
|
|
174
|
+
|
|
175
|
+
<style scoped>
|
|
176
|
+
.second-factor-settings__actions,
|
|
177
|
+
.second-factor-settings__secret-row {
|
|
178
|
+
display: flex;
|
|
179
|
+
flex-wrap: wrap;
|
|
180
|
+
align-items: center;
|
|
181
|
+
gap: 8px;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.second-factor-settings__actions {
|
|
185
|
+
justify-content: flex-end;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.second-factor-settings__qr {
|
|
189
|
+
display: block;
|
|
190
|
+
width: min(200px, 100%);
|
|
191
|
+
height: auto;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.second-factor-settings__secret {
|
|
195
|
+
font-family: ui-monospace, monospace;
|
|
196
|
+
overflow-wrap: anywhere;
|
|
197
|
+
user-select: all;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.second-factor-settings__codes {
|
|
201
|
+
display: grid;
|
|
202
|
+
grid-template-columns: repeat(auto-fill, minmax(12ch, 1fr));
|
|
203
|
+
gap: 8px 24px;
|
|
204
|
+
padding: 0;
|
|
205
|
+
list-style: none;
|
|
206
|
+
font-family: ui-monospace, monospace;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
[tabindex="-1"]:focus {
|
|
210
|
+
outline: none;
|
|
211
|
+
}
|
|
212
|
+
</style>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
2
2
|
import { createAuthWeb } from "./authWeb.js";
|
|
3
|
+
import { registerSecondFactorRoutes } from "./routes/secondFactorRoutes.js";
|
|
3
4
|
|
|
4
5
|
const AuthWebFeature = defineFeature({
|
|
5
6
|
id: "auth.web",
|
|
@@ -25,6 +26,7 @@ const AuthWebFeature = defineFeature({
|
|
|
25
26
|
for (const route of authWeb.routes) {
|
|
26
27
|
http.router.register(route.method, route.path, route, route.handler);
|
|
27
28
|
}
|
|
29
|
+
registerSecondFactorRoutes(http.router, { rateLimits: authWeb.rateLimits });
|
|
28
30
|
return { authWeb };
|
|
29
31
|
}
|
|
30
32
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isRecord } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* The auth routes' default rate limits, per client address. Sign-in
|
|
5
|
-
* failed attempts per account, in the provider; these
|
|
6
|
-
* address can try anything at all.
|
|
4
|
+
* The auth routes' default rate limits, per client address. Sign-in and the
|
|
5
|
+
* second factor also limit failed attempts per account, in the provider; these
|
|
6
|
+
* limits bound how fast one address can try anything at all.
|
|
7
7
|
*/
|
|
8
8
|
const DEFAULT_AUTH_RATE_LIMITS = Object.freeze({
|
|
9
9
|
register: Object.freeze({ max: 10, timeWindow: "1 minute" }),
|
|
@@ -17,7 +17,12 @@ const DEFAULT_AUTH_RATE_LIMITS = Object.freeze({
|
|
|
17
17
|
devLoginAs: Object.freeze({ max: 30, timeWindow: "1 minute" }),
|
|
18
18
|
passwordForgot: Object.freeze({ max: 5, timeWindow: "1 minute" }),
|
|
19
19
|
passwordRecovery: Object.freeze({ max: 20, timeWindow: "1 minute" }),
|
|
20
|
-
passwordReset: Object.freeze({ max: 20, timeWindow: "1 minute" })
|
|
20
|
+
passwordReset: Object.freeze({ max: 20, timeWindow: "1 minute" }),
|
|
21
|
+
secondFactorSetup: Object.freeze({ max: 10, timeWindow: "1 minute" }),
|
|
22
|
+
secondFactorSetupConfirm: Object.freeze({ max: 10, timeWindow: "1 minute" }),
|
|
23
|
+
secondFactorVerify: Object.freeze({ max: 10, timeWindow: "1 minute" }),
|
|
24
|
+
secondFactorRecoveryCodes: Object.freeze({ max: 5, timeWindow: "1 minute" }),
|
|
25
|
+
secondFactorRemove: Object.freeze({ max: 5, timeWindow: "1 minute" })
|
|
21
26
|
});
|
|
22
27
|
|
|
23
28
|
/**
|
package/src/server/authWeb.js
CHANGED
|
@@ -6,9 +6,9 @@ import { AuthWebService } from "./services/AuthWebService.js";
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The auth web routes over the installed auth service: their service,
|
|
9
|
-
* controller and route table
|
|
10
|
-
* deferred auth service exists, so dev
|
|
11
|
-
* predicate the auth providers use.
|
|
9
|
+
* controller and route table, and every auth route's rate limit. Routes are
|
|
10
|
+
* registered at setup, before the deferred auth service exists, so dev
|
|
11
|
+
* login-as is read from the env with the predicate the auth providers use.
|
|
12
12
|
* @param {object} dependencies
|
|
13
13
|
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService
|
|
14
14
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} dependencies.env
|
|
@@ -20,14 +20,16 @@ function createAuthWeb({ authService, env, config }) {
|
|
|
20
20
|
devAuthBootstrapEnabled: isDevAuthBootstrapEnabled(resolveDevAuthPolicyFromEnv(env))
|
|
21
21
|
});
|
|
22
22
|
const controller = new AuthController({ service });
|
|
23
|
+
const rateLimits = resolveAuthRateLimits(config);
|
|
23
24
|
const routes = buildRoutes(controller, {
|
|
24
25
|
includeDevLoginAs: service.isDevLoginAsAvailable(),
|
|
25
|
-
rateLimits
|
|
26
|
+
rateLimits
|
|
26
27
|
});
|
|
27
28
|
return Object.freeze({
|
|
28
29
|
controller,
|
|
29
30
|
routes: Object.freeze(routes),
|
|
30
|
-
service
|
|
31
|
+
service,
|
|
32
|
+
rateLimits
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
|
|
@@ -24,7 +24,7 @@ import { DEFAULT_AUTH_RATE_LIMITS } from "../authRateLimits.js";
|
|
|
24
24
|
* @param {import("../controllers/AuthController.js").AuthController} controller
|
|
25
25
|
* @param {{
|
|
26
26
|
* includeDevLoginAs?: boolean,
|
|
27
|
-
* rateLimits?:
|
|
27
|
+
* rateLimits?: import("../types.js").AuthRateLimits
|
|
28
28
|
* }} [options]
|
|
29
29
|
*/
|
|
30
30
|
function buildRoutes(controller, { includeDevLoginAs = false, rateLimits = DEFAULT_AUTH_RATE_LIMITS } = {}) {
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACTION_CONFIRM_SECOND_FACTOR_SETUP,
|
|
3
|
+
ACTION_READ_SECOND_FACTOR,
|
|
4
|
+
ACTION_REMOVE_SECOND_FACTOR,
|
|
5
|
+
ACTION_REPLACE_SECOND_FACTOR_RECOVERY_CODES,
|
|
6
|
+
ACTION_START_SECOND_FACTOR_SETUP,
|
|
7
|
+
ACTION_VERIFY_SECOND_FACTOR
|
|
8
|
+
} from "@jskit-ai/auth-core/server/actions/secondFactorActions";
|
|
9
|
+
import { AUTH_PATHS } from "@jskit-ai/auth-core/shared/authPaths";
|
|
10
|
+
import {
|
|
11
|
+
authSecondFactorReadCommand,
|
|
12
|
+
authSecondFactorRecoveryCodesReplaceCommand,
|
|
13
|
+
authSecondFactorRemoveCommand,
|
|
14
|
+
authSecondFactorSetupConfirmCommand,
|
|
15
|
+
authSecondFactorSetupStartCommand,
|
|
16
|
+
authSecondFactorVerifyCommand
|
|
17
|
+
} from "@jskit-ai/auth-core/shared/commands";
|
|
18
|
+
import { withStandardErrorResponses } from "@jskit-ai/http-runtime/shared/validators/errorResponses";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The signed-in person's second factor: reading it, setting up an
|
|
22
|
+
* authenticator, proving a code for this session, and replacing the recovery
|
|
23
|
+
* codes or removing the authenticator with a proof. Each route runs its
|
|
24
|
+
* auth.secondFactor action, limited per client address by its entry in
|
|
25
|
+
* `rateLimits` (none when that entry is null); the provider also makes an
|
|
26
|
+
* account wait after wrong codes.
|
|
27
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
28
|
+
* @param {{ rateLimits: import("../types.js").AuthRateLimits }} dependencies
|
|
29
|
+
*/
|
|
30
|
+
function registerSecondFactorRoutes(router, { rateLimits }) {
|
|
31
|
+
router.register(
|
|
32
|
+
"GET",
|
|
33
|
+
AUTH_PATHS.SECOND_FACTOR,
|
|
34
|
+
{
|
|
35
|
+
auth: "required",
|
|
36
|
+
meta: { tags: ["auth"], summary: "Read the account's second factor and when this session proved it" },
|
|
37
|
+
responses: withStandardErrorResponses({ 200: authSecondFactorReadCommand.operation.response })
|
|
38
|
+
},
|
|
39
|
+
async function (request, reply) {
|
|
40
|
+
reply.code(200).send(await request.executeAction({ actionId: ACTION_READ_SECOND_FACTOR }));
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
router.register(
|
|
45
|
+
"POST",
|
|
46
|
+
AUTH_PATHS.SECOND_FACTOR_SETUP,
|
|
47
|
+
{
|
|
48
|
+
auth: "required",
|
|
49
|
+
meta: { tags: ["auth"], summary: "Start setting up an authenticator app: its secret, shown once" },
|
|
50
|
+
responses: withStandardErrorResponses({ 200: authSecondFactorSetupStartCommand.operation.response }),
|
|
51
|
+
rateLimit: rateLimits.secondFactorSetup
|
|
52
|
+
},
|
|
53
|
+
async function (request, reply) {
|
|
54
|
+
reply.code(200).send(await request.executeAction({ actionId: ACTION_START_SECOND_FACTOR_SETUP }));
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
router.register(
|
|
59
|
+
"POST",
|
|
60
|
+
AUTH_PATHS.SECOND_FACTOR_SETUP_CONFIRM,
|
|
61
|
+
{
|
|
62
|
+
auth: "required",
|
|
63
|
+
meta: { tags: ["auth"], summary: "Confirm the authenticator with its first code: the recovery codes, shown once" },
|
|
64
|
+
body: authSecondFactorSetupConfirmCommand.operation.body,
|
|
65
|
+
responses: withStandardErrorResponses(
|
|
66
|
+
{ 200: authSecondFactorSetupConfirmCommand.operation.response },
|
|
67
|
+
{ includeValidation400: true }
|
|
68
|
+
),
|
|
69
|
+
rateLimit: rateLimits.secondFactorSetupConfirm
|
|
70
|
+
},
|
|
71
|
+
async function (request, reply) {
|
|
72
|
+
reply.code(200).send(await request.executeAction({ actionId: ACTION_CONFIRM_SECOND_FACTOR_SETUP, input: request.input.body }));
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
router.register(
|
|
77
|
+
"POST",
|
|
78
|
+
AUTH_PATHS.SECOND_FACTOR_VERIFY,
|
|
79
|
+
{
|
|
80
|
+
auth: "required",
|
|
81
|
+
meta: { tags: ["auth"], summary: "Prove the second factor for this session with a code or a recovery code" },
|
|
82
|
+
body: authSecondFactorVerifyCommand.operation.body,
|
|
83
|
+
responses: withStandardErrorResponses(
|
|
84
|
+
{ 200: authSecondFactorVerifyCommand.operation.response },
|
|
85
|
+
{ includeValidation400: true }
|
|
86
|
+
),
|
|
87
|
+
rateLimit: rateLimits.secondFactorVerify
|
|
88
|
+
},
|
|
89
|
+
async function (request, reply) {
|
|
90
|
+
reply.code(200).send(await request.executeAction({ actionId: ACTION_VERIFY_SECOND_FACTOR, input: request.input.body }));
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
router.register(
|
|
95
|
+
"POST",
|
|
96
|
+
AUTH_PATHS.SECOND_FACTOR_RECOVERY_CODES,
|
|
97
|
+
{
|
|
98
|
+
auth: "required",
|
|
99
|
+
meta: { tags: ["auth"], summary: "Replace the recovery codes, with a proof: the new codes, shown once" },
|
|
100
|
+
body: authSecondFactorRecoveryCodesReplaceCommand.operation.body,
|
|
101
|
+
responses: withStandardErrorResponses(
|
|
102
|
+
{ 200: authSecondFactorRecoveryCodesReplaceCommand.operation.response },
|
|
103
|
+
{ includeValidation400: true }
|
|
104
|
+
),
|
|
105
|
+
rateLimit: rateLimits.secondFactorRecoveryCodes
|
|
106
|
+
},
|
|
107
|
+
async function (request, reply) {
|
|
108
|
+
reply.code(200).send(await request.executeAction({
|
|
109
|
+
actionId: ACTION_REPLACE_SECOND_FACTOR_RECOVERY_CODES,
|
|
110
|
+
input: request.input.body
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
router.register(
|
|
116
|
+
"DELETE",
|
|
117
|
+
AUTH_PATHS.SECOND_FACTOR,
|
|
118
|
+
{
|
|
119
|
+
auth: "required",
|
|
120
|
+
meta: { tags: ["auth"], summary: "Remove the authenticator, with a proof" },
|
|
121
|
+
body: authSecondFactorRemoveCommand.operation.body,
|
|
122
|
+
responses: withStandardErrorResponses(
|
|
123
|
+
{ 200: authSecondFactorRemoveCommand.operation.response },
|
|
124
|
+
{ includeValidation400: true }
|
|
125
|
+
),
|
|
126
|
+
rateLimit: rateLimits.secondFactorRemove
|
|
127
|
+
},
|
|
128
|
+
async function (request, reply) {
|
|
129
|
+
reply.code(200).send(await request.executeAction({ actionId: ACTION_REMOVE_SECOND_FACTOR, input: request.input.body }));
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { registerSecondFactorRoutes };
|
package/src/server/types.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
// The type of the `auth.web` capability, for features that require it.
|
|
1
|
+
// The type of the `auth.web` capability, for features that require it, and of the auth routes' rate limits.
|
|
2
2
|
|
|
3
3
|
/** @typedef {ReturnType<typeof import("./authWeb.js").createAuthWeb>} AuthWeb */
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* The per-address limit of each auth route; null for a route without one.
|
|
7
|
+
* @typedef {ReturnType<typeof import("./authRateLimits.js").resolveAuthRateLimits>} AuthRateLimits
|
|
8
|
+
*/
|
|
9
|
+
|
|
5
10
|
export {};
|