@hogsend/engine 0.64.0 → 0.65.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.
- package/package.json +12 -12
- package/src/account-links/__fixtures__/fake-fetch.test.ts +57 -0
- package/src/account-links/__fixtures__/fake-fetch.ts +114 -0
- package/src/account-links/__fixtures__/steam.json +21 -0
- package/src/account-links/__fixtures__/twitch.json +22 -0
- package/src/account-links/index.ts +8 -0
- package/src/account-links/steam.test.ts +243 -0
- package/src/account-links/steam.ts +48 -0
- package/src/account-links/twitch.test.ts +157 -0
- package/src/account-links/twitch.ts +99 -0
- package/src/container.account-links.env.test.ts +65 -0
- package/src/container.account-links.halfenv.test.ts +48 -0
- package/src/container.account-links.test.ts +185 -0
- package/src/container.ts +120 -0
- package/src/env.ts +37 -0
- package/src/index.ts +87 -1
- package/src/lib/account-link-emit.ts +126 -0
- package/src/lib/account-link-events.ts +180 -0
- package/src/lib/account-link-hooks.test.ts +158 -0
- package/src/lib/account-link-hooks.ts +73 -0
- package/src/lib/account-link-ingest.ts +364 -0
- package/src/lib/account-link-nonce.test.ts +52 -0
- package/src/lib/account-link-nonce.ts +57 -0
- package/src/lib/account-link-origins.test.ts +72 -0
- package/src/lib/account-link-origins.ts +87 -0
- package/src/lib/account-link-pkce.test.ts +91 -0
- package/src/lib/account-link-pkce.ts +106 -0
- package/src/lib/account-link-provider-registry.test.ts +52 -0
- package/src/lib/account-link-provider-registry.ts +61 -0
- package/src/lib/account-link-throttle.test.ts +178 -0
- package/src/lib/account-link-throttle.ts +130 -0
- package/src/lib/account-link-url.ts +85 -0
- package/src/lib/account-links-emit.test.ts +525 -0
- package/src/lib/account-links-env.test.ts +40 -0
- package/src/lib/account-links-from-env.test.ts +187 -0
- package/src/lib/account-links-from-env.ts +126 -0
- package/src/lib/account-links.ts +1300 -0
- package/src/lib/app-secret.ts +34 -0
- package/src/lib/connector-actions.test.ts +13 -4
- package/src/lib/connector-state.account-link.test.ts +114 -0
- package/src/lib/connector-state.ts +48 -3
- package/src/lib/contacts.ts +320 -6
- package/src/lib/outbound-account-payloads.test.ts +137 -0
- package/src/lib/outbound.ts +91 -0
- package/src/lib/provider-credentials.ts +19 -5
- package/src/lib/webhook-catalog-sync.test.ts +24 -0
- package/src/lib/webhook-signing.ts +18 -1
- package/src/routes/accounts/callback.ts +597 -0
- package/src/routes/accounts/emit.ts +231 -0
- package/src/routes/accounts/import-links.ts +210 -0
- package/src/routes/accounts/index.ts +75 -0
- package/src/routes/accounts/lookup.ts +177 -0
- package/src/routes/accounts/me.ts +215 -0
- package/src/routes/accounts/mint.ts +279 -0
- package/src/routes/accounts/pages.ts +62 -0
- package/src/routes/accounts/resolve.ts +61 -0
- package/src/routes/accounts/serialize.ts +96 -0
- package/src/routes/accounts/shared.ts +107 -0
- package/src/routes/accounts/start.ts +229 -0
- package/src/routes/accounts/unlink.ts +98 -0
- package/src/routes/admin/agent.ts +2 -0
- package/src/routes/admin/api-keys.ts +7 -0
- package/src/routes/admin/contacts.ts +38 -2
- package/src/routes/connectors/index.ts +17 -0
- package/src/routes/contacts/index.ts +8 -0
- package/src/routes/index.ts +75 -0
- package/src/testing-barrel.test.ts +74 -0
- package/src/testing.ts +17 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { AccountLinkCallbackError } from "@hogsend/core";
|
|
4
|
+
import { fakeFetch } from "./__fixtures__/fake-fetch.js";
|
|
5
|
+
import twitchFixtures from "./__fixtures__/twitch.json" with { type: "json" };
|
|
6
|
+
import { mapTwitchUser, twitchAccountLink } from "./twitch.js";
|
|
7
|
+
|
|
8
|
+
const CONFIG = {
|
|
9
|
+
clientId: "fixture-client-id",
|
|
10
|
+
clientSecret: "fixture-secret",
|
|
11
|
+
};
|
|
12
|
+
const REDIRECT_URI = "https://api.example.com/v1/accounts/twitch/callback";
|
|
13
|
+
|
|
14
|
+
function provider() {
|
|
15
|
+
return twitchAccountLink(CONFIG);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test("authorizeUrl carries scope=user:read:email and force_verify", async () => {
|
|
19
|
+
const url = new URL(
|
|
20
|
+
await provider().authorizeUrl({
|
|
21
|
+
state: "signed-state",
|
|
22
|
+
redirectUri: REDIRECT_URI,
|
|
23
|
+
codeChallenge: "challenge-abc",
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
assert.equal(
|
|
27
|
+
url.origin + url.pathname,
|
|
28
|
+
"https://id.twitch.tv/oauth2/authorize",
|
|
29
|
+
);
|
|
30
|
+
assert.equal(url.searchParams.get("response_type"), "code");
|
|
31
|
+
assert.equal(url.searchParams.get("client_id"), CONFIG.clientId);
|
|
32
|
+
assert.equal(url.searchParams.get("redirect_uri"), REDIRECT_URI);
|
|
33
|
+
assert.equal(url.searchParams.get("scope"), "user:read:email");
|
|
34
|
+
assert.equal(url.searchParams.get("state"), "signed-state");
|
|
35
|
+
assert.equal(url.searchParams.get("code_challenge"), "challenge-abc");
|
|
36
|
+
assert.equal(url.searchParams.get("code_challenge_method"), "S256");
|
|
37
|
+
assert.equal(url.searchParams.get("force_verify"), "true");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("the Helix profile call sends both Authorization and Client-Id", async () => {
|
|
41
|
+
const { fetchImpl, calls } = fakeFetch({
|
|
42
|
+
"POST https://id.twitch.tv/oauth2/token": {
|
|
43
|
+
body: { access_token: "helix-access-token", refresh_token: "r1" },
|
|
44
|
+
},
|
|
45
|
+
"GET https://api.twitch.tv/helix/users": { body: twitchFixtures.users },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
await provider().handleCallback({
|
|
49
|
+
query: { code: "auth-code", state: "signed-state" },
|
|
50
|
+
redirectUri: REDIRECT_URI,
|
|
51
|
+
codeVerifier: "verifier-abc",
|
|
52
|
+
fetchImpl,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const helix = calls.find((c) => c.url.startsWith("https://api.twitch.tv/"));
|
|
56
|
+
assert.ok(helix, "no Helix call recorded");
|
|
57
|
+
assert.equal(helix.headers.authorization, "Bearer helix-access-token");
|
|
58
|
+
assert.equal(helix.headers["client-id"], CONFIG.clientId);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("mapTwitchUser maps data[0] to a LinkedIdentity", () => {
|
|
62
|
+
const identity = mapTwitchUser(twitchFixtures.users);
|
|
63
|
+
// The IMMUTABLE numeric id, never login/display_name.
|
|
64
|
+
assert.equal(identity.providerUserId, "141981764");
|
|
65
|
+
assert.equal(identity.username, "TwitchDev");
|
|
66
|
+
assert.match(identity.avatarUrl ?? "", /^https:\/\/static-cdn\.jtvnw\.net\//);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("mapTwitchUser never sets verifiedEmail and stores twitch_email instead", () => {
|
|
70
|
+
const identity = mapTwitchUser(twitchFixtures.users);
|
|
71
|
+
assert.equal(identity.verifiedEmail, undefined);
|
|
72
|
+
assert.equal("verifiedEmail" in identity, false);
|
|
73
|
+
assert.equal(identity.properties?.twitch_email, "not-real@email.com");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("mapTwitchUser throws when data is empty", () => {
|
|
77
|
+
assert.throws(
|
|
78
|
+
() => mapTwitchUser(twitchFixtures.usersEmpty),
|
|
79
|
+
(err: unknown) =>
|
|
80
|
+
err instanceof AccountLinkCallbackError &&
|
|
81
|
+
err.reason === "exchange_failed",
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("the full handleCallback runs through the injected fetchImpl with no network", async () => {
|
|
86
|
+
const { fetchImpl, calls } = fakeFetch({
|
|
87
|
+
"POST https://id.twitch.tv/oauth2/token": {
|
|
88
|
+
body: {
|
|
89
|
+
access_token: "helix-access-token",
|
|
90
|
+
refresh_token: "r1",
|
|
91
|
+
expires_in: 3600,
|
|
92
|
+
scope: "user:read:email",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
"GET https://api.twitch.tv/helix/users": { body: twitchFixtures.users },
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const identity = await provider().handleCallback({
|
|
99
|
+
query: { code: "auth-code", state: "signed-state" },
|
|
100
|
+
redirectUri: REDIRECT_URI,
|
|
101
|
+
codeVerifier: "verifier-abc",
|
|
102
|
+
fetchImpl,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Exactly the two expected calls, both through the fake — an unmatched
|
|
106
|
+
// route would have thrown, so nothing reached the global fetch.
|
|
107
|
+
assert.deepEqual(
|
|
108
|
+
calls.map(
|
|
109
|
+
(c) => `${c.method} ${new URL(c.url).origin}${new URL(c.url).pathname}`,
|
|
110
|
+
),
|
|
111
|
+
[
|
|
112
|
+
"POST https://id.twitch.tv/oauth2/token",
|
|
113
|
+
"GET https://api.twitch.tv/helix/users",
|
|
114
|
+
],
|
|
115
|
+
);
|
|
116
|
+
// The exchange carried the PKCE verifier and the byte-exact redirect_uri.
|
|
117
|
+
const exchange = new URLSearchParams(calls[0]?.body ?? "");
|
|
118
|
+
assert.equal(exchange.get("code"), "auth-code");
|
|
119
|
+
assert.equal(exchange.get("code_verifier"), "verifier-abc");
|
|
120
|
+
assert.equal(exchange.get("redirect_uri"), REDIRECT_URI);
|
|
121
|
+
|
|
122
|
+
assert.equal(identity.providerUserId, "141981764");
|
|
123
|
+
assert.equal(identity.verifiedEmail, undefined);
|
|
124
|
+
// storeTokens: the grant rides on the identity for the engine to seal.
|
|
125
|
+
assert.equal(identity.tokens?.accessToken, "helix-access-token");
|
|
126
|
+
assert.equal(identity.tokens?.refreshToken, "r1");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a 401 from /helix/users throws exchange_failed with no response body in the message", async () => {
|
|
130
|
+
const leakyBody = { error: "Unauthorized", message: "SECRET-LEAK-MARKER" };
|
|
131
|
+
const { fetchImpl } = fakeFetch({
|
|
132
|
+
"POST https://id.twitch.tv/oauth2/token": {
|
|
133
|
+
body: { access_token: "helix-access-token" },
|
|
134
|
+
},
|
|
135
|
+
"GET https://api.twitch.tv/helix/users": { status: 401, body: leakyBody },
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await assert.rejects(
|
|
139
|
+
() =>
|
|
140
|
+
provider().handleCallback({
|
|
141
|
+
query: { code: "auth-code" },
|
|
142
|
+
redirectUri: REDIRECT_URI,
|
|
143
|
+
codeVerifier: "verifier-abc",
|
|
144
|
+
fetchImpl,
|
|
145
|
+
}),
|
|
146
|
+
(err: unknown) => {
|
|
147
|
+
assert.ok(err instanceof AccountLinkCallbackError);
|
|
148
|
+
assert.equal(err.reason, "exchange_failed");
|
|
149
|
+
// Status + host only. Never the body, never a token, never the secret.
|
|
150
|
+
assert.match(err.message, /api\.twitch\.tv responded 401/);
|
|
151
|
+
assert.doesNotMatch(err.message, /SECRET-LEAK-MARKER/);
|
|
152
|
+
assert.doesNotMatch(err.message, /helix-access-token/);
|
|
153
|
+
assert.doesNotMatch(err.message, /fixture-secret/);
|
|
154
|
+
return true;
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AccountLinkCallbackError,
|
|
3
|
+
type AccountLinkProvider,
|
|
4
|
+
type LinkedIdentity,
|
|
5
|
+
oauth2Link,
|
|
6
|
+
} from "@hogsend/core";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Twitch app credentials, from an application registered at
|
|
10
|
+
* `https://dev.twitch.tv/console/apps` with OAuth redirect URL
|
|
11
|
+
* `<API_PUBLIC_URL>/v1/accounts/twitch/callback`. Env spelling:
|
|
12
|
+
* `ACCOUNT_LINK_TWITCH_CLIENT_ID` / `ACCOUNT_LINK_TWITCH_CLIENT_SECRET`
|
|
13
|
+
* (see `lib/account-links-from-env.ts` — registered only when BOTH are set).
|
|
14
|
+
*/
|
|
15
|
+
export interface TwitchAccountLinkConfig {
|
|
16
|
+
clientId: string;
|
|
17
|
+
clientSecret: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The slice of a Helix `GET /helix/users` row this mapping reads. */
|
|
21
|
+
interface HelixUser {
|
|
22
|
+
id?: string;
|
|
23
|
+
login?: string;
|
|
24
|
+
display_name?: string;
|
|
25
|
+
profile_image_url?: string;
|
|
26
|
+
email?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Map a Helix `GET /helix/users` response onto a {@link LinkedIdentity}.
|
|
31
|
+
* Exported for direct unit testing — it is the only real logic in this file.
|
|
32
|
+
*
|
|
33
|
+
* - `providerUserId` is Helix's numeric `id`, the IMMUTABLE key. Never `login`
|
|
34
|
+
* or `display_name`: both are user-editable, so keying on one lets a player
|
|
35
|
+
* rename themselves onto another player's link row.
|
|
36
|
+
* - The email is a PROPERTY, never an identity key and never `verifiedEmail`:
|
|
37
|
+
* Helix returns `email` under the `user:read:email` scope but exposes no
|
|
38
|
+
* per-address verification boolean, and DECISIONS §6.4 makes a
|
|
39
|
+
* provider-reported email a property regardless. It lands in
|
|
40
|
+
* `properties.twitch_email`.
|
|
41
|
+
*/
|
|
42
|
+
export function mapTwitchUser(json: unknown): Omit<LinkedIdentity, "tokens"> {
|
|
43
|
+
const user = (json as { data?: HelixUser[] } | null | undefined)?.data?.[0];
|
|
44
|
+
if (!user?.id) {
|
|
45
|
+
// An empty `data` array with a 200 status happens when the token's user
|
|
46
|
+
// was deleted mid-flow. An identity with an empty providerUserId would
|
|
47
|
+
// key a link row on "", so throw instead.
|
|
48
|
+
throw new AccountLinkCallbackError(
|
|
49
|
+
"exchange_failed",
|
|
50
|
+
"Helix /users returned no user for the presented token",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const username = user.display_name || user.login;
|
|
54
|
+
return {
|
|
55
|
+
providerUserId: user.id,
|
|
56
|
+
...(username ? { username } : {}),
|
|
57
|
+
...(user.profile_image_url ? { avatarUrl: user.profile_image_url } : {}),
|
|
58
|
+
...(user.email ? { properties: { twitch_email: user.email } } : {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The Twitch account-link provider: pure CONFIG over the `oauth2Link()` preset
|
|
64
|
+
* (OAuth2 authorization code + PKCE, tokens sealed, revoke on unlink). All
|
|
65
|
+
* protocol mechanics — authorize URL, token exchange, `error=access_denied`
|
|
66
|
+
* mapping, the userinfo fetch — live in the preset
|
|
67
|
+
* (`@hogsend/core`, `providers/account-link-presets.ts`), not here.
|
|
68
|
+
*
|
|
69
|
+
* `userInfo.headers` carries `Client-Id` because Helix 401s any request
|
|
70
|
+
* without it; the preset merges it UNDER the `Authorization: Bearer` header.
|
|
71
|
+
*/
|
|
72
|
+
export function twitchAccountLink(
|
|
73
|
+
config: TwitchAccountLinkConfig,
|
|
74
|
+
): AccountLinkProvider {
|
|
75
|
+
return oauth2Link({
|
|
76
|
+
meta: {
|
|
77
|
+
id: "twitch",
|
|
78
|
+
name: "Twitch",
|
|
79
|
+
description: "Link a Twitch account (OAuth2 + PKCE)",
|
|
80
|
+
},
|
|
81
|
+
authorizeEndpoint: "https://id.twitch.tv/oauth2/authorize",
|
|
82
|
+
tokenEndpoint: "https://id.twitch.tv/oauth2/token",
|
|
83
|
+
clientId: config.clientId,
|
|
84
|
+
clientSecret: config.clientSecret,
|
|
85
|
+
scopes: ["user:read:email"],
|
|
86
|
+
usePkce: true,
|
|
87
|
+
storeTokens: true,
|
|
88
|
+
// Without force_verify Twitch silently reuses the browser's live session,
|
|
89
|
+
// which links whoever happens to be signed in — on a shared machine that
|
|
90
|
+
// is the wrong account with no prompt.
|
|
91
|
+
authorizeParams: { force_verify: "true" },
|
|
92
|
+
revokeEndpoint: "https://id.twitch.tv/oauth2/revoke",
|
|
93
|
+
userInfo: {
|
|
94
|
+
url: "https://api.twitch.tv/helix/users",
|
|
95
|
+
headers: { "Client-Id": config.clientId },
|
|
96
|
+
map: mapTwitchUser,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
// The env-var leg of the allowlist merge needs ACCOUNT_LINK_ALLOWED_ORIGINS
|
|
5
|
+
// SET at env-parse time, and t3-env parses process.env exactly once per
|
|
6
|
+
// import — so this case lives in its own file (its own node:test process),
|
|
7
|
+
// exactly the email-provider-boot.hogsend.test.ts split. Env is set BEFORE
|
|
8
|
+
// the container import; see container.account-links.test.ts for the rest of
|
|
9
|
+
// the boot preconditions.
|
|
10
|
+
process.env.NODE_ENV = "test";
|
|
11
|
+
process.env.LOG_LEVEL = "error";
|
|
12
|
+
process.env.DATABASE_URL ??= "postgresql://test:test@localhost:5432/test";
|
|
13
|
+
process.env.BETTER_AUTH_SECRET ??=
|
|
14
|
+
"test-secret-for-node-test-minimum-32-characters-long";
|
|
15
|
+
process.env.HATCHET_CLIENT_TOKEN ??=
|
|
16
|
+
"eyJhbGciOiJFUzI1NiIsImtpZCI6InRlc3QifQ.eyJhdWQiOiJsb2NhbGhvc3QiLCJleHAiOjQ5MzMyNDA5ODMsImdycGNfYnJvYWRjYXN0X2FkZHJlc3MiOiJsb2NhbGhvc3Q6NzA3NyIsImlhdCI6MTc3OTY0MDk4MywiaXNzIjoibG9jYWxob3N0Iiwic2VydmVyX3VybCI6ImxvY2FsaG9zdCIsInN1YiI6InRlc3QtdGVuYW50LWlkIiwidG9rZW5faWQiOiJ0ZXN0LXRva2VuLWlkIn0.test";
|
|
17
|
+
process.env.ACCOUNT_LINK_ALLOWED_ORIGINS =
|
|
18
|
+
"https://env-a.example.com, https://env-b.example.com";
|
|
19
|
+
process.env.ACCOUNT_LINK_TWITCH_CLIENT_ID = "env-twitch-client-id";
|
|
20
|
+
process.env.ACCOUNT_LINK_TWITCH_CLIENT_SECRET = "env-twitch-client-secret";
|
|
21
|
+
|
|
22
|
+
const { createHogsendClient } = await import("./container.js");
|
|
23
|
+
|
|
24
|
+
test("parses ACCOUNT_LINK_ALLOWED_ORIGINS and the option into one list, env first", () => {
|
|
25
|
+
const client = createHogsendClient({
|
|
26
|
+
accountLinks: {
|
|
27
|
+
allowedOrigins: [
|
|
28
|
+
"https://option.example.com",
|
|
29
|
+
// Duplicate of an env entry — deduped, first (env) position kept.
|
|
30
|
+
"https://env-a.example.com",
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
assert.deepEqual(client.accountLinkAllowedOrigins, [
|
|
35
|
+
"https://env-a.example.com",
|
|
36
|
+
"https://env-b.example.com",
|
|
37
|
+
"https://option.example.com",
|
|
38
|
+
]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("registers twitch + steam from env", () => {
|
|
42
|
+
const client = createHogsendClient();
|
|
43
|
+
// Steam unconditionally, twitch because BOTH ACCOUNT_LINK_TWITCH_* vars are
|
|
44
|
+
// set above. Env-preset order: steam first, then twitch.
|
|
45
|
+
assert.deepEqual(client.accountLinkProviders.ids(), ["steam", "twitch"]);
|
|
46
|
+
assert.equal(client.accountLinkProviders.get("twitch")?.meta.name, "Twitch");
|
|
47
|
+
assert.equal(
|
|
48
|
+
client.accountLinkProviders.get("twitch")?.capabilities?.tokens,
|
|
49
|
+
true,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a consumer provider of the same id overrides the env preset", () => {
|
|
54
|
+
const consumerTwitch = {
|
|
55
|
+
meta: { id: "twitch", name: "consumer-twitch" },
|
|
56
|
+
authorizeUrl: () => "https://example.com/authorize",
|
|
57
|
+
handleCallback: async () => ({ providerUserId: "u1" }),
|
|
58
|
+
};
|
|
59
|
+
const client = createHogsendClient({
|
|
60
|
+
accountLinks: { providers: [consumerTwitch] },
|
|
61
|
+
});
|
|
62
|
+
// Env presets first, consumer last: same count, consumer object wins.
|
|
63
|
+
assert.deepEqual(client.accountLinkProviders.ids(), ["steam", "twitch"]);
|
|
64
|
+
assert.equal(client.accountLinkProviders.get("twitch"), consumerTwitch);
|
|
65
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
// The half-configured-twitch boot needs ACCOUNT_LINK_TWITCH_CLIENT_ID set and
|
|
5
|
+
// _CLIENT_SECRET unset at env-parse time, and t3-env parses process.env
|
|
6
|
+
// exactly once per import — so this combination lives in its own file (its own
|
|
7
|
+
// node:test process), the container.account-links.env.test.ts split. Env is
|
|
8
|
+
// set BEFORE the container import; LOG_LEVEL=warn so the warning under test is
|
|
9
|
+
// actually emitted.
|
|
10
|
+
process.env.NODE_ENV = "test";
|
|
11
|
+
process.env.LOG_LEVEL = "warn";
|
|
12
|
+
process.env.DATABASE_URL ??= "postgresql://test:test@localhost:5432/test";
|
|
13
|
+
process.env.BETTER_AUTH_SECRET ??=
|
|
14
|
+
"test-secret-for-node-test-minimum-32-characters-long";
|
|
15
|
+
process.env.HATCHET_CLIENT_TOKEN ??=
|
|
16
|
+
"eyJhbGciOiJFUzI1NiIsImtpZCI6InRlc3QifQ.eyJhdWQiOiJsb2NhbGhvc3QiLCJleHAiOjQ5MzMyNDA5ODMsImdycGNfYnJvYWRjYXN0X2FkZHJlc3MiOiJsb2NhbGhvc3Q6NzA3NyIsImlhdCI6MTc3OTY0MDk4MywiaXNzIjoibG9jYWxob3N0Iiwic2VydmVyX3VybCI6ImxvY2FsaG9zdCIsInN1YiI6InRlc3QtdGVuYW50LWlkIiwidG9rZW5faWQiOiJ0ZXN0LXRva2VuLWlkIn0.test";
|
|
17
|
+
process.env.ACCOUNT_LINK_TWITCH_CLIENT_ID = "env-twitch-client-id";
|
|
18
|
+
delete process.env.ACCOUNT_LINK_TWITCH_CLIENT_SECRET;
|
|
19
|
+
|
|
20
|
+
const { createHogsendClient } = await import("./container.js");
|
|
21
|
+
|
|
22
|
+
/** Same boot-capture idiom as container.account-links.test.ts. */
|
|
23
|
+
function captureBoot<T>(fn: () => T): { value: T; out: string } {
|
|
24
|
+
const original = process.stdout.write.bind(process.stdout);
|
|
25
|
+
let out = "";
|
|
26
|
+
process.stdout.write = ((chunk: unknown) => {
|
|
27
|
+
out += String(chunk);
|
|
28
|
+
return true;
|
|
29
|
+
}) as typeof process.stdout.write;
|
|
30
|
+
try {
|
|
31
|
+
return { value: fn(), out };
|
|
32
|
+
} finally {
|
|
33
|
+
process.stdout.write = original;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const HALF_CONFIGURED_WARN = "twitch account linking is half-configured";
|
|
38
|
+
|
|
39
|
+
test("warns once, does not throw, on a half-configured twitch", () => {
|
|
40
|
+
// Does not throw — the boot completes…
|
|
41
|
+
const { value: client, out } = captureBoot(() => createHogsendClient());
|
|
42
|
+
// …twitch is ABSENT from the registry (not present-but-disabled), steam
|
|
43
|
+
// still registers…
|
|
44
|
+
assert.deepEqual(client.accountLinkProviders.ids(), ["steam"]);
|
|
45
|
+
// …and the warning fires exactly once, naming the MISSING var.
|
|
46
|
+
assert.equal(out.split(HALF_CONFIGURED_WARN).length - 1, 1);
|
|
47
|
+
assert.match(out, /ACCOUNT_LINK_TWITCH_CLIENT_SECRET is unset/);
|
|
48
|
+
});
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import type { AccountLinkHooks, AccountLinkProvider } from "@hogsend/core";
|
|
4
|
+
|
|
5
|
+
// Env is set BEFORE the container import — env.ts validates at module scope.
|
|
6
|
+
// LOG_LEVEL=warn so the boot warnings under test are actually emitted. No
|
|
7
|
+
// DATABASE_URL connection is opened (`postgres()` is lazy) and boot side
|
|
8
|
+
// effects are skipped under NODE_ENV=test, so a full createHogsendClient()
|
|
9
|
+
// completes offline. Every ACCOUNT_LINK_* var is UNSET here — the env-set
|
|
10
|
+
// merge case lives in container.account-links.env.test.ts (its own process,
|
|
11
|
+
// because t3-env parses process.env exactly once per import).
|
|
12
|
+
process.env.NODE_ENV = "test";
|
|
13
|
+
process.env.LOG_LEVEL = "warn";
|
|
14
|
+
process.env.DATABASE_URL ??= "postgresql://test:test@localhost:5432/test";
|
|
15
|
+
process.env.BETTER_AUTH_SECRET ??=
|
|
16
|
+
"test-secret-for-node-test-minimum-32-characters-long";
|
|
17
|
+
process.env.HATCHET_CLIENT_TOKEN ??=
|
|
18
|
+
"eyJhbGciOiJFUzI1NiIsImtpZCI6InRlc3QifQ.eyJhdWQiOiJsb2NhbGhvc3QiLCJleHAiOjQ5MzMyNDA5ODMsImdycGNfYnJvYWRjYXN0X2FkZHJlc3MiOiJsb2NhbGhvc3Q6NzA3NyIsImlhdCI6MTc3OTY0MDk4MywiaXNzIjoibG9jYWxob3N0Iiwic2VydmVyX3VybCI6ImxvY2FsaG9zdCIsInN1YiI6InRlc3QtdGVuYW50LWlkIiwidG9rZW5faWQiOiJ0ZXN0LXRva2VuLWlkIn0.test";
|
|
19
|
+
for (const name of [
|
|
20
|
+
"ACCOUNT_LINK_TWITCH_CLIENT_ID",
|
|
21
|
+
"ACCOUNT_LINK_TWITCH_CLIENT_SECRET",
|
|
22
|
+
"STEAM_WEB_API_KEY",
|
|
23
|
+
"ACCOUNT_LINK_ALLOWED_ORIGINS",
|
|
24
|
+
]) {
|
|
25
|
+
delete process.env[name];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { createHogsendClient } = await import("./container.js");
|
|
29
|
+
|
|
30
|
+
function stubProvider(id: string, name = id): AccountLinkProvider {
|
|
31
|
+
return {
|
|
32
|
+
meta: { id, name },
|
|
33
|
+
authorizeUrl: () => `https://example.com/authorize?provider=${id}`,
|
|
34
|
+
handleCallback: async () => ({ providerUserId: `${id}-user` }),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Boot the container while capturing everything winston writes to stdout
|
|
40
|
+
* (boot-time warns happen inside createHogsendClient, before any test could
|
|
41
|
+
* spy on `client.logger`). Output is swallowed during the boot so the TAP
|
|
42
|
+
* stream stays clean; the winston Console transport writes synchronously.
|
|
43
|
+
*/
|
|
44
|
+
function captureBoot<T>(fn: () => T): { value: T; out: string } {
|
|
45
|
+
const original = process.stdout.write.bind(process.stdout);
|
|
46
|
+
let out = "";
|
|
47
|
+
process.stdout.write = ((chunk: unknown) => {
|
|
48
|
+
out += String(chunk);
|
|
49
|
+
return true;
|
|
50
|
+
}) as typeof process.stdout.write;
|
|
51
|
+
try {
|
|
52
|
+
return { value: fn(), out };
|
|
53
|
+
} finally {
|
|
54
|
+
process.stdout.write = original;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The account-link boot warnings under test, as greppable fragments. */
|
|
59
|
+
const ALLOWLIST_WARN = "no allowed origin is configured";
|
|
60
|
+
const DUP_WARN = "more than once";
|
|
61
|
+
|
|
62
|
+
function countOf(haystack: string, needle: string): number {
|
|
63
|
+
return haystack.split(needle).length - 1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
test("exposes an empty registry and {} hooks with no config", () => {
|
|
67
|
+
// The inert-when-unconfigured posture: no ACCOUNT_LINK_* env var (deleted
|
|
68
|
+
// above) and no `accountLinks` option ⇒ no operator intent, so NOTHING
|
|
69
|
+
// registers — not even the credential-free steam preset — and no
|
|
70
|
+
// account-link warning fires on a deploy that never asked for the feature.
|
|
71
|
+
const { value: client, out } = captureBoot(() => createHogsendClient());
|
|
72
|
+
assert.equal(client.accountLinkProviders.count(), 0);
|
|
73
|
+
assert.deepEqual(client.accountLinkHooks, {});
|
|
74
|
+
assert.deepEqual(client.accountLinkAllowedOrigins, []);
|
|
75
|
+
assert.equal(out.includes(ALLOWLIST_WARN), false);
|
|
76
|
+
assert.equal(out.includes(DUP_WARN), false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("builds steam when the consumer passes an accountLinks option", () => {
|
|
80
|
+
// Code intent: ANY `accountLinks` option — even `{}` — opts the deploy in,
|
|
81
|
+
// and the credential-free steam preset registers.
|
|
82
|
+
const { value: client } = captureBoot(() =>
|
|
83
|
+
createHogsendClient({ accountLinks: {} }),
|
|
84
|
+
);
|
|
85
|
+
assert.deepEqual(client.accountLinkProviders.ids(), ["steam"]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("registers a consumer-supplied provider", () => {
|
|
89
|
+
const steam = stubProvider("steam");
|
|
90
|
+
const { value: client } = captureBoot(() =>
|
|
91
|
+
createHogsendClient({
|
|
92
|
+
accountLinks: {
|
|
93
|
+
providers: [steam],
|
|
94
|
+
allowedOrigins: ["https://play.example.com"],
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
assert.equal(client.accountLinkProviders.count(), 1);
|
|
99
|
+
assert.equal(client.accountLinkProviders.get("steam"), steam);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("keeps the last of two consumer providers sharing an id, and warns once", () => {
|
|
103
|
+
const first = stubProvider("twitch", "first");
|
|
104
|
+
const second = stubProvider("twitch", "second");
|
|
105
|
+
const { value: client, out } = captureBoot(() =>
|
|
106
|
+
createHogsendClient({
|
|
107
|
+
accountLinks: {
|
|
108
|
+
providers: [first, second],
|
|
109
|
+
allowedOrigins: ["https://play.example.com"],
|
|
110
|
+
},
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
// 2 = the unconditional steam env preset + the (deduped) consumer twitch.
|
|
114
|
+
assert.equal(client.accountLinkProviders.count(), 2);
|
|
115
|
+
assert.equal(client.accountLinkProviders.get("twitch"), second);
|
|
116
|
+
assert.equal(countOf(out, DUP_WARN), 1);
|
|
117
|
+
// The warn names the duplicated id.
|
|
118
|
+
assert.match(out, /"twitch"/);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a consumer provider of the same id overrides the env preset", () => {
|
|
122
|
+
const steam = stubProvider("steam", "consumer-steam");
|
|
123
|
+
const { value: client } = captureBoot(() =>
|
|
124
|
+
createHogsendClient({
|
|
125
|
+
accountLinks: {
|
|
126
|
+
providers: [steam],
|
|
127
|
+
allowedOrigins: ["https://play.example.com"],
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
// Env presets merge FIRST, consumer last — last-writer-wins on meta.id, so
|
|
132
|
+
// the registry holds the consumer's steam, not the env preset's.
|
|
133
|
+
assert.equal(client.accountLinkProviders.count(), 1);
|
|
134
|
+
assert.equal(client.accountLinkProviders.get("steam"), steam);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("exposes accountLinkHooks verbatim", () => {
|
|
138
|
+
const hooks: AccountLinkHooks = {
|
|
139
|
+
beforeLink: () => ({ allow: false, reason: "test" }),
|
|
140
|
+
afterLink: () => {},
|
|
141
|
+
};
|
|
142
|
+
const { value: client } = captureBoot(() =>
|
|
143
|
+
createHogsendClient({ accountLinks: { hooks } }),
|
|
144
|
+
);
|
|
145
|
+
// Reference equality: held verbatim, not wrapped, not per-hook defaulted.
|
|
146
|
+
assert.equal(client.accountLinkHooks, hooks);
|
|
147
|
+
assert.equal(client.accountLinkHooks.beforeLink, hooks.beforeLink);
|
|
148
|
+
assert.equal(client.accountLinkHooks.afterUnlink, undefined);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("throws at boot on a malformed allowed origin", () => {
|
|
152
|
+
assert.throws(
|
|
153
|
+
() =>
|
|
154
|
+
captureBoot(() =>
|
|
155
|
+
createHogsendClient({
|
|
156
|
+
accountLinks: {
|
|
157
|
+
providers: [stubProvider("steam")],
|
|
158
|
+
allowedOrigins: ["https://x.example.com/cb"],
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
),
|
|
162
|
+
/"https:\/\/x\.example\.com\/cb"/,
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("warns when providers are registered but the allowlist is empty", () => {
|
|
167
|
+
const { value: client, out } = captureBoot(() =>
|
|
168
|
+
createHogsendClient({
|
|
169
|
+
accountLinks: { providers: [stubProvider("steam")] },
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
assert.equal(client.accountLinkProviders.count(), 1);
|
|
173
|
+
assert.deepEqual(client.accountLinkAllowedOrigins, []);
|
|
174
|
+
assert.equal(countOf(out, ALLOWLIST_WARN), 1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("the empty-allowlist warn fires for an opted-in deploy with no allowlist", () => {
|
|
178
|
+
// A deploy that ASKED for account links (`accountLinks: {}` opts in and
|
|
179
|
+
// registers steam) but configured no allowed origin is a genuine
|
|
180
|
+
// misconfiguration: no returnTo is accepted and postMessage never fires,
|
|
181
|
+
// so it warns once. The bare-boot case above stays silent — nothing
|
|
182
|
+
// registers without intent.
|
|
183
|
+
const { out } = captureBoot(() => createHogsendClient({ accountLinks: {} }));
|
|
184
|
+
assert.equal(countOf(out, ALLOWLIST_WARN), 1);
|
|
185
|
+
});
|