@jskit-ai/auth-web 0.2.4 → 0.2.6
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/package.json +11 -7
- package/patterns/auth-surface/PATTERN.md +25 -12
- package/patterns/auth-surface/example/tools/preview-identity +7 -1
- package/src/client/composables/loginView/useLoginViewActions.js +18 -1
- package/src/client/composables/loginView/useSessionRecheckOnShow.js +23 -0
- package/src/client/runtime/useLoginView.js +2 -0
- package/src/client/views/DefaultLoginView.vue +2 -0
- package/src/server/AuthWebFeature.js +10 -21
- package/src/server/authWeb.js +31 -0
- package/src/server/controllers/AuthController.js +10 -22
- package/src/server/managedPreviewIdentity.js +92 -43
- package/src/server/routes/authRoutes.js +17 -19
- package/src/server/services/AuthWebService.js +27 -62
- package/src/server/types.js +5 -0
- package/src/test/playwright.js +6 -3
- package/test/loginViewActions.test.js +46 -0
- package/test/managedPreviewIdentity.test.js +83 -12
- package/test/playwright.test.js +44 -1
- package/test/providerRuntime.test.js +6 -3
- package/test/sessionRecheckOnShow.test.js +83 -0
|
@@ -16,13 +16,11 @@ import {
|
|
|
16
16
|
} from "@jskit-ai/auth-core/shared/commands";
|
|
17
17
|
import { AUTH_PATHS } from "@jskit-ai/auth-core/shared/authPaths";
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* @param {import("../controllers/AuthController.js").AuthController} controller
|
|
21
|
+
* @param {{ includeDevLoginAs?: boolean }} [options]
|
|
22
|
+
*/
|
|
19
23
|
function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
20
|
-
if (!controller) {
|
|
21
|
-
throw new Error("Auth routes require a controller instance.");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const handler = (methodName) => controller[methodName].bind(controller);
|
|
25
|
-
|
|
26
24
|
return [
|
|
27
25
|
{
|
|
28
26
|
path: AUTH_PATHS.REGISTER,
|
|
@@ -43,7 +41,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
43
41
|
max: 10,
|
|
44
42
|
timeWindow: "1 minute"
|
|
45
43
|
},
|
|
46
|
-
handler:
|
|
44
|
+
handler: (request, reply) => controller.register(request, reply)
|
|
47
45
|
},
|
|
48
46
|
{
|
|
49
47
|
path: AUTH_PATHS.REGISTER_CONFIRMATION_RESEND,
|
|
@@ -64,7 +62,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
64
62
|
max: 5,
|
|
65
63
|
timeWindow: "1 minute"
|
|
66
64
|
},
|
|
67
|
-
handler:
|
|
65
|
+
handler: (request, reply) => controller.resendRegisterConfirmation(request, reply)
|
|
68
66
|
},
|
|
69
67
|
{
|
|
70
68
|
path: AUTH_PATHS.LOGIN,
|
|
@@ -85,7 +83,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
85
83
|
max: 10,
|
|
86
84
|
timeWindow: "1 minute"
|
|
87
85
|
},
|
|
88
|
-
handler:
|
|
86
|
+
handler: (request, reply) => controller.login(request, reply)
|
|
89
87
|
},
|
|
90
88
|
{
|
|
91
89
|
path: AUTH_PATHS.LOGIN_OTP_REQUEST,
|
|
@@ -106,7 +104,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
106
104
|
max: 10,
|
|
107
105
|
timeWindow: "1 minute"
|
|
108
106
|
},
|
|
109
|
-
handler:
|
|
107
|
+
handler: (request, reply) => controller.requestOtpLogin(request, reply)
|
|
110
108
|
},
|
|
111
109
|
{
|
|
112
110
|
path: AUTH_PATHS.LOGIN_OTP_VERIFY,
|
|
@@ -127,7 +125,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
127
125
|
max: 10,
|
|
128
126
|
timeWindow: "1 minute"
|
|
129
127
|
},
|
|
130
|
-
handler:
|
|
128
|
+
handler: (request, reply) => controller.verifyOtpLogin(request, reply)
|
|
131
129
|
},
|
|
132
130
|
{
|
|
133
131
|
path: AUTH_PATHS.OAUTH_START_TEMPLATE,
|
|
@@ -150,7 +148,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
150
148
|
max: 20,
|
|
151
149
|
timeWindow: "1 minute"
|
|
152
150
|
},
|
|
153
|
-
handler:
|
|
151
|
+
handler: (request, reply) => controller.oauthStart(request, reply)
|
|
154
152
|
},
|
|
155
153
|
{
|
|
156
154
|
path: AUTH_PATHS.OAUTH_COMPLETE,
|
|
@@ -171,7 +169,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
171
169
|
max: 20,
|
|
172
170
|
timeWindow: "1 minute"
|
|
173
171
|
},
|
|
174
|
-
handler:
|
|
172
|
+
handler: (request, reply) => controller.oauthComplete(request, reply)
|
|
175
173
|
},
|
|
176
174
|
...(includeDevLoginAs ? [{
|
|
177
175
|
path: AUTH_PATHS.DEV_LOGIN_AS,
|
|
@@ -192,7 +190,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
192
190
|
max: 30,
|
|
193
191
|
timeWindow: "1 minute"
|
|
194
192
|
},
|
|
195
|
-
handler:
|
|
193
|
+
handler: (request, reply) => controller.devLoginAs(request, reply)
|
|
196
194
|
}] : []),
|
|
197
195
|
{
|
|
198
196
|
path: AUTH_PATHS.PASSWORD_FORGOT,
|
|
@@ -213,7 +211,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
213
211
|
max: 5,
|
|
214
212
|
timeWindow: "1 minute"
|
|
215
213
|
},
|
|
216
|
-
handler:
|
|
214
|
+
handler: (request, reply) => controller.requestPasswordReset(request, reply)
|
|
217
215
|
},
|
|
218
216
|
{
|
|
219
217
|
path: AUTH_PATHS.PASSWORD_RECOVERY,
|
|
@@ -234,7 +232,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
234
232
|
max: 20,
|
|
235
233
|
timeWindow: "1 minute"
|
|
236
234
|
},
|
|
237
|
-
handler:
|
|
235
|
+
handler: (request, reply) => controller.completePasswordRecovery(request, reply)
|
|
238
236
|
},
|
|
239
237
|
{
|
|
240
238
|
path: AUTH_PATHS.PASSWORD_RESET,
|
|
@@ -255,7 +253,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
255
253
|
max: 20,
|
|
256
254
|
timeWindow: "1 minute"
|
|
257
255
|
},
|
|
258
|
-
handler:
|
|
256
|
+
handler: (request, reply) => controller.resetPassword(request, reply)
|
|
259
257
|
},
|
|
260
258
|
{
|
|
261
259
|
path: AUTH_PATHS.LOGOUT,
|
|
@@ -268,7 +266,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
268
266
|
responses: withStandardErrorResponses({
|
|
269
267
|
200: authLogoutCommand.operation.response
|
|
270
268
|
}),
|
|
271
|
-
handler:
|
|
269
|
+
handler: (request, reply) => controller.logout(request, reply)
|
|
272
270
|
},
|
|
273
271
|
{
|
|
274
272
|
path: AUTH_PATHS.SESSION,
|
|
@@ -282,7 +280,7 @@ function buildRoutes(controller, { includeDevLoginAs = false } = {}) {
|
|
|
282
280
|
200: authSessionReadCommand.operation.response,
|
|
283
281
|
503: authSessionReadCommand.operation.unavailableResponse
|
|
284
282
|
}),
|
|
285
|
-
handler:
|
|
283
|
+
handler: (request, reply) => controller.session(request, reply)
|
|
286
284
|
}
|
|
287
285
|
];
|
|
288
286
|
}
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { normalizeAuthCapabilities } from "@jskit-ai/auth-core/shared/authCapabilities";
|
|
2
2
|
import { AUTH_ACTION_IDS } from "../constants/authActionIds.js";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The auth routes' service: it runs the auth actions for a request and
|
|
6
|
+
* writes the installed auth service's cookies.
|
|
7
|
+
*/
|
|
4
8
|
class AuthWebService {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @param {object} dependencies
|
|
11
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService
|
|
12
|
+
* @param {boolean} dependencies.devAuthBootstrapEnabled Whether dev login-as is offered.
|
|
13
|
+
*/
|
|
14
|
+
constructor({ authService, devAuthBootstrapEnabled }) {
|
|
9
15
|
this.authService = authService;
|
|
10
|
-
this.devAuthBootstrapEnabled =
|
|
11
|
-
typeof devAuthBootstrapEnabled === "boolean" ? devAuthBootstrapEnabled : null;
|
|
16
|
+
this.devAuthBootstrapEnabled = devAuthBootstrapEnabled;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
static get actionIds() {
|
|
@@ -71,18 +76,8 @@ class AuthWebService {
|
|
|
71
76
|
});
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
resolveAuthService() {
|
|
75
|
-
return this.authService;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
79
|
isDevLoginAsAvailable() {
|
|
79
|
-
|
|
80
|
-
return this.devAuthBootstrapEnabled === true;
|
|
81
|
-
}
|
|
82
|
-
const authService = this.resolveAuthService();
|
|
83
|
-
return typeof authService?.isDevAuthBootstrapEnabled === "function"
|
|
84
|
-
? authService.isDevAuthBootstrapEnabled()
|
|
85
|
-
: false;
|
|
80
|
+
return this.devAuthBootstrapEnabled;
|
|
86
81
|
}
|
|
87
82
|
|
|
88
83
|
async logout(request) {
|
|
@@ -120,21 +115,25 @@ class AuthWebService {
|
|
|
120
115
|
|
|
121
116
|
writeSessionCookies(reply, session) {
|
|
122
117
|
if (session && reply) {
|
|
123
|
-
this.
|
|
118
|
+
this.authService.writeSessionCookies(reply, session);
|
|
124
119
|
}
|
|
125
120
|
}
|
|
126
121
|
|
|
127
|
-
|
|
128
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Providers that bind an OAuth attempt to the browser (state, nonce, PKCE)
|
|
124
|
+
* keep it in their own short-lived cookie; others need no hook.
|
|
125
|
+
*/
|
|
129
126
|
writeOAuthStartCookies(reply, result) {
|
|
130
|
-
const authService = this
|
|
127
|
+
const { authService } = this;
|
|
128
|
+
// readable-check: allow method-probe - optional operation: writeOAuthStartCookies is an optional auth.service operation
|
|
131
129
|
if (reply && typeof authService.writeOAuthStartCookies === "function") {
|
|
132
130
|
authService.writeOAuthStartCookies(reply, result);
|
|
133
131
|
}
|
|
134
132
|
}
|
|
135
133
|
|
|
136
134
|
clearOAuthStartCookies(reply) {
|
|
137
|
-
const authService = this
|
|
135
|
+
const { authService } = this;
|
|
136
|
+
// readable-check: allow method-probe - optional operation: clearOAuthStartCookies is an optional auth.service operation
|
|
138
137
|
if (reply && typeof authService.clearOAuthStartCookies === "function") {
|
|
139
138
|
authService.clearOAuthStartCookies(reply);
|
|
140
139
|
}
|
|
@@ -142,51 +141,17 @@ class AuthWebService {
|
|
|
142
141
|
|
|
143
142
|
clearSessionCookies(reply) {
|
|
144
143
|
if (reply) {
|
|
145
|
-
this.
|
|
144
|
+
this.authService.clearSessionCookies(reply);
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
147
|
|
|
149
148
|
getOAuthProviderCatalogPayload() {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
const capabilities = normalizeAuthCapabilities(authService.getCapabilities());
|
|
153
|
-
const oauth = capabilities.features.oauthLogin;
|
|
154
|
-
return {
|
|
155
|
-
authCapabilities: capabilities,
|
|
156
|
-
oauthProviders: oauth.enabled ? oauth.providers : [],
|
|
157
|
-
oauthDefaultProvider: oauth.enabled ? oauth.defaultProvider : null
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
const catalog =
|
|
161
|
-
typeof authService.getOAuthProviderCatalog === "function"
|
|
162
|
-
? authService.getOAuthProviderCatalog()
|
|
163
|
-
: null;
|
|
164
|
-
const providers = Array.isArray(catalog?.providers)
|
|
165
|
-
? catalog.providers
|
|
166
|
-
.map((provider) => ({
|
|
167
|
-
id: String(provider?.id || "").trim().toLowerCase(),
|
|
168
|
-
label: String(provider?.label || "").trim()
|
|
169
|
-
}))
|
|
170
|
-
.filter((provider) => provider.id && provider.label)
|
|
171
|
-
: [];
|
|
172
|
-
const defaultProvider = String(catalog?.defaultProvider || "").trim().toLowerCase();
|
|
173
|
-
const normalizedDefaultProvider = providers.some((provider) => provider.id === defaultProvider)
|
|
174
|
-
? defaultProvider
|
|
175
|
-
: null;
|
|
176
|
-
const fallbackCapabilities = normalizeAuthCapabilities({
|
|
177
|
-
features: {
|
|
178
|
-
oauthLogin: {
|
|
179
|
-
enabled: providers.length > 0,
|
|
180
|
-
providers,
|
|
181
|
-
defaultProvider: normalizedDefaultProvider
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
|
|
149
|
+
const capabilities = normalizeAuthCapabilities(this.authService.getCapabilities());
|
|
150
|
+
const oauth = capabilities.features.oauthLogin;
|
|
186
151
|
return {
|
|
187
|
-
authCapabilities:
|
|
188
|
-
oauthProviders: providers,
|
|
189
|
-
oauthDefaultProvider:
|
|
152
|
+
authCapabilities: capabilities,
|
|
153
|
+
oauthProviders: oauth.enabled ? oauth.providers : [],
|
|
154
|
+
oauthDefaultProvider: oauth.enabled ? oauth.defaultProvider : null
|
|
190
155
|
};
|
|
191
156
|
}
|
|
192
157
|
}
|
package/src/test/playwright.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { DEV_AUTH_SECRET_HEADER } from "@jskit-ai/auth-core/server/devAuth";
|
|
2
2
|
import { AUTH_PATHS } from "@jskit-ai/auth-core/shared/authPaths";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
function defaultLocalBaseUrl() {
|
|
5
|
+
const port = String(process.env.PLAYWRIGHT_PORT || "").trim() || "4173";
|
|
6
|
+
return `http://127.0.0.1:${port}`;
|
|
7
|
+
}
|
|
5
8
|
|
|
6
9
|
function resolveLocalBaseUrl(value) {
|
|
7
|
-
const baseUrl = String(value ||
|
|
10
|
+
const baseUrl = String(value || defaultLocalBaseUrl()).trim().replace(/\/+$/u, "");
|
|
8
11
|
let url;
|
|
9
12
|
try {
|
|
10
13
|
url = new URL(baseUrl);
|
|
@@ -42,7 +45,7 @@ async function loginAsExistingUser(page, {
|
|
|
42
45
|
email = "",
|
|
43
46
|
userId = "",
|
|
44
47
|
secret = process.env.AUTH_DEV_BYPASS_SECRET,
|
|
45
|
-
baseURL = process.env.PLAYWRIGHT_BASE_URL ||
|
|
48
|
+
baseURL = process.env.PLAYWRIGHT_BASE_URL || defaultLocalBaseUrl()
|
|
46
49
|
} = {}) {
|
|
47
50
|
const context = page?.context?.();
|
|
48
51
|
if (!context?.request) {
|
|
@@ -93,6 +93,52 @@ for (const authenticated of [true, false]) {
|
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
test("an open sign-in page forwards once a session starts elsewhere", async (t) => {
|
|
97
|
+
const state = createMinimalLoginViewState("/w/acme/bookings?date=2026-09-16");
|
|
98
|
+
const destinations = [];
|
|
99
|
+
const originalWindow = globalThis.window;
|
|
100
|
+
globalThis.window = {
|
|
101
|
+
location: {
|
|
102
|
+
href: "https://example.com/auth/login",
|
|
103
|
+
replace: (path) => destinations.push(path)
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
t.after(() => {
|
|
107
|
+
if (originalWindow === undefined) delete globalThis.window;
|
|
108
|
+
else globalThis.window = originalWindow;
|
|
109
|
+
clearAuthCsrfTokenCache();
|
|
110
|
+
});
|
|
111
|
+
const sessions = [
|
|
112
|
+
() => createJsonResponse({ authenticated: false, csrfToken: "csrf-signed-out" }),
|
|
113
|
+
() => {
|
|
114
|
+
throw new TypeError("Failed to fetch");
|
|
115
|
+
},
|
|
116
|
+
() => createJsonResponse({ authenticated: true, csrfToken: "csrf-signed-in" })
|
|
117
|
+
];
|
|
118
|
+
const fetch = t.mock.method(globalThis, "fetch", async () => sessions.shift()());
|
|
119
|
+
const actions = useLoginViewActions({
|
|
120
|
+
state,
|
|
121
|
+
validation: {},
|
|
122
|
+
queryClient: {
|
|
123
|
+
fetchQuery: ({ queryFn }) => queryFn()
|
|
124
|
+
},
|
|
125
|
+
errorRuntime: { report: (entry) => assert.fail(entry.message) }
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await actions.redirectIfSignedIn();
|
|
129
|
+
await actions.redirectIfSignedIn();
|
|
130
|
+
assert.deepEqual(destinations, [], "a signed-out or offline check keeps the form");
|
|
131
|
+
|
|
132
|
+
state.loading.value = true;
|
|
133
|
+
await actions.redirectIfSignedIn();
|
|
134
|
+
assert.equal(fetch.mock.callCount(), 2, "no check runs while a sign-in is in progress");
|
|
135
|
+
|
|
136
|
+
state.loading.value = false;
|
|
137
|
+
await actions.redirectIfSignedIn();
|
|
138
|
+
assert.deepEqual(destinations, [state.requestedReturnTo.value]);
|
|
139
|
+
assert.equal(state.errorMessage.value, "");
|
|
140
|
+
});
|
|
141
|
+
|
|
96
142
|
test("useLoginViewActions preserves the intended destination when starting OAuth sign-in", () => {
|
|
97
143
|
const state = createMinimalLoginViewState("/w/acme/workouts/2026-05-07?tab=chart");
|
|
98
144
|
const assignedTargets = [];
|
|
@@ -3,11 +3,13 @@ import { readFileSync, statSync } from "node:fs";
|
|
|
3
3
|
import test from "node:test";
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
DEFAULT_PREVIEW_IDENTITY_PROTOCOL,
|
|
7
7
|
executeManagedPreviewIdentityRequest
|
|
8
8
|
} from "../src/server/managedPreviewIdentity.js";
|
|
9
9
|
|
|
10
10
|
const SECRET = "a".repeat(64);
|
|
11
|
+
const HOST_PROTOCOL = "example-host.preview-identity.command.v1";
|
|
12
|
+
const HOST_OPTIONS = Object.freeze({ protocol: HOST_PROTOCOL, localHostnames: [/^preview-[a-f0-9]{12}$/u] });
|
|
11
13
|
|
|
12
14
|
function jsonResponse(payload, { cookie = "", status = 200 } = {}) {
|
|
13
15
|
return new Response(JSON.stringify(payload), {
|
|
@@ -19,22 +21,35 @@ function jsonResponse(payload, { cookie = "", status = 200 } = {}) {
|
|
|
19
21
|
});
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
test("managed preview identity
|
|
23
|
-
assert.equal(
|
|
24
|
+
test("managed preview identity answers only the protocol id its host names", async () => {
|
|
25
|
+
assert.equal(DEFAULT_PREVIEW_IDENTITY_PROTOCOL, "jskit.preview-identity.command.v1");
|
|
24
26
|
|
|
25
27
|
const result = await executeManagedPreviewIdentityRequest({
|
|
26
28
|
operation: "logout",
|
|
27
|
-
protocol: "
|
|
29
|
+
protocol: "retired.preview-identity.command.v1",
|
|
28
30
|
requestId: "retired-protocol",
|
|
29
31
|
target: { origin: "http://localhost:3000" }
|
|
30
32
|
}, {
|
|
33
|
+
...HOST_OPTIONS,
|
|
31
34
|
env: {},
|
|
32
35
|
fetchImpl: async () => assert.fail("fetch must not run")
|
|
33
36
|
});
|
|
34
37
|
|
|
35
38
|
assert.equal(result.ok, false);
|
|
36
|
-
assert.equal(result.protocol,
|
|
39
|
+
assert.equal(result.protocol, HOST_PROTOCOL);
|
|
37
40
|
assert.equal(result.code, "jskit_managed_preview_identity_protocol_invalid");
|
|
41
|
+
|
|
42
|
+
const unnamed = await executeManagedPreviewIdentityRequest({
|
|
43
|
+
operation: "logout",
|
|
44
|
+
protocol: HOST_PROTOCOL,
|
|
45
|
+
requestId: "default-protocol",
|
|
46
|
+
target: { origin: "http://localhost:3000" }
|
|
47
|
+
}, {
|
|
48
|
+
env: {},
|
|
49
|
+
fetchImpl: async () => assert.fail("fetch must not run")
|
|
50
|
+
});
|
|
51
|
+
assert.equal(unnamed.protocol, DEFAULT_PREVIEW_IDENTITY_PROTOCOL);
|
|
52
|
+
assert.equal(unnamed.code, "jskit_managed_preview_identity_protocol_invalid");
|
|
38
53
|
});
|
|
39
54
|
|
|
40
55
|
test("managed preview identity signs out before selecting an existing application user", async () => {
|
|
@@ -51,14 +66,15 @@ test("managed preview identity signs out before selecting an existing applicatio
|
|
|
51
66
|
];
|
|
52
67
|
const result = await executeManagedPreviewIdentityRequest({
|
|
53
68
|
operation: "login-as",
|
|
54
|
-
protocol:
|
|
69
|
+
protocol: HOST_PROTOCOL,
|
|
55
70
|
requestId: "request-1",
|
|
56
71
|
subject: {
|
|
57
72
|
kind: "selector",
|
|
58
73
|
selector: { type: "email", value: "ada@example.com" }
|
|
59
74
|
},
|
|
60
|
-
target: { origin: "http://
|
|
75
|
+
target: { origin: "http://preview-deadbeefcafe" }
|
|
61
76
|
}, {
|
|
77
|
+
...HOST_OPTIONS,
|
|
62
78
|
env: {
|
|
63
79
|
AUTH_DEV_BYPASS_ENABLED: "true",
|
|
64
80
|
AUTH_DEV_BYPASS_SECRET: SECRET
|
|
@@ -70,6 +86,7 @@ test("managed preview identity signs out before selecting an existing applicatio
|
|
|
70
86
|
});
|
|
71
87
|
|
|
72
88
|
assert.equal(result.ok, true);
|
|
89
|
+
assert.equal(result.protocol, HOST_PROTOCOL);
|
|
73
90
|
assert.equal(result.identity.email, "ada@example.com");
|
|
74
91
|
assert.equal(result.signedOut, false);
|
|
75
92
|
assert.deepEqual(calls.map(({ href }) => new URL(href).pathname), [
|
|
@@ -81,22 +98,32 @@ test("managed preview identity signs out before selecting an existing applicatio
|
|
|
81
98
|
assert.equal(JSON.parse(calls[2].options.body).email, "ada@example.com");
|
|
82
99
|
});
|
|
83
100
|
|
|
101
|
+
test("managed preview identity requires the application's environment instead of reporting a disabled exchange", async () => {
|
|
102
|
+
await assert.rejects(executeManagedPreviewIdentityRequest({
|
|
103
|
+
operation: "logout",
|
|
104
|
+
protocol: HOST_PROTOCOL,
|
|
105
|
+
requestId: "request-env",
|
|
106
|
+
target: { origin: "http://localhost:3000" }
|
|
107
|
+
}, { ...HOST_OPTIONS, fetchImpl: async () => assert.fail("fetch must not run") }), /requires the application's environment/u);
|
|
108
|
+
});
|
|
109
|
+
|
|
84
110
|
test("managed preview identity rejects a disabled or non-local exchange", async () => {
|
|
85
111
|
const disabled = await executeManagedPreviewIdentityRequest({
|
|
86
112
|
operation: "logout",
|
|
87
|
-
protocol:
|
|
113
|
+
protocol: HOST_PROTOCOL,
|
|
88
114
|
requestId: "request-2",
|
|
89
115
|
target: { origin: "http://localhost:3000" }
|
|
90
|
-
}, { env: {}, fetchImpl: async () => assert.fail("fetch must not run") });
|
|
116
|
+
}, { ...HOST_OPTIONS, env: {}, fetchImpl: async () => assert.fail("fetch must not run") });
|
|
91
117
|
assert.equal(disabled.ok, false);
|
|
92
118
|
assert.equal(disabled.code, "jskit_managed_preview_identity_disabled");
|
|
93
119
|
|
|
94
120
|
const remote = await executeManagedPreviewIdentityRequest({
|
|
95
121
|
operation: "logout",
|
|
96
|
-
protocol:
|
|
122
|
+
protocol: HOST_PROTOCOL,
|
|
97
123
|
requestId: "request-3",
|
|
98
124
|
target: { origin: "https://example.com" }
|
|
99
125
|
}, {
|
|
126
|
+
...HOST_OPTIONS,
|
|
100
127
|
env: {
|
|
101
128
|
AUTH_DEV_BYPASS_ENABLED: "true",
|
|
102
129
|
AUTH_DEV_BYPASS_SECRET: SECRET
|
|
@@ -105,6 +132,48 @@ test("managed preview identity rejects a disabled or non-local exchange", async
|
|
|
105
132
|
});
|
|
106
133
|
assert.equal(remote.ok, false);
|
|
107
134
|
assert.equal(remote.code, "jskit_managed_preview_identity_target_invalid");
|
|
135
|
+
|
|
136
|
+
const unlisted = await executeManagedPreviewIdentityRequest({
|
|
137
|
+
operation: "logout",
|
|
138
|
+
protocol: DEFAULT_PREVIEW_IDENTITY_PROTOCOL,
|
|
139
|
+
requestId: "request-4",
|
|
140
|
+
target: { origin: "http://preview-deadbeefcafe" }
|
|
141
|
+
}, {
|
|
142
|
+
env: {
|
|
143
|
+
AUTH_DEV_BYPASS_ENABLED: "true",
|
|
144
|
+
AUTH_DEV_BYPASS_SECRET: SECRET
|
|
145
|
+
},
|
|
146
|
+
fetchImpl: async () => assert.fail("fetch must not run")
|
|
147
|
+
});
|
|
148
|
+
assert.equal(unlisted.code, "jskit_managed_preview_identity_target_invalid",
|
|
149
|
+
"a hostname the application does not list is not local");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("managed preview identity accepts the dev auth secret auth-core accepts", async () => {
|
|
153
|
+
const request = {
|
|
154
|
+
operation: "logout",
|
|
155
|
+
protocol: HOST_PROTOCOL,
|
|
156
|
+
requestId: "request-5",
|
|
157
|
+
target: { origin: "http://localhost:3000" }
|
|
158
|
+
};
|
|
159
|
+
const missing = await executeManagedPreviewIdentityRequest(request, {
|
|
160
|
+
...HOST_OPTIONS,
|
|
161
|
+
env: { AUTH_DEV_BYPASS_ENABLED: "true", AUTH_DEV_BYPASS_SECRET: " " },
|
|
162
|
+
fetchImpl: async () => assert.fail("fetch must not run")
|
|
163
|
+
});
|
|
164
|
+
assert.equal(missing.code, "jskit_managed_preview_identity_secret_missing");
|
|
165
|
+
|
|
166
|
+
const responses = [
|
|
167
|
+
jsonResponse({ csrfToken: "csrf-token" }, { cookie: "session=initial; Path=/" }),
|
|
168
|
+
jsonResponse({ ok: true }, { cookie: "session=; Max-Age=0; Path=/" })
|
|
169
|
+
];
|
|
170
|
+
const signedOut = await executeManagedPreviewIdentityRequest(request, {
|
|
171
|
+
...HOST_OPTIONS,
|
|
172
|
+
env: { AUTH_DEV_BYPASS_ENABLED: "true", AUTH_DEV_BYPASS_SECRET: " any-local-secret " },
|
|
173
|
+
fetchImpl: async () => responses.shift()
|
|
174
|
+
});
|
|
175
|
+
assert.equal(signedOut.ok, true);
|
|
176
|
+
assert.deepEqual(signedOut.setCookie, ["session=initial; Path=/", "session=; Max-Age=0; Path=/"]);
|
|
108
177
|
});
|
|
109
178
|
|
|
110
179
|
test("the auth surface pattern ships the managed-preview login as standard", () => {
|
|
@@ -112,13 +181,15 @@ test("the auth surface pattern ships the managed-preview login as standard", ()
|
|
|
112
181
|
const executable = new URL("../patterns/auth-surface/example/tools/preview-identity", import.meta.url);
|
|
113
182
|
|
|
114
183
|
assert.equal(statSync(executable).mode & 0o111, 0o111);
|
|
115
|
-
|
|
184
|
+
const source = readFileSync(executable, "utf8");
|
|
185
|
+
assert.match(source, /runManagedPreviewIdentityCommand\(\{\n {2}protocol: /u);
|
|
186
|
+
assert.match(source, /localHostnames: /u);
|
|
116
187
|
assert.match(pattern, /standard, not optional/u);
|
|
117
188
|
assert.ok(pattern.includes([
|
|
118
189
|
"#### Preview identity",
|
|
119
190
|
"",
|
|
120
191
|
"- Command: `tools/preview-identity`",
|
|
121
|
-
|
|
192
|
+
"- Protocol: `<the host's protocol id>`",
|
|
122
193
|
"- Identity types: `email` `user-id`",
|
|
123
194
|
"- Enabled environment: `AUTH_DEV_BYPASS_ENABLED`",
|
|
124
195
|
"- Secret environment: `AUTH_DEV_BYPASS_SECRET`",
|
package/test/playwright.test.js
CHANGED
|
@@ -20,7 +20,23 @@ function createResponse({ status = 200, body = {} } = {}) {
|
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
function setPlaywrightEnv(context, env) {
|
|
24
|
+
const names = ["PLAYWRIGHT_BASE_URL", "PLAYWRIGHT_PORT"];
|
|
25
|
+
const previous = names.map((name) => [name, process.env[name]]);
|
|
26
|
+
context.after(() => {
|
|
27
|
+
for (const [name, value] of previous) {
|
|
28
|
+
if (value === undefined) delete process.env[name];
|
|
29
|
+
else process.env[name] = value;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
for (const name of names) {
|
|
33
|
+
if (env[name] === undefined) delete process.env[name];
|
|
34
|
+
else process.env[name] = env[name];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("local Playwright login exchanges CSRF and the private secret in the page context", async (context) => {
|
|
39
|
+
setPlaywrightEnv(context, {});
|
|
24
40
|
const calls = [];
|
|
25
41
|
const request = {
|
|
26
42
|
async get(url) {
|
|
@@ -66,6 +82,33 @@ test("local Playwright login exchanges CSRF and the private secret in the page c
|
|
|
66
82
|
]);
|
|
67
83
|
});
|
|
68
84
|
|
|
85
|
+
test("local Playwright login follows PLAYWRIGHT_PORT when no base URL is given", async (context) => {
|
|
86
|
+
const urls = [];
|
|
87
|
+
const request = {
|
|
88
|
+
async get(url) {
|
|
89
|
+
urls.push(url);
|
|
90
|
+
return createResponse({ body: { csrfToken: "csrf-test" } });
|
|
91
|
+
},
|
|
92
|
+
async post(url) {
|
|
93
|
+
urls.push(url);
|
|
94
|
+
return createResponse({ body: { ok: true, userId: "7" } });
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const page = {
|
|
98
|
+
context() {
|
|
99
|
+
return { request };
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
setPlaywrightEnv(context, { PLAYWRIGHT_PORT: "4183" });
|
|
103
|
+
|
|
104
|
+
await loginAsExistingUser(page, { email: "ada@example.com", secret: "private-test-secret" });
|
|
105
|
+
|
|
106
|
+
assert.deepEqual(urls, [
|
|
107
|
+
"http://127.0.0.1:4183/api/session",
|
|
108
|
+
"http://127.0.0.1:4183/api/dev-auth/login-as"
|
|
109
|
+
]);
|
|
110
|
+
});
|
|
111
|
+
|
|
69
112
|
test("local Playwright login refuses to send the private secret to a managed URL", async () => {
|
|
70
113
|
let requestCount = 0;
|
|
71
114
|
const page = {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import {
|
|
3
|
+
import { ActionProvider } from "@jskit-ai/kernel/server/actions";
|
|
4
|
+
import { EventProvider } from "@jskit-ai/kernel/server/runtime";
|
|
5
|
+
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
4
6
|
import { createCapabilityRuntime } from "@jskit-ai/kernel/shared/capabilities";
|
|
5
7
|
import { AuthFeature } from "@jskit-ai/auth-core/server/providers/AuthFeature";
|
|
6
8
|
import { HttpProvider } from "@jskit-ai/kernel/server/http";
|
|
@@ -61,7 +63,7 @@ function createAuthService(events = []) {
|
|
|
61
63
|
actor: { displayName: "Ada", email: "ada@example.com" }
|
|
62
64
|
};
|
|
63
65
|
},
|
|
64
|
-
async devLoginAs(
|
|
66
|
+
async devLoginAs(input) {
|
|
65
67
|
return {
|
|
66
68
|
session: { access_token: "dev-a", refresh_token: "dev-r" },
|
|
67
69
|
actor: { id: input.userId || "7", displayName: "Dev Ada", email: "ada@example.com" }
|
|
@@ -104,11 +106,12 @@ async function startAuthWeb({ env = {}, authService = null } = {}) {
|
|
|
104
106
|
const fastify = createFastifyStub();
|
|
105
107
|
const runtime = createCapabilityRuntime({
|
|
106
108
|
inputs: {
|
|
109
|
+
"runtime.logger": silentLogger,
|
|
107
110
|
"auth.service": authService || createAuthService(),
|
|
108
111
|
"runtime.env": { NODE_ENV: "test", ...env },
|
|
109
112
|
"runtime.fastify": fastify
|
|
110
113
|
},
|
|
111
|
-
providers: [
|
|
114
|
+
providers: [EventProvider, ActionProvider, HttpProvider, AuthFeature, AuthWebFeature]
|
|
112
115
|
});
|
|
113
116
|
await runtime.start();
|
|
114
117
|
return { fastify, runtime };
|