@specific.dev/spectest 0.43.0 → 0.44.0

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.
@@ -0,0 +1,47 @@
1
+ import type { ProviderOptions } from "./emulate/service.js";
2
+ export type AppleOptions = ProviderOptions;
3
+ /**
4
+ * Apple, answering at its real endpoints. Drop into
5
+ * `environment.services`:
6
+ *
7
+ * ```ts
8
+ * import { apple } from "@specific.dev/spectest/components";
9
+ *
10
+ * services: {
11
+ * apple: apple({
12
+ * users: [{ email: "alice@example.com", name: "Alice Example" }],
13
+ * client: {
14
+ * clientId: "com.example.app",
15
+ * clientSecret: "client-assertion",
16
+ * redirectUris: ["https://app.test/callback/apple"],
17
+ * },
18
+ * }),
19
+ * app: { …, dependsOn: ["apple"] },
20
+ * }
21
+ * ```
22
+ *
23
+ * The app keeps its production configuration — it sends a browser to
24
+ * `https://appleid.apple.com/auth/authorize` and exchanges the code at
25
+ * `https://appleid.apple.com/auth/token`.
26
+ */
27
+ export declare function apple(opts?: AppleOptions): {
28
+ image: {
29
+ type: "dockerfile";
30
+ content: string;
31
+ };
32
+ command: string;
33
+ files: {
34
+ path: string;
35
+ content: string;
36
+ }[];
37
+ env: {
38
+ SPECTEST_AUTH_CONFIG: string;
39
+ };
40
+ ports: number[];
41
+ readyCheck: {
42
+ type: "http";
43
+ port: number;
44
+ path: string;
45
+ timeoutSecs: number;
46
+ };
47
+ };
@@ -0,0 +1,59 @@
1
+ // `apple()` — Apple, emulated inside the environment, answering at its real
2
+ // endpoints.
3
+ //
4
+ // Today that is Sign in with Apple: discovery, the account picker, the
5
+ // token exchange, the refresh grant, and revocation, all on
6
+ // `appleid.apple.com`.
7
+ //
8
+ // Apple publishes **no userinfo endpoint** — the real one does not either,
9
+ // because for Apple the id_token is the whole profile. An app that wants to
10
+ // re-read an account uses the refresh grant, which is the only
11
+ // server-to-server call Apple offers.
12
+ import { emulatorService, oauthClients, resolveUsers } from "./emulate/service.js";
13
+ function spec(users, opts) {
14
+ return {
15
+ name: "apple",
16
+ module: "@emulators/apple",
17
+ pluginExport: "applePlugin",
18
+ baseUrl: "https://appleid.apple.com",
19
+ // Every Apple endpoint is on this one host, so no path or discovery
20
+ // correction is needed.
21
+ hosts: ["appleid.apple.com"],
22
+ jwksPath: "^/auth/keys$",
23
+ fallbackUser: { login: users[0].email, id: 1, scopes: ["openid", "email", "name"] },
24
+ seed: {
25
+ users: users.map((u) => ({ email: u.email, name: u.name })),
26
+ // Apple authenticates the client with a signed assertion rather than
27
+ // a shared secret, so the registration carries a team id instead.
28
+ ...oauthClients(opts.client, { team_id: "SPECTEST" }),
29
+ },
30
+ };
31
+ }
32
+ /**
33
+ * Apple, answering at its real endpoints. Drop into
34
+ * `environment.services`:
35
+ *
36
+ * ```ts
37
+ * import { apple } from "@specific.dev/spectest/components";
38
+ *
39
+ * services: {
40
+ * apple: apple({
41
+ * users: [{ email: "alice@example.com", name: "Alice Example" }],
42
+ * client: {
43
+ * clientId: "com.example.app",
44
+ * clientSecret: "client-assertion",
45
+ * redirectUris: ["https://app.test/callback/apple"],
46
+ * },
47
+ * }),
48
+ * app: { …, dependsOn: ["apple"] },
49
+ * }
50
+ * ```
51
+ *
52
+ * The app keeps its production configuration — it sends a browser to
53
+ * `https://appleid.apple.com/auth/authorize` and exchanges the code at
54
+ * `https://appleid.apple.com/auth/token`.
55
+ */
56
+ export function apple(opts = {}) {
57
+ const users = resolveUsers("apple", opts.users);
58
+ return emulatorService(spec(users, opts));
59
+ }
@@ -0,0 +1,126 @@
1
+ /** A person who can sign in. Seeded into the provider's account picker. */
2
+ export interface ProviderUser {
3
+ /** Primary address. Also the account's test id on the picker page. */
4
+ email: string;
5
+ /** Display name. */
6
+ name?: string;
7
+ /** Avatar URL, passed through to the profile claims. */
8
+ picture?: string;
9
+ /** GitHub username. Defaults to the local part of `email`. */
10
+ login?: string;
11
+ }
12
+ /** An OAuth client, as registered with the real provider. Declaring one
13
+ * makes the environment **stricter**: `clientId`, `clientSecret` and
14
+ * `redirectUris` are then all validated, exactly as in production. Omit it
15
+ * and any client is accepted. */
16
+ export interface OAuthClient {
17
+ clientId: string;
18
+ clientSecret: string;
19
+ /** Callback URLs the app is allowed to return to. A request to any other
20
+ * one is refused, which is what makes a misconfigured callback fail here
21
+ * rather than in production. */
22
+ redirectUris?: string[];
23
+ }
24
+ /**
25
+ * The accounts every provider offers when a component declares none.
26
+ *
27
+ * Two, not one: a test that asserts it signed in as Alice only means
28
+ * something if the provider had somebody else to return. Both carry a
29
+ * `login`, so GitHub gets a handle without the caller supplying one.
30
+ *
31
+ * `example.com` is reserved for exactly this (RFC 2606), so no default
32
+ * account can ever collide with a real address.
33
+ */
34
+ export declare const DEFAULT_USERS: readonly ProviderUser[];
35
+ /** Options every provider component takes. */
36
+ export interface ProviderOptions {
37
+ /**
38
+ * The accounts offered on the provider's picker page. Defaults to
39
+ * **Alice Example** (`alice@example.com`) and **Bob Example**
40
+ * (`bob@example.com`) — enough to sign in as somebody, and to tell two
41
+ * people apart, without declaring anything.
42
+ */
43
+ users?: ProviderUser[];
44
+ /** The app's OAuth client. Omit it and any client id, secret and callback
45
+ * URL are accepted. */
46
+ client?: OAuthClient;
47
+ }
48
+ /** What `entry.mjs` consumes. Everything provider-specific lives in the
49
+ * per-provider module; the script itself is generic. */
50
+ export interface ProviderSpec {
51
+ name: string;
52
+ module: string;
53
+ pluginExport: string;
54
+ /** Base URL the emulator advertises — the provider's real sign-in host. */
55
+ baseUrl: string;
56
+ /** Every hostname routed to this emulator. */
57
+ hosts: string[];
58
+ /** False for a plain OAuth 2.0 provider with no id_token (GitHub). */
59
+ oidc?: boolean;
60
+ /** Issuer to stamp on the re-issued id_token, where the emulator cannot
61
+ * derive it from `baseUrl`. */
62
+ issuer?: string;
63
+ /** Values merged over the emulator's discovery document. */
64
+ discovery?: Record<string, string>;
65
+ /** Path (as a regex source) answered with our own JWKS. */
66
+ jwksPath?: string;
67
+ /** Picker-page identifier → the account's email, where the provider
68
+ * identifies an account by something else. Keeps the account's test id
69
+ * the same on every provider. */
70
+ aliases?: Record<string, string>;
71
+ /** Real path → the path this emulator serves it on. Identity elsewhere. */
72
+ rewrites?: {
73
+ from: string;
74
+ to: string;
75
+ }[];
76
+ fallbackUser?: {
77
+ login: string;
78
+ id: number;
79
+ scopes: string[];
80
+ };
81
+ seed?: Record<string, unknown>;
82
+ }
83
+ /** The service definition for one provider: the container, plus the claim
84
+ * on its domains (one certificate carrying every hostname as a SAN, and a
85
+ * proxy route each). One `certificate(...)` rather than per-host `tls`
86
+ * entries, for the reason `aws()` gives — `tls` mints a separate leaf per
87
+ * entry, and a provider can claim four names. */
88
+ export declare function emulatorService(spec: ProviderSpec): {
89
+ image: {
90
+ type: "dockerfile";
91
+ content: string;
92
+ };
93
+ command: string;
94
+ files: {
95
+ path: string;
96
+ content: string;
97
+ }[];
98
+ env: {
99
+ SPECTEST_AUTH_CONFIG: string;
100
+ };
101
+ ports: number[];
102
+ readyCheck: {
103
+ type: "http";
104
+ port: number;
105
+ path: string;
106
+ timeoutSecs: number;
107
+ };
108
+ };
109
+ /** The accounts to seed: what the caller declared, or {@link DEFAULT_USERS}.
110
+ * An explicitly empty list is a mistake, not a request for none — a
111
+ * provider with no accounts can never sign anyone in. */
112
+ export declare function resolveUsers(component: string, users: ProviderUser[] | undefined): ProviderUser[];
113
+ export declare function localPart(email: string): string;
114
+ export declare function givenName(user: ProviderUser): string | undefined;
115
+ export declare function familyName(user: ProviderUser): string | undefined;
116
+ /** The `oauth_clients` seed entry every provider but GitHub uses. */
117
+ export declare function oauthClients(client: OAuthClient | undefined, extra?: Record<string, unknown>): {
118
+ oauth_clients?: undefined;
119
+ } | {
120
+ oauth_clients: {
121
+ client_id: string;
122
+ client_secret: string;
123
+ name: string;
124
+ redirect_uris: string[];
125
+ }[];
126
+ };
@@ -0,0 +1,153 @@
1
+ // Shared runtime behind the third-party provider components — `google()`,
2
+ // `github()`, `apple()`, `microsoft()`, `okta()`.
3
+ //
4
+ // Each of those is one container answering at that provider's **real**
5
+ // endpoints: the component claims the provider's domains, so DNS, a leaf
6
+ // certificate from the in-VM root CA, and a reverse proxy all point at it.
7
+ // The app under test keeps its production configuration and never learns it
8
+ // is under test.
9
+ //
10
+ // A provider component is therefore a table, not a program. It names the
11
+ // hosts it claims, the emulator package behind it, the handful of paths
12
+ // where that emulator differs from the real service, and how to seed it.
13
+ // Everything else lives here and in `entry.mjs`.
14
+ //
15
+ // ── Internal notes (deliberately not in the user-facing docs) ─────────────
16
+ //
17
+ // The emulators come from vercel-labs/emulate (Apache-2.0), used through
18
+ // their published `@emulators/*` packages and pinned. They are NOT SDK
19
+ // dependencies: they install into the image below. That matters —
20
+ // `sdk/package.json` is in the base-snapshot discriminator, so a dependency
21
+ // here would force a base rebuild and one cold start for every project on
22
+ // the box, for components most projects never use.
23
+ //
24
+ // Each emulator serves every one of its endpoints on ONE origin. Real
25
+ // providers spread them over several hosts, and a few paths differ.
26
+ // `entry.mjs` routes by Host header, applies the per-provider path table,
27
+ // and corrects the discovery document. It also re-issues every id_token
28
+ // with one RSA key it generates at boot, and serves that key at the
29
+ // provider's real JWKS URL. That single step covers three separate faults:
30
+ // the Google emulator signs HS256 and publishes an empty JWKS (so nothing
31
+ // can verify its token), the Microsoft one cannot put the tenant in its
32
+ // issuer, and any future provider whose issuer is not its base URL is
33
+ // handled in advance.
34
+ //
35
+ // The Dockerfile is deliberately CONSTANT — every provider package is
36
+ // installed whether or not this component is the one using it. Two reasons:
37
+ // one image is shared by every provider component and every project on the
38
+ // box, so the layer cache is hit; and the daemon dedupes identical
39
+ // dockerfile builds inside one bootstrap, so five provider services in one
40
+ // environment cost one build, not five.
41
+ //
42
+ // `entry.mjs` ships as an asset next to this file, read at load time and
43
+ // injected with `files`. It is not a `.ts` string constant: it imports
44
+ // `@emulators/*`, which the SDK does not depend on, so `tsc` could not
45
+ // check it anyway, and 200 lines inside a template literal is worse to
46
+ // maintain. `.mjs` also keeps it out of `tsconfig`'s `include` with no
47
+ // exclude rule, and `files: ["src"]` in the manifest still ships it.
48
+ import { readFileSync } from "node:fs";
49
+ import path from "node:path";
50
+ import { fileURLToPath } from "node:url";
51
+ import { certificate, provides, proxy, SELF_SERVICE_TOKEN } from "../../index.js";
52
+ /** Base image, pinned. Bun runs the emulators' ESM directly. */
53
+ const IMAGE_BASE = "oven/bun:1.3.14-alpine";
54
+ /** Emulator package version, pinned rather than floated. */
55
+ const EMULATE_VERSION = "0.9.0";
56
+ /** The port the provider is served on. Callers reach it through the
57
+ * provider's real hostnames, never through this port. */
58
+ const PORT = 4000;
59
+ /** Ready-check budget. The container boots in about a second; the slow part
60
+ * is a cold image build on a machine that has never run it. */
61
+ const READY_TIMEOUT_SECS = 120;
62
+ /**
63
+ * The accounts every provider offers when a component declares none.
64
+ *
65
+ * Two, not one: a test that asserts it signed in as Alice only means
66
+ * something if the provider had somebody else to return. Both carry a
67
+ * `login`, so GitHub gets a handle without the caller supplying one.
68
+ *
69
+ * `example.com` is reserved for exactly this (RFC 2606), so no default
70
+ * account can ever collide with a real address.
71
+ */
72
+ export const DEFAULT_USERS = [
73
+ { email: "alice@example.com", name: "Alice Example", login: "alice" },
74
+ { email: "bob@example.com", name: "Bob Example", login: "bob" },
75
+ ];
76
+ /** The entry script, read from disk next to this module. */
77
+ const ENTRY_SCRIPT = (() => {
78
+ const here = path.dirname(fileURLToPath(import.meta.url));
79
+ return readFileSync(path.join(here, "entry.mjs"), "utf8");
80
+ })();
81
+ const DOCKERFILE = `
82
+ FROM ${IMAGE_BASE}
83
+ WORKDIR /srv
84
+ RUN echo '{"name":"spectest-provider","private":true}' > package.json \\
85
+ && bun add \\
86
+ @emulators/core@${EMULATE_VERSION} \\
87
+ @emulators/google@${EMULATE_VERSION} \\
88
+ @emulators/github@${EMULATE_VERSION} \\
89
+ @emulators/apple@${EMULATE_VERSION} \\
90
+ @emulators/microsoft@${EMULATE_VERSION} \\
91
+ @emulators/okta@${EMULATE_VERSION} \\
92
+ jose@6
93
+ `;
94
+ /** The service definition for one provider: the container, plus the claim
95
+ * on its domains (one certificate carrying every hostname as a SAN, and a
96
+ * proxy route each). One `certificate(...)` rather than per-host `tls`
97
+ * entries, for the reason `aws()` gives — `tls` mints a separate leaf per
98
+ * entry, and a provider can claim four names. */
99
+ export function emulatorService(spec) {
100
+ const service = {
101
+ image: { type: "dockerfile", content: DOCKERFILE },
102
+ command: "bun /srv/auth.mjs",
103
+ files: [{ path: "/srv/auth.mjs", content: ENTRY_SCRIPT }],
104
+ env: { SPECTEST_AUTH_CONFIG: JSON.stringify({ port: PORT, providers: [spec] }) },
105
+ ports: [PORT],
106
+ readyCheck: {
107
+ type: "http",
108
+ port: PORT,
109
+ path: "/healthz",
110
+ timeoutSecs: READY_TIMEOUT_SECS,
111
+ },
112
+ };
113
+ return provides(service, [
114
+ certificate(spec.hosts),
115
+ ...spec.hosts.map((hostname) => proxy(hostname, { service: SELF_SERVICE_TOKEN, port: PORT })),
116
+ ]);
117
+ }
118
+ // ── Seeding helpers, shared by the provider modules ───────────────────────
119
+ /** The accounts to seed: what the caller declared, or {@link DEFAULT_USERS}.
120
+ * An explicitly empty list is a mistake, not a request for none — a
121
+ * provider with no accounts can never sign anyone in. */
122
+ export function resolveUsers(component, users) {
123
+ if (users && users.length === 0) {
124
+ throw new Error(`${component}(): \`users\` is empty — omit it for the default accounts`);
125
+ }
126
+ return users ?? [...DEFAULT_USERS];
127
+ }
128
+ export function localPart(email) {
129
+ return email.split("@")[0] ?? email;
130
+ }
131
+ export function givenName(user) {
132
+ return user.name?.split(" ")[0];
133
+ }
134
+ export function familyName(user) {
135
+ const parts = user.name?.split(" ") ?? [];
136
+ return parts.length > 1 ? parts.slice(1).join(" ") : undefined;
137
+ }
138
+ /** The `oauth_clients` seed entry every provider but GitHub uses. */
139
+ export function oauthClients(client, extra = {}) {
140
+ if (!client)
141
+ return {};
142
+ return {
143
+ oauth_clients: [
144
+ {
145
+ client_id: client.clientId,
146
+ client_secret: client.clientSecret,
147
+ name: "spectest",
148
+ redirect_uris: client.redirectUris ?? [],
149
+ ...extra,
150
+ },
151
+ ],
152
+ };
153
+ }
@@ -0,0 +1,52 @@
1
+ import type { ProviderOptions } from "./emulate/service.js";
2
+ export type GithubOptions = ProviderOptions;
3
+ /**
4
+ * GitHub, answering at its real endpoints. Drop into
5
+ * `environment.services`:
6
+ *
7
+ * ```ts
8
+ * import { github } from "@specific.dev/spectest/components";
9
+ *
10
+ * services: {
11
+ * github: github({
12
+ * users: [{ email: "bob@example.com", name: "Bob Example", login: "bob" }],
13
+ * client: {
14
+ * clientId: "Iv1.example",
15
+ * clientSecret: "ghs_example",
16
+ * redirectUris: ["https://app.test/callback/github"],
17
+ * },
18
+ * }),
19
+ * app: { …, dependsOn: ["github"] },
20
+ * }
21
+ * ```
22
+ *
23
+ * The app keeps its production configuration — it sends a browser to
24
+ * `https://github.com/login/oauth/authorize`, exchanges the code at
25
+ * `https://github.com/login/oauth/access_token`, and reads the account from
26
+ * `https://api.github.com/user`.
27
+ *
28
+ * The account's test id on the picker page is its **email**, even though
29
+ * GitHub identifies it by login — so a test reads the same whichever
30
+ * provider it drives.
31
+ */
32
+ export declare function github(opts?: GithubOptions): {
33
+ image: {
34
+ type: "dockerfile";
35
+ content: string;
36
+ };
37
+ command: string;
38
+ files: {
39
+ path: string;
40
+ content: string;
41
+ }[];
42
+ env: {
43
+ SPECTEST_AUTH_CONFIG: string;
44
+ };
45
+ ports: number[];
46
+ readyCheck: {
47
+ type: "http";
48
+ port: number;
49
+ path: string;
50
+ timeoutSecs: number;
51
+ };
52
+ };
@@ -0,0 +1,90 @@
1
+ // `github()` — GitHub, emulated inside the environment, answering at its
2
+ // real endpoints.
3
+ //
4
+ // Today that is Sign in with GitHub — the authorize page, the token
5
+ // exchange, and the profile reads every GitHub sign-in makes (`/user`,
6
+ // `/user/emails`). The component is named for the provider rather than for
7
+ // the feature: the backing emulator carries most of the REST API — repos,
8
+ // issues, pull requests, checks, webhooks — and reaching it is a matter of
9
+ // what this component chooses to expose, not of new plumbing.
10
+ //
11
+ // Landmine for whoever extends this: the claim is env-wide. `github.com`
12
+ // and `api.github.com` stop reaching the internet for every container in
13
+ // the environment, so anything else that talks to GitHub — a build step
14
+ // that clones a repository, an unrelated API call — reaches the emulator
15
+ // and gets a 404 outside the surface it serves.
16
+ import { emulatorService, localPart, resolveUsers } from "./emulate/service.js";
17
+ function spec(users, opts) {
18
+ const client = opts.client;
19
+ return {
20
+ name: "github",
21
+ module: "@emulators/github",
22
+ pluginExport: "githubPlugin",
23
+ baseUrl: "https://github.com",
24
+ // api.github.com cannot be avoided: every GitHub sign-in reads the
25
+ // profile from /user and the addresses from /user/emails.
26
+ hosts: ["github.com", "api.github.com"],
27
+ // OAuth 2.0, not OIDC — there is no id_token to re-issue and no JWKS.
28
+ oidc: false,
29
+ // GitHub identifies an account by login, not by address.
30
+ aliases: Object.fromEntries(users.map((u) => [u.login ?? localPart(u.email), u.email])),
31
+ fallbackUser: {
32
+ login: users[0].login ?? localPart(users[0].email),
33
+ id: 1,
34
+ scopes: ["user", "user:email"],
35
+ },
36
+ seed: {
37
+ users: users.map((u) => ({
38
+ login: u.login ?? localPart(u.email),
39
+ name: u.name,
40
+ email: u.email,
41
+ })),
42
+ // GitHub calls them oauth_apps, and its own field is `oauth_apps`.
43
+ ...(client
44
+ ? {
45
+ oauth_apps: [
46
+ {
47
+ client_id: client.clientId,
48
+ client_secret: client.clientSecret,
49
+ name: "spectest",
50
+ redirect_uris: client.redirectUris ?? [],
51
+ },
52
+ ],
53
+ }
54
+ : {}),
55
+ },
56
+ };
57
+ }
58
+ /**
59
+ * GitHub, answering at its real endpoints. Drop into
60
+ * `environment.services`:
61
+ *
62
+ * ```ts
63
+ * import { github } from "@specific.dev/spectest/components";
64
+ *
65
+ * services: {
66
+ * github: github({
67
+ * users: [{ email: "bob@example.com", name: "Bob Example", login: "bob" }],
68
+ * client: {
69
+ * clientId: "Iv1.example",
70
+ * clientSecret: "ghs_example",
71
+ * redirectUris: ["https://app.test/callback/github"],
72
+ * },
73
+ * }),
74
+ * app: { …, dependsOn: ["github"] },
75
+ * }
76
+ * ```
77
+ *
78
+ * The app keeps its production configuration — it sends a browser to
79
+ * `https://github.com/login/oauth/authorize`, exchanges the code at
80
+ * `https://github.com/login/oauth/access_token`, and reads the account from
81
+ * `https://api.github.com/user`.
82
+ *
83
+ * The account's test id on the picker page is its **email**, even though
84
+ * GitHub identifies it by login — so a test reads the same whichever
85
+ * provider it drives.
86
+ */
87
+ export function github(opts = {}) {
88
+ const users = resolveUsers("github", opts.users);
89
+ return emulatorService(spec(users, opts));
90
+ }
@@ -0,0 +1,57 @@
1
+ import type { ProviderOptions } from "./emulate/service.js";
2
+ export type GoogleOptions = ProviderOptions;
3
+ /**
4
+ * Google, answering at its real endpoints. Drop into
5
+ * `environment.services`:
6
+ *
7
+ * ```ts
8
+ * import { google } from "@specific.dev/spectest/components";
9
+ *
10
+ * services: {
11
+ * // Accounts default to Alice and Bob Example; declare `users` to
12
+ * // choose your own. A `client` makes the environment validate the
13
+ * // app's client id, secret and callback URL.
14
+ * google: google({
15
+ * client: {
16
+ * clientId: "1234.apps.googleusercontent.com",
17
+ * clientSecret: "GOCSPX-test",
18
+ * redirectUris: ["https://app.test/callback/google"],
19
+ * },
20
+ * }),
21
+ * app: { …, dependsOn: ["google"] },
22
+ * }
23
+ * ```
24
+ *
25
+ * The app keeps its production configuration — it sends a browser to
26
+ * `https://accounts.google.com/o/oauth2/v2/auth`, exchanges the code at
27
+ * `https://oauth2.googleapis.com/token`, and verifies the id_token against
28
+ * `https://www.googleapis.com/oauth2/v3/certs`, exactly as it does live.
29
+ *
30
+ * A test signs in the way a user does, through the browser:
31
+ *
32
+ * ```ts
33
+ * await page.getByTestId("login-google").click();
34
+ * await page.getByTestId("spectest-user-alice@example.com").click();
35
+ * ```
36
+ */
37
+ export declare function google(opts?: GoogleOptions): {
38
+ image: {
39
+ type: "dockerfile";
40
+ content: string;
41
+ };
42
+ command: string;
43
+ files: {
44
+ path: string;
45
+ content: string;
46
+ }[];
47
+ env: {
48
+ SPECTEST_AUTH_CONFIG: string;
49
+ };
50
+ ports: number[];
51
+ readyCheck: {
52
+ type: "http";
53
+ port: number;
54
+ path: string;
55
+ timeoutSecs: number;
56
+ };
57
+ };
@@ -0,0 +1,92 @@
1
+ // `google()` — Google, emulated inside the environment, answering at its
2
+ // real endpoints.
3
+ //
4
+ // Today that is Sign in with Google: OpenID Connect discovery, the account
5
+ // picker, PKCE, the token exchange, refresh, and userinfo. The component is
6
+ // named for the provider rather than for the feature, because the backing
7
+ // emulator also carries Gmail, Calendar and Drive — claiming
8
+ // `gmail.googleapis.com` and routing it here is a table entry, not a new
9
+ // design.
10
+ import { emulatorService, familyName, givenName, oauthClients, resolveUsers, } from "./emulate/service.js";
11
+ function spec(users, opts) {
12
+ return {
13
+ name: "google",
14
+ module: "@emulators/google",
15
+ pluginExport: "googlePlugin",
16
+ baseUrl: "https://accounts.google.com",
17
+ hosts: [
18
+ "accounts.google.com",
19
+ "oauth2.googleapis.com",
20
+ "www.googleapis.com",
21
+ "openidconnect.googleapis.com",
22
+ ],
23
+ discovery: {
24
+ issuer: "https://accounts.google.com",
25
+ authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth",
26
+ token_endpoint: "https://oauth2.googleapis.com/token",
27
+ userinfo_endpoint: "https://openidconnect.googleapis.com/v1/userinfo",
28
+ revocation_endpoint: "https://oauth2.googleapis.com/revoke",
29
+ jwks_uri: "https://www.googleapis.com/oauth2/v3/certs",
30
+ },
31
+ jwksPath: "^/oauth2/v3/certs$",
32
+ rewrites: [
33
+ // oauth2.googleapis.com serves these at the root; the emulator puts
34
+ // them under /oauth2.
35
+ { from: "^/token$", to: "/oauth2/token" },
36
+ { from: "^/revoke$", to: "/oauth2/revoke" },
37
+ // openidconnect.googleapis.com and the v3 alias both mean userinfo.
38
+ { from: "^/v1/userinfo$", to: "/oauth2/v2/userinfo" },
39
+ { from: "^/oauth2/v3/userinfo$", to: "/oauth2/v2/userinfo" },
40
+ ],
41
+ fallbackUser: { login: users[0].email, id: 1, scopes: ["openid", "email", "profile"] },
42
+ seed: {
43
+ users: users.map((u) => ({
44
+ email: u.email,
45
+ name: u.name,
46
+ given_name: givenName(u),
47
+ family_name: familyName(u),
48
+ picture: u.picture,
49
+ email_verified: true,
50
+ })),
51
+ ...oauthClients(opts.client),
52
+ },
53
+ };
54
+ }
55
+ /**
56
+ * Google, answering at its real endpoints. Drop into
57
+ * `environment.services`:
58
+ *
59
+ * ```ts
60
+ * import { google } from "@specific.dev/spectest/components";
61
+ *
62
+ * services: {
63
+ * // Accounts default to Alice and Bob Example; declare `users` to
64
+ * // choose your own. A `client` makes the environment validate the
65
+ * // app's client id, secret and callback URL.
66
+ * google: google({
67
+ * client: {
68
+ * clientId: "1234.apps.googleusercontent.com",
69
+ * clientSecret: "GOCSPX-test",
70
+ * redirectUris: ["https://app.test/callback/google"],
71
+ * },
72
+ * }),
73
+ * app: { …, dependsOn: ["google"] },
74
+ * }
75
+ * ```
76
+ *
77
+ * The app keeps its production configuration — it sends a browser to
78
+ * `https://accounts.google.com/o/oauth2/v2/auth`, exchanges the code at
79
+ * `https://oauth2.googleapis.com/token`, and verifies the id_token against
80
+ * `https://www.googleapis.com/oauth2/v3/certs`, exactly as it does live.
81
+ *
82
+ * A test signs in the way a user does, through the browser:
83
+ *
84
+ * ```ts
85
+ * await page.getByTestId("login-google").click();
86
+ * await page.getByTestId("spectest-user-alice@example.com").click();
87
+ * ```
88
+ */
89
+ export function google(opts = {}) {
90
+ const users = resolveUsers("google", opts.users);
91
+ return emulatorService(spec(users, opts));
92
+ }