@jskit-ai/auth-web 0.2.16 → 0.2.18
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/package.json +6 -6
- package/src/test/playwright.js +36 -15
- package/test/playwright.test.js +82 -22
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `loginRequestAsExistingUser(request, options)` (new, `@jskit-ai/auth-web/test/playwright`) signs a Playwright APIRequestContext in, as `loginAsExistingUser()` signs a page's context in: a browser test `signIn` module uses it to sign each worker in once.
|
|
6
|
+
- `loginAsExistingUser(page, { email | userId, secret })` from `@jskit-ai/auth-web/test/playwright` signs in on the test's own server: with no `baseURL`, its requests go to the page context's `baseURL`, so each Playwright worker signs in on its own server (`browserTestConfig()`), instead of `PLAYWRIGHT_BASE_URL` or `127.0.0.1:${PLAYWRIGHT_PORT || 4173}`, which sent every worker's sign-in to the first worker's server. The dev-auth secret still goes only to a local server: the one a given `baseURL` names, or the one that answered the session request; a managed `PLAYWRIGHT_BASE_URL` is still refused before any request.
|
|
5
7
|
- The `auth/auth-surface` pattern shows the app's identity on its sign-in, reset and sign-out screens: its pages render the app's view wrappers (`src/views/auth/LoginView.vue`, `ResetPasswordView.vue`, `SignOutView.vue`) instead of the default views, and each wrapper fills the `brand` slot with the new `src/components/AppBrand.vue`, the app's name as `index.html`'s `<title>` spells it (a `text-title-large` wordmark in the wide pane, a `text-title-medium` row on phones, no heading). The foundations' rename step renames it with the `<title>`. Upgrading: an app whose `src/pages/auth/*.vue` render the default views directly makes them render its wrappers, and gives the wrappers its brand.
|
|
6
8
|
- The app bar's account control is the avatar alone, a round icon button named by its `aria-label`, on compact and medium windows (below 840dp), so a long name no longer squeezes the bar's title to "Setti…" beside its actions; on expanded windows the name beside the avatar ends in an ellipsis past 12rem. It reads the shell's window size classes (`useShellWindowSizeClass()`).
|
|
7
9
|
- Sign in and Register are a Material 3 segmented button (`role="radiogroup"`, labelled by the new message key `jskit.auth.login.mode.label`, "Sign in or register"): one `outline`, two equal segments, the selected one on `secondary-container` with a check. It shows only when both are offered; an app or a remembered account with Sign in alone shows no switcher. Register no longer ends with "Already have an account? Back to sign in", which repeated the Sign in segment, so the message key `jskit.auth.login.hasAccount` is gone.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-web",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"./locales/en.json": "./locales/en.json"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@jskit-ai/auth-core": "0.2.
|
|
29
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
30
|
-
"@jskit-ai/kernel": "0.2.
|
|
31
|
-
"@jskit-ai/shell-web": "0.2.
|
|
28
|
+
"@jskit-ai/auth-core": "0.2.13",
|
|
29
|
+
"@jskit-ai/http-runtime": "0.2.13",
|
|
30
|
+
"@jskit-ai/kernel": "0.2.13",
|
|
31
|
+
"@jskit-ai/shell-web": "0.2.16",
|
|
32
32
|
"@tanstack/vue-query": "^5.90.5",
|
|
33
33
|
"pinia": "^4.0.3",
|
|
34
34
|
"vue": "^3.5.44",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
},
|
|
87
87
|
{
|
|
88
88
|
"subpath": "./test/playwright",
|
|
89
|
-
"summary": "Exports the localhost-only Playwright
|
|
89
|
+
"summary": "Exports the localhost-only Playwright helpers that create a dev-auth session in the tested browser context or in an API request context, such as a browser test worker's sign-in."
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
"subpath": "./server/managedPreviewIdentity",
|
package/src/test/playwright.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
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
|
-
function defaultLocalBaseUrl() {
|
|
5
|
-
const port = String(process.env.PLAYWRIGHT_PORT || "").trim() || "4173";
|
|
6
|
-
return `http://127.0.0.1:${port}`;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
4
|
function resolveLocalBaseUrl(value) {
|
|
10
|
-
const baseUrl = String(value
|
|
5
|
+
const baseUrl = String(value).trim().replace(/\/+$/u, "");
|
|
11
6
|
let url;
|
|
12
7
|
try {
|
|
13
8
|
url = new URL(baseUrl);
|
|
@@ -41,15 +36,22 @@ async function responseBody(response) {
|
|
|
41
36
|
return body ? ` ${body}` : "";
|
|
42
37
|
}
|
|
43
38
|
|
|
44
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Signs `request`, a Playwright APIRequestContext, in as an existing user through the local app's dev-auth route: a
|
|
41
|
+
* page context's `request`, or the one a browserTestConfig() `signIn` module is given to sign a worker in once. Without
|
|
42
|
+
* `baseURL`, it signs in on the context's own baseURL: under browserTestConfig() each worker's own server. The private
|
|
43
|
+
* secret is only ever sent to a local app URL; a managed target (PLAYWRIGHT_BASE_URL) is refused before any request.
|
|
44
|
+
* @param {import("@playwright/test").APIRequestContext} request
|
|
45
|
+
* @param {{ email?: string, userId?: string, secret?: string, baseURL?: string }} [options]
|
|
46
|
+
*/
|
|
47
|
+
async function loginRequestAsExistingUser(request, {
|
|
45
48
|
email = "",
|
|
46
49
|
userId = "",
|
|
47
50
|
secret = process.env.AUTH_DEV_BYPASS_SECRET,
|
|
48
|
-
baseURL =
|
|
51
|
+
baseURL = ""
|
|
49
52
|
} = {}) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw new Error("loginAsExistingUser requires a Playwright page.");
|
|
53
|
+
if (typeof request?.get !== "function") {
|
|
54
|
+
throw new Error("loginRequestAsExistingUser requires a Playwright APIRequestContext.");
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
const normalizedSecret = String(secret || "").trim();
|
|
@@ -57,12 +59,18 @@ async function loginAsExistingUser(page, {
|
|
|
57
59
|
throw new Error("loginAsExistingUser requires AUTH_DEV_BYPASS_SECRET.");
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
const
|
|
62
|
+
const givenBaseUrl = baseURL ? resolveLocalBaseUrl(baseURL) : "";
|
|
63
|
+
if (!givenBaseUrl && process.env.PLAYWRIGHT_BASE_URL) {
|
|
64
|
+
resolveLocalBaseUrl(process.env.PLAYWRIGHT_BASE_URL);
|
|
65
|
+
}
|
|
61
66
|
const identity = resolveIdentity({ email, userId });
|
|
62
|
-
|
|
67
|
+
// A path alone resolves against the context's own baseURL.
|
|
68
|
+
const sessionResponse = await request.get(`${givenBaseUrl}${AUTH_PATHS.SESSION}`);
|
|
63
69
|
if (!sessionResponse.ok()) {
|
|
64
70
|
throw new Error(`Session bootstrap failed: ${sessionResponse.status()}${await responseBody(sessionResponse)}`);
|
|
65
71
|
}
|
|
72
|
+
// The secret goes only to the local server that answered the session request.
|
|
73
|
+
const localBaseUrl = givenBaseUrl || resolveLocalBaseUrl(new URL(sessionResponse.url()).origin);
|
|
66
74
|
|
|
67
75
|
const session = await sessionResponse.json();
|
|
68
76
|
const csrfToken = String(session?.csrfToken || "").trim();
|
|
@@ -70,7 +78,7 @@ async function loginAsExistingUser(page, {
|
|
|
70
78
|
throw new Error("Session bootstrap did not return csrfToken.");
|
|
71
79
|
}
|
|
72
80
|
|
|
73
|
-
const loginResponse = await
|
|
81
|
+
const loginResponse = await request.post(`${localBaseUrl}${AUTH_PATHS.DEV_LOGIN_AS}`, {
|
|
74
82
|
data: identity,
|
|
75
83
|
headers: {
|
|
76
84
|
"csrf-token": csrfToken,
|
|
@@ -84,4 +92,17 @@ async function loginAsExistingUser(page, {
|
|
|
84
92
|
return loginResponse.json();
|
|
85
93
|
}
|
|
86
94
|
|
|
87
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Signs `page`'s context in as an existing user, as `loginRequestAsExistingUser()` signs its `request` in.
|
|
97
|
+
* @param {import("@playwright/test").Page} page
|
|
98
|
+
* @param {{ email?: string, userId?: string, secret?: string, baseURL?: string }} [options]
|
|
99
|
+
*/
|
|
100
|
+
async function loginAsExistingUser(page, options = {}) {
|
|
101
|
+
const context = page?.context?.();
|
|
102
|
+
if (!context?.request) {
|
|
103
|
+
throw new Error("loginAsExistingUser requires a Playwright page.");
|
|
104
|
+
}
|
|
105
|
+
return loginRequestAsExistingUser(context.request, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { loginAsExistingUser, loginRequestAsExistingUser };
|
package/test/playwright.test.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
+
import { createServer } from "node:http";
|
|
2
3
|
import test from "node:test";
|
|
4
|
+
import { request as playwrightRequest } from "@playwright/test";
|
|
3
5
|
import { DEV_AUTH_SECRET_HEADER } from "@jskit-ai/auth-core/server/devAuth";
|
|
4
|
-
import { loginAsExistingUser } from "../src/test/playwright.js";
|
|
6
|
+
import { loginAsExistingUser, loginRequestAsExistingUser } from "../src/test/playwright.js";
|
|
5
7
|
|
|
6
|
-
function createResponse({ status = 200, body = {} } = {}) {
|
|
8
|
+
function createResponse({ status = 200, body = {}, url = "" } = {}) {
|
|
7
9
|
return {
|
|
10
|
+
url() {
|
|
11
|
+
return url;
|
|
12
|
+
},
|
|
8
13
|
ok() {
|
|
9
14
|
return status >= 200 && status < 300;
|
|
10
15
|
},
|
|
@@ -38,10 +43,11 @@ function setPlaywrightEnv(context, env) {
|
|
|
38
43
|
test("local Playwright login exchanges CSRF and the private secret in the page context", async (context) => {
|
|
39
44
|
setPlaywrightEnv(context, {});
|
|
40
45
|
const calls = [];
|
|
46
|
+
// As Playwright's request context does, a path alone resolves against the context's baseURL.
|
|
41
47
|
const request = {
|
|
42
48
|
async get(url) {
|
|
43
49
|
calls.push({ method: "GET", url });
|
|
44
|
-
return createResponse({ body: { csrfToken: "csrf-test" } });
|
|
50
|
+
return createResponse({ body: { csrfToken: "csrf-test" }, url: new URL(url, "http://127.0.0.1:4173").href });
|
|
45
51
|
},
|
|
46
52
|
async post(url, options) {
|
|
47
53
|
calls.push({ method: "POST", url, options });
|
|
@@ -66,7 +72,7 @@ test("local Playwright login exchanges CSRF and the private secret in the page c
|
|
|
66
72
|
assert.deepEqual(calls, [
|
|
67
73
|
{
|
|
68
74
|
method: "GET",
|
|
69
|
-
url: "
|
|
75
|
+
url: "/api/session"
|
|
70
76
|
},
|
|
71
77
|
{
|
|
72
78
|
method: "POST",
|
|
@@ -82,31 +88,85 @@ test("local Playwright login exchanges CSRF and the private secret in the page c
|
|
|
82
88
|
]);
|
|
83
89
|
});
|
|
84
90
|
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
/** A worker's app server: answers the session and dev-login routes and records what reached it. */
|
|
92
|
+
async function workerServer(name) {
|
|
93
|
+
const requests = [];
|
|
94
|
+
const server = createServer((request, response) => {
|
|
95
|
+
requests.push(`${request.method} ${request.url}${request.headers[DEV_AUTH_SECRET_HEADER] ? " with the secret" : ""}`);
|
|
96
|
+
response.setHeader("content-type", "application/json");
|
|
97
|
+
if (request.method === "POST") response.setHeader("set-cookie", `session=${name.replace(" ", "-")}; Path=/; HttpOnly; SameSite=Lax`);
|
|
98
|
+
response.end(JSON.stringify(request.method === "GET" ? { csrfToken: `csrf-${name}` } : { ok: true, server: name }));
|
|
99
|
+
});
|
|
100
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
101
|
+
return { requests, port: server.address().port, close: () => new Promise((resolve) => server.close(resolve)) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
test("each worker signs in on its own server, its context's baseURL, whatever PLAYWRIGHT_PORT says", async (context) => {
|
|
105
|
+
const first = await workerServer("worker 0");
|
|
106
|
+
const second = await workerServer("worker 1");
|
|
107
|
+
// The runner's base port is worker 0's; worker 1's config gives its contexts the next server.
|
|
108
|
+
setPlaywrightEnv(context, { PLAYWRIGHT_PORT: String(first.port) });
|
|
109
|
+
const request = await playwrightRequest.newContext({ baseURL: `http://127.0.0.1:${second.port}` });
|
|
110
|
+
context.after(async () => {
|
|
111
|
+
await request.dispose();
|
|
112
|
+
await Promise.all([first.close(), second.close()]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const result = await loginAsExistingUser({ context: () => ({ request }) }, { email: "ada@example.com", secret: "private-test-secret" });
|
|
116
|
+
|
|
117
|
+
assert.deepEqual(result, { ok: true, server: "worker 1" });
|
|
118
|
+
assert.deepEqual(second.requests, ["GET /api/session", "POST /api/dev-auth/login-as with the secret"]);
|
|
119
|
+
assert.deepEqual(first.requests, []);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("a browser test worker's request context signs in once and keeps the session in its storage state", async (context) => {
|
|
123
|
+
setPlaywrightEnv(context, {});
|
|
124
|
+
const server = await workerServer("worker 0");
|
|
125
|
+
const request = await playwrightRequest.newContext({ baseURL: `http://127.0.0.1:${server.port}` });
|
|
126
|
+
context.after(async () => {
|
|
127
|
+
await request.dispose();
|
|
128
|
+
await server.close();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const result = await loginRequestAsExistingUser(request, { email: "ada@example.com", secret: "private-test-secret" });
|
|
132
|
+
|
|
133
|
+
assert.deepEqual(result, { ok: true, server: "worker 0" });
|
|
134
|
+
assert.deepEqual(server.requests, ["GET /api/session", "POST /api/dev-auth/login-as with the secret"]);
|
|
135
|
+
// What browserTestConfig() leaves for the worker's tests: the signed-in session's cookie.
|
|
136
|
+
assert.deepEqual((await request.storageState()).cookies.map(({ name, value }) => `${name}=${value}`), ["session=worker-0"]);
|
|
137
|
+
await assert.rejects(loginRequestAsExistingUser({}, { email: "ada@example.com", secret: "private-test-secret" }),
|
|
138
|
+
/requires a Playwright APIRequestContext/u);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("local Playwright login refuses a managed target before any request when no base URL is given", async (context) => {
|
|
142
|
+
setPlaywrightEnv(context, { PLAYWRIGHT_BASE_URL: "https://preview.example.test" });
|
|
143
|
+
let requestCount = 0;
|
|
144
|
+
const page = { context: () => ({ request: { async get() { requestCount += 1; } } }) };
|
|
145
|
+
|
|
146
|
+
await assert.rejects(
|
|
147
|
+
loginAsExistingUser(page, { email: "ada@example.com", secret: "private-test-secret" }),
|
|
148
|
+
/only sends the dev-auth secret to a local app URL/u
|
|
149
|
+
);
|
|
150
|
+
assert.equal(requestCount, 0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("local Playwright login sends the secret only to a local server that answered the session request", async (context) => {
|
|
154
|
+
setPlaywrightEnv(context, {});
|
|
155
|
+
const posts = [];
|
|
87
156
|
const request = {
|
|
88
157
|
async get(url) {
|
|
89
|
-
|
|
90
|
-
return createResponse({ body: { csrfToken: "csrf-test" } });
|
|
158
|
+
return createResponse({ body: { csrfToken: "csrf-test" }, url: new URL(url, "https://app.example.test").href });
|
|
91
159
|
},
|
|
92
160
|
async post(url) {
|
|
93
|
-
|
|
94
|
-
return createResponse({ body: { ok: true, userId: "7" } });
|
|
161
|
+
posts.push(url);
|
|
95
162
|
}
|
|
96
163
|
};
|
|
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
164
|
|
|
106
|
-
assert.
|
|
107
|
-
"
|
|
108
|
-
|
|
109
|
-
|
|
165
|
+
await assert.rejects(
|
|
166
|
+
loginAsExistingUser({ context: () => ({ request }) }, { email: "ada@example.com", secret: "private-test-secret" }),
|
|
167
|
+
/only sends the dev-auth secret to a local app URL/u
|
|
168
|
+
);
|
|
169
|
+
assert.deepEqual(posts, []);
|
|
110
170
|
});
|
|
111
171
|
|
|
112
172
|
test("local Playwright login refuses to send the private secret to a managed URL", async () => {
|