@jskit-ai/auth-web 0.2.34 → 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.
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import process from "node:process";
|
|
1
2
|
import { fileURLToPath } from "node:url";
|
|
2
3
|
import { defineConfig } from "vite";
|
|
3
4
|
import vue from "@vitejs/plugin-vue";
|
|
4
5
|
|
|
6
|
+
// A test with a real auth server behind the page names it here, and /api goes to it; the other tests answer /api in
|
|
7
|
+
// the page themselves.
|
|
8
|
+
const apiServer = process.env.JSKIT_AUTH_WEB_FIXTURE_API_SERVER;
|
|
9
|
+
|
|
5
10
|
export default defineConfig({
|
|
6
11
|
root: fileURLToPath(new URL("./", import.meta.url)),
|
|
7
12
|
// The auth-surface pattern's pages and view wrappers import the app's own files through "@/", as in an app.
|
|
8
13
|
resolve: { alias: { "@": fileURLToPath(new URL("../../patterns/auth-surface/example/src", import.meta.url)) } },
|
|
9
14
|
plugins: [vue()],
|
|
10
|
-
server: { host: "127.0.0.1", strictPort: true }
|
|
15
|
+
server: { host: "127.0.0.1", strictPort: true, ...(apiServer ? { proxy: { "/api": apiServer } } : {}) }
|
|
11
16
|
});
|
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.35",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"uqr": "0.1.3"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@jskit-ai/auth-core": "0.2.
|
|
34
|
+
"@jskit-ai/auth-core": "0.2.23",
|
|
35
35
|
"@jskit-ai/http-runtime": "0.2.22",
|
|
36
36
|
"@jskit-ai/kernel": "0.2.22",
|
|
37
37
|
"@jskit-ai/shell-web": "0.2.31",
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import { once } from "node:events";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import process from "node:process";
|
|
8
|
+
import { after, before, test } from "node:test";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { chromium, expect } from "@playwright/test";
|
|
11
|
+
import Fastify from "fastify";
|
|
12
|
+
import { noAppProfiles } from "@jskit-ai/auth-core/server/noAppProfiles";
|
|
13
|
+
import { AuthExtensionsProvider } from "@jskit-ai/auth-core/server/providers/AuthExtensionsProvider";
|
|
14
|
+
import { AuthFeature } from "@jskit-ai/auth-core/server/providers/AuthFeature";
|
|
15
|
+
import { AuthPolicyProvider } from "@jskit-ai/auth-core/server/providers/AuthPolicyProvider";
|
|
16
|
+
import { ActionProvider } from "@jskit-ai/kernel/server/actions";
|
|
17
|
+
import { HttpProvider } from "@jskit-ai/kernel/server/http";
|
|
18
|
+
import { EventProvider } from "@jskit-ai/kernel/server/runtime";
|
|
19
|
+
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
20
|
+
import { createCapabilityRuntime } from "@jskit-ai/kernel/shared/capabilities";
|
|
21
|
+
import { createLocalFileBackend } from "../../auth-provider-local-core/src/server/lib/index.js";
|
|
22
|
+
import { createService, oidcProviderEntry, startOidcProvider } from "../../auth-provider-local-core/test-support/oauthHarness.js";
|
|
23
|
+
import {
|
|
24
|
+
createChromiumLaunchOptions,
|
|
25
|
+
reservePort,
|
|
26
|
+
startViteFixture,
|
|
27
|
+
stopProcess
|
|
28
|
+
} from "../../../tooling/testUtils/browserFixture.mjs";
|
|
29
|
+
import { AuthWebFeature } from "../src/server/AuthWebFeature.js";
|
|
30
|
+
|
|
31
|
+
// The real sign-in page, served on http://localhost:PORT, with the real auth routes, auth policy and local auth behind
|
|
32
|
+
// its /api, all with AUTH_COOKIE_SAME_SITE=strict. The Google stand-in and the webmail page are on 127.0.0.1, another
|
|
33
|
+
// site, and the way back from either starts on its own page (the stand-in's Continue button, the webmail's link), so
|
|
34
|
+
// it is a cross-site navigation, on which a browser sends no SameSite=Strict cookie. (Chromium still sends them when
|
|
35
|
+
// the provider only redirects the app's own navigation back, without a page of its own.)
|
|
36
|
+
const RUN = process.env.JSKIT_AUTH_WEB_BROWSER_INTEGRATION === "1";
|
|
37
|
+
const options = { skip: !RUN, timeout: 120_000 };
|
|
38
|
+
const STRICT = Object.freeze({ AUTH_COOKIE_SAME_SITE: "strict" });
|
|
39
|
+
const PASSWORD = "correct horse battery staple";
|
|
40
|
+
const SESSION_COOKIES = ["_csrf", "jskit_local_access_token", "jskit_local_refresh_token"];
|
|
41
|
+
|
|
42
|
+
let stack = null;
|
|
43
|
+
const cleanups = [];
|
|
44
|
+
|
|
45
|
+
before(async () => {
|
|
46
|
+
if (!RUN) return;
|
|
47
|
+
const vitePort = await reservePort();
|
|
48
|
+
const appUrl = `http://localhost:${vitePort}`;
|
|
49
|
+
const storeDir = await fs.mkdtemp(path.join(os.tmpdir(), "jskit-auth-web-same-site-"));
|
|
50
|
+
cleanups.push(() => fs.rm(storeDir, { recursive: true, force: true }));
|
|
51
|
+
const issuer = await startOidcProvider({ appUrl, consentPage: true });
|
|
52
|
+
cleanups.push(() => issuer.close());
|
|
53
|
+
const authService = createService({
|
|
54
|
+
backend: createLocalFileBackend({ storeDir }),
|
|
55
|
+
oauthProviders: [oidcProviderEntry(issuer)],
|
|
56
|
+
profileProjector: noAppProfiles,
|
|
57
|
+
env: { APP_PUBLIC_URL: appUrl, ...STRICT }
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const api = Fastify();
|
|
61
|
+
cleanups.push(() => api.close());
|
|
62
|
+
// What reaches the server: each request with the names of the cookies it carried.
|
|
63
|
+
const apiRequests = [];
|
|
64
|
+
api.addHook("onRequest", async (request) => {
|
|
65
|
+
apiRequests.push({ key: `${request.method} ${request.url.split("?")[0]}`, cookies: cookieNames(request.headers.cookie) });
|
|
66
|
+
});
|
|
67
|
+
const runtime = createCapabilityRuntime({
|
|
68
|
+
inputs: {
|
|
69
|
+
"auth.service": authService,
|
|
70
|
+
"runtime.config": {},
|
|
71
|
+
"runtime.env": { NODE_ENV: "test", ...STRICT },
|
|
72
|
+
"runtime.fastify": api,
|
|
73
|
+
"runtime.logger": silentLogger
|
|
74
|
+
},
|
|
75
|
+
providers: [EventProvider, ActionProvider, HttpProvider, AuthExtensionsProvider, AuthFeature, AuthPolicyProvider,
|
|
76
|
+
AuthWebFeature]
|
|
77
|
+
});
|
|
78
|
+
await runtime.start();
|
|
79
|
+
cleanups.push(() => runtime.shutdown());
|
|
80
|
+
await api.listen({ host: "127.0.0.1", port: 0 });
|
|
81
|
+
|
|
82
|
+
const vite = await startViteFixture({
|
|
83
|
+
fixtureRoot: fileURLToPath(new URL("../fixtures/sign-in/", import.meta.url)),
|
|
84
|
+
port: vitePort,
|
|
85
|
+
env: { JSKIT_AUTH_WEB_FIXTURE_API_SERVER: `http://127.0.0.1:${api.server.address().port}` }
|
|
86
|
+
});
|
|
87
|
+
cleanups.push(() => stopProcess(vite));
|
|
88
|
+
const browser = await chromium.launch(createChromiumLaunchOptions());
|
|
89
|
+
cleanups.push(() => browser.close());
|
|
90
|
+
stack = { appUrl, issuerUrl: issuer.issuer, authService, apiRequests, browser };
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
after(async () => {
|
|
94
|
+
for (const cleanup of cleanups.reverse()) {
|
|
95
|
+
await cleanup();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
function cookieNames(header) {
|
|
100
|
+
return String(header || "").split(";").map((pair) => pair.split("=")[0].trim()).filter(Boolean).sort();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A browser of its own, which records every page it loads with the headers it sent. */
|
|
104
|
+
async function openBrowser(t) {
|
|
105
|
+
const context = await stack.browser.newContext();
|
|
106
|
+
t.after(() => context.close());
|
|
107
|
+
const page = await context.newPage();
|
|
108
|
+
page.setDefaultTimeout(15_000);
|
|
109
|
+
page.setDefaultNavigationTimeout(60_000);
|
|
110
|
+
const documents = [];
|
|
111
|
+
const errors = [];
|
|
112
|
+
page.on("pageerror", (error) => errors.push(error.message));
|
|
113
|
+
page.on("request", (request) => {
|
|
114
|
+
if (request.resourceType() === "document") {
|
|
115
|
+
documents.push({ url: new URL(request.url()), headers: request.allHeaders() });
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
return { context, page, documents, errors };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The headers of the one page load of the app that `matches` its address. */
|
|
122
|
+
async function appDocument(documents, matches) {
|
|
123
|
+
const loads = documents.filter(({ url }) => url.origin === new URL(stack.appUrl).origin && matches(url));
|
|
124
|
+
assert.equal(loads.length, 1, `page loads: ${documents.map(({ url }) => url.href).join(", ")}`);
|
|
125
|
+
return loads[0].headers;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function apiRequest(key) {
|
|
129
|
+
const requests = stack.apiRequests.filter((request) => request.key === key);
|
|
130
|
+
assert.equal(requests.length > 0, true, `no ${key} among ${stack.apiRequests.map((request) => request.key).join(", ")}`);
|
|
131
|
+
return requests;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
test("with strict cookies, Google sign-in comes back from the provider's site and completes on the login page", options, async (t) => {
|
|
135
|
+
const { context, page, documents, errors } = await openBrowser(t);
|
|
136
|
+
const attemptSetCookies = [];
|
|
137
|
+
page.on("response", (response) => {
|
|
138
|
+
if (new URL(response.url()).pathname === "/api/oauth/google/start") {
|
|
139
|
+
attemptSetCookies.push(response.headersArray());
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
stack.apiRequests.length = 0;
|
|
143
|
+
await page.goto(`${stack.appUrl}/auth/login?returnTo=/home`);
|
|
144
|
+
await page.getByRole("button", { name: "Continue with Google" }).click();
|
|
145
|
+
await page.waitForURL((url) => url.href.startsWith(stack.issuerUrl));
|
|
146
|
+
await page.getByRole("button", { name: "Continue", exact: true }).click();
|
|
147
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: true");
|
|
148
|
+
assert.equal(new URL(page.url()).pathname, "/home");
|
|
149
|
+
|
|
150
|
+
// The start set the attempt cookie Strict, before the browser left for the provider.
|
|
151
|
+
const [startHeaders] = await Promise.all(attemptSetCookies);
|
|
152
|
+
const attemptCookie = startHeaders.find(({ name, value }) => name.toLowerCase() === "set-cookie" &&
|
|
153
|
+
value.startsWith("jskit_local_oauth_attempt="));
|
|
154
|
+
assert.match(attemptCookie.value, /; SameSite=Strict(;|$)/u);
|
|
155
|
+
|
|
156
|
+
// The way back to the login page started on the provider's page, a cross-site navigation, so it carried none of the
|
|
157
|
+
// app's cookies: neither the attempt nor the CSRF secret the login page had been given.
|
|
158
|
+
const back = await appDocument(documents, (url) => url.pathname === "/auth/login" && url.searchParams.has("code"));
|
|
159
|
+
assert.equal(back["sec-fetch-site"], "cross-site");
|
|
160
|
+
assert.deepEqual(cookieNames(back.cookie), []);
|
|
161
|
+
// The page's own same-origin POST carried the attempt, which completed the sign-in.
|
|
162
|
+
assert.deepEqual(apiRequest("POST /api/oauth/complete").map(({ cookies }) => cookies), [
|
|
163
|
+
["_csrf", "jskit_local_oauth_attempt"]
|
|
164
|
+
]);
|
|
165
|
+
assert.deepEqual(apiRequest("GET /api/session").at(-1).cookies, SESSION_COOKIES);
|
|
166
|
+
|
|
167
|
+
const kept = (await context.cookies(stack.appUrl)).map(({ name, sameSite }) => [name, sameSite]);
|
|
168
|
+
assert.deepEqual(kept.sort(), SESSION_COOKIES.map((name) => [name, "Strict"]));
|
|
169
|
+
assert.deepEqual(errors, []);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/** A webmail page on 127.0.0.1, another site than the app's, with one link into the app. */
|
|
173
|
+
async function startWebmail(t, link) {
|
|
174
|
+
const server = http.createServer((_request, response) => {
|
|
175
|
+
response.setHeader("content-type", "text/html; charset=utf-8");
|
|
176
|
+
response.end(`<!doctype html><title>Inbox</title><a href="${link}">Open your account</a>`);
|
|
177
|
+
});
|
|
178
|
+
server.listen(0, "127.0.0.1");
|
|
179
|
+
await once(server, "listening");
|
|
180
|
+
t.after(async () => {
|
|
181
|
+
server.closeAllConnections();
|
|
182
|
+
server.close();
|
|
183
|
+
await once(server, "close");
|
|
184
|
+
});
|
|
185
|
+
return `http://127.0.0.1:${server.address().port}/`;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
test("with strict cookies, a link from another site opens the app signed in by the page's own requests", options, async (t) => {
|
|
189
|
+
await stack.authService.register({ email: "grace@example.com", password: PASSWORD });
|
|
190
|
+
const webmail = await startWebmail(t, `${stack.appUrl}/home`);
|
|
191
|
+
const { page, documents, errors } = await openBrowser(t);
|
|
192
|
+
await page.goto(`${stack.appUrl}/auth/login?returnTo=/home`);
|
|
193
|
+
await page.getByLabel("Email").fill("grace@example.com");
|
|
194
|
+
await page.getByLabel("Password", { exact: true }).fill(PASSWORD);
|
|
195
|
+
await page.getByTestId("auth-submit").click();
|
|
196
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: true");
|
|
197
|
+
|
|
198
|
+
stack.apiRequests.length = 0;
|
|
199
|
+
documents.length = 0;
|
|
200
|
+
await page.goto(webmail);
|
|
201
|
+
await page.getByRole("link", { name: "Open your account" }).click();
|
|
202
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: true");
|
|
203
|
+
// The link's page load came from another site and carried no Strict cookie ...
|
|
204
|
+
const landing = await appDocument(documents, (url) => url.pathname === "/home");
|
|
205
|
+
assert.equal(landing["sec-fetch-site"], "cross-site");
|
|
206
|
+
assert.deepEqual(cookieNames(landing.cookie), []);
|
|
207
|
+
// ... and the page's own requests carried them all, so the person is signed in.
|
|
208
|
+
for (const { cookies } of apiRequest("GET /api/session")) {
|
|
209
|
+
assert.deepEqual(cookies, SESSION_COOKIES);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// A page load the person starts in the app carries them, so the empty Cookie header above is SameSite at work.
|
|
213
|
+
documents.length = 0;
|
|
214
|
+
await page.goto(`${stack.appUrl}/home`);
|
|
215
|
+
await expect(page.getByTestId("home")).toHaveText("Signed in: true");
|
|
216
|
+
const typed = await appDocument(documents, (url) => url.pathname === "/home");
|
|
217
|
+
assert.equal(typed["sec-fetch-site"], "none");
|
|
218
|
+
assert.deepEqual(cookieNames(typed.cookie), SESSION_COOKIES);
|
|
219
|
+
assert.deepEqual(errors, []);
|
|
220
|
+
});
|