@jskit-ai/auth-web 0.2.32 → 0.2.34
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/locales/en.json +51 -0
- package/package.json +42 -6
- package/patterns/auth-surface/PATTERN.md +10 -0
- 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/managedPreviewIdentity.js +78 -43
- 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/managedPreviewIdentity.test.js +98 -1
- 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
|
|
|
@@ -269,37 +269,86 @@ async function login(fetchImpl, targetOrigin, identity, secret, session) {
|
|
|
269
269
|
if (!fetchResponse.ok || payload?.ok !== true) {
|
|
270
270
|
throw rejected(payload, fetchResponse, { setCookie, signedOut: true });
|
|
271
271
|
}
|
|
272
|
+
// readable-check: allow setter-seam - not a slot: setCookie is the preview identity protocol's list of Set-Cookie headers
|
|
273
|
+
return { identity: payload, setCookie };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* JSKIT's development exchange, the default: it signs the preview browser out, and for `login-as` back in through the
|
|
278
|
+
* development login-as route, so it needs both development values in the application's environment.
|
|
279
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
280
|
+
* @returns {PreviewIdentityExchange}
|
|
281
|
+
*/
|
|
282
|
+
function developmentExchange(env) {
|
|
283
|
+
const devAuth = resolveDevAuthPolicyFromEnv(env);
|
|
284
|
+
if (!isDevAuthBootstrapEnabled(devAuth)) {
|
|
285
|
+
throw commandError(
|
|
286
|
+
"Managed preview identity is not enabled.",
|
|
287
|
+
"jskit_managed_preview_identity_disabled",
|
|
288
|
+
{ statusCode: 403 }
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
if (!devAuth.secret) {
|
|
292
|
+
throw commandError(
|
|
293
|
+
"Managed preview identity secret is unavailable.",
|
|
294
|
+
"jskit_managed_preview_identity_secret_missing",
|
|
295
|
+
{ statusCode: 500 }
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
return async ({ operation, identity, targetOrigin, fetchImpl }) => {
|
|
299
|
+
const session = await bootstrapSession(fetchImpl, targetOrigin);
|
|
300
|
+
const signedOutSession = await logout(fetchImpl, targetOrigin, session);
|
|
301
|
+
return operation === "logout"
|
|
302
|
+
? signedOutSession
|
|
303
|
+
: login(fetchImpl, targetOrigin, identity, devAuth.secret, signedOutSession);
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The identity a sign-in reports: what the application answered, else what the request asked for.
|
|
309
|
+
* @param {Record<string, any> | null | undefined} answered
|
|
310
|
+
* @param {{ email?: string, userId?: string }} asked
|
|
311
|
+
*/
|
|
312
|
+
function signedInIdentity(answered, asked) {
|
|
272
313
|
return {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
username: String(payload.username || "").trim()
|
|
278
|
-
},
|
|
279
|
-
// readable-check: allow setter-seam - not a slot: setCookie is the preview identity protocol's list of Set-Cookie headers
|
|
280
|
-
setCookie
|
|
314
|
+
displayName: String(answered?.displayName || answered?.username || "").trim(),
|
|
315
|
+
email: String(answered?.email || asked.email || "").trim().toLowerCase(),
|
|
316
|
+
userId: String(answered?.userId || asked.userId || "").trim(),
|
|
317
|
+
username: String(answered?.username || "").trim()
|
|
281
318
|
};
|
|
282
319
|
}
|
|
283
320
|
|
|
321
|
+
/**
|
|
322
|
+
* How the preview browser is signed in or out: given the operation, the existing user a `login-as` names
|
|
323
|
+
* (`{ email }` or `{ userId }`, else null), the checked local target origin and the fetch to reach it with, it resolves
|
|
324
|
+
* with the Set-Cookie headers for the browser and, for `login-as`, what the application says about the user signed
|
|
325
|
+
* in. A refusal is an error whose `code`, `statusCode`, `setCookie` and `signedOut` the response reports.
|
|
326
|
+
* @typedef {(call: { operation: "login-as" | "logout", identity: { email?: string, userId?: string } | null, targetOrigin: string, fetchImpl: typeof fetch }) => Promise<{ setCookie: string[], identity?: Record<string, any> | null }>} PreviewIdentityExchange
|
|
327
|
+
*/
|
|
328
|
+
|
|
284
329
|
/**
|
|
285
330
|
* Answers one preview identity request: signs the preview browser out, and for
|
|
286
331
|
* `login-as` back in as an existing application user, through the development
|
|
287
|
-
* exchange
|
|
332
|
+
* exchange, or through the application's own `exchange` when it has another
|
|
333
|
+
* way in. The preview host names the protocol id and any hostnames, besides
|
|
288
334
|
* loopback, that it serves the application on.
|
|
289
335
|
* @param {Record<string, any>} value The command's request, which is validated here.
|
|
290
336
|
* @param {object} options
|
|
291
|
-
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} options.env The application's environment, such as
|
|
337
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} [options.env] The application's environment, such as
|
|
338
|
+
* `process.env`, which the development exchange reads; required without `exchange`.
|
|
292
339
|
* @param {typeof fetch} [options.fetchImpl]
|
|
293
340
|
* @param {string} [options.protocol] The protocol id requests carry and responses echo.
|
|
294
341
|
* @param {RegExp[]} [options.localHostnames] Hostnames, besides loopback, the application is served on.
|
|
342
|
+
* @param {PreviewIdentityExchange} [options.exchange] The application's own exchange, in place of the development one.
|
|
295
343
|
*/
|
|
296
344
|
async function executeManagedPreviewIdentityRequest(value, {
|
|
297
345
|
env,
|
|
298
346
|
fetchImpl = globalThis.fetch,
|
|
299
347
|
protocol = DEFAULT_PREVIEW_IDENTITY_PROTOCOL,
|
|
300
|
-
localHostnames = []
|
|
348
|
+
localHostnames = [],
|
|
349
|
+
exchange
|
|
301
350
|
}) {
|
|
302
|
-
if (!env) {
|
|
351
|
+
if (!env && !exchange) {
|
|
303
352
|
throw new TypeError("executeManagedPreviewIdentityRequest requires the application's environment, such as process.env.");
|
|
304
353
|
}
|
|
305
354
|
let requestId = String(value?.requestId || "").trim();
|
|
@@ -314,42 +363,25 @@ async function executeManagedPreviewIdentityRequest(value, {
|
|
|
314
363
|
{ statusCode: 500 }
|
|
315
364
|
);
|
|
316
365
|
}
|
|
317
|
-
const
|
|
318
|
-
if (!isDevAuthBootstrapEnabled(devAuth)) {
|
|
319
|
-
throw commandError(
|
|
320
|
-
"Managed preview identity is not enabled.",
|
|
321
|
-
"jskit_managed_preview_identity_disabled",
|
|
322
|
-
{ statusCode: 403 }
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
if (!devAuth.secret) {
|
|
326
|
-
throw commandError(
|
|
327
|
-
"Managed preview identity secret is unavailable.",
|
|
328
|
-
"jskit_managed_preview_identity_secret_missing",
|
|
329
|
-
{ statusCode: 500 }
|
|
330
|
-
);
|
|
331
|
-
}
|
|
366
|
+
const run = exchange || developmentExchange(env);
|
|
332
367
|
const identity = request.operation === "login-as"
|
|
333
368
|
? identityFromSubject(request.subject)
|
|
334
369
|
: null;
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
setCookie: signedOutSession.setCookie,
|
|
343
|
-
signedOut: true
|
|
344
|
-
});
|
|
370
|
+
const result = await run({ operation: request.operation, identity, targetOrigin: request.targetOrigin, fetchImpl });
|
|
371
|
+
if (!Array.isArray(result?.setCookie)) {
|
|
372
|
+
throw commandError(
|
|
373
|
+
"Managed preview identity exchange returned no cookie list.",
|
|
374
|
+
"jskit_managed_preview_identity_exchange_invalid",
|
|
375
|
+
{ statusCode: 502 }
|
|
376
|
+
);
|
|
345
377
|
}
|
|
346
|
-
const
|
|
378
|
+
const signedOut = request.operation === "logout";
|
|
347
379
|
return response(protocol, requestId, {
|
|
348
|
-
identity: result.identity,
|
|
380
|
+
identity: signedOut ? null : signedInIdentity(result.identity, identity),
|
|
349
381
|
ok: true,
|
|
350
382
|
// readable-check: allow setter-seam - not a slot: setCookie is the preview identity protocol's list of Set-Cookie headers
|
|
351
383
|
setCookie: result.setCookie,
|
|
352
|
-
signedOut
|
|
384
|
+
signedOut
|
|
353
385
|
});
|
|
354
386
|
} catch (error) {
|
|
355
387
|
return failure(protocol, requestId, error);
|
|
@@ -359,12 +391,14 @@ async function executeManagedPreviewIdentityRequest(value, {
|
|
|
359
391
|
/**
|
|
360
392
|
* Reads one request from stdin and writes its response to stdout.
|
|
361
393
|
* @param {object} options
|
|
362
|
-
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} options.env The application's environment, such as
|
|
394
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} [options.env] The application's environment, such as
|
|
395
|
+
* `process.env`, which the development exchange reads; required without `exchange`.
|
|
363
396
|
* @param {typeof fetch} [options.fetchImpl]
|
|
364
397
|
* @param {AsyncIterable<string | Buffer>} [options.stdin]
|
|
365
398
|
* @param {{ write(text: string): unknown }} [options.stdout]
|
|
366
399
|
* @param {string} [options.protocol] The protocol id requests carry and responses echo.
|
|
367
400
|
* @param {RegExp[]} [options.localHostnames] Hostnames, besides loopback, the application is served on.
|
|
401
|
+
* @param {PreviewIdentityExchange} [options.exchange] The application's own exchange, in place of the development one.
|
|
368
402
|
*/
|
|
369
403
|
async function runManagedPreviewIdentityCommand({
|
|
370
404
|
env,
|
|
@@ -372,7 +406,8 @@ async function runManagedPreviewIdentityCommand({
|
|
|
372
406
|
stdin = process.stdin,
|
|
373
407
|
stdout = process.stdout,
|
|
374
408
|
protocol = DEFAULT_PREVIEW_IDENTITY_PROTOCOL,
|
|
375
|
-
localHostnames = []
|
|
409
|
+
localHostnames = [],
|
|
410
|
+
exchange
|
|
376
411
|
}) {
|
|
377
412
|
let request;
|
|
378
413
|
try {
|
|
@@ -381,7 +416,7 @@ async function runManagedPreviewIdentityCommand({
|
|
|
381
416
|
stdout.write(`${JSON.stringify(failure(protocol, "", error))}\n`);
|
|
382
417
|
return 0;
|
|
383
418
|
}
|
|
384
|
-
const result = await executeManagedPreviewIdentityRequest(request, { env, fetchImpl, protocol, localHostnames });
|
|
419
|
+
const result = await executeManagedPreviewIdentityRequest(request, { env, fetchImpl, protocol, localHostnames, exchange });
|
|
385
420
|
stdout.write(`${JSON.stringify(result)}\n`);
|
|
386
421
|
return 0;
|
|
387
422
|
}
|
|
@@ -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 } = {}) {
|