@hogsend/engine 0.64.0 → 0.66.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/battlenet.ts +84 -0
- package/src/account-links/epic.ts +87 -0
- package/src/account-links/index.ts +15 -0
- package/src/account-links/riot.ts +82 -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/account-links/xbox.ts +92 -0
- package/src/app.ts +9 -1
- 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 +53 -0
- package/src/index.ts +93 -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 +190 -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 +342 -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/snippet.ts +76 -0
- 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 +81 -0
- package/src/routes/snippet.test.ts +70 -0
- package/src/routes/snippet.ts +58 -0
- package/src/testing-barrel.test.ts +74 -0
- package/src/testing.ts +17 -0
- package/src/workflows/import-contacts.ts +6 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
AccountLinkCallbackError,
|
|
5
|
+
hours,
|
|
6
|
+
STEAM_OPENID_ENDPOINT,
|
|
7
|
+
steamReturnTo,
|
|
8
|
+
} from "@hogsend/core";
|
|
9
|
+
import { fakeFetch } from "./__fixtures__/fake-fetch.js";
|
|
10
|
+
import steamFixtures from "./__fixtures__/steam.json" with { type: "json" };
|
|
11
|
+
import { steamAccountLink } from "./steam.js";
|
|
12
|
+
|
|
13
|
+
const REALM = "https://api.example.com";
|
|
14
|
+
const CALLBACK = "https://api.example.com/v1/accounts/steam/callback";
|
|
15
|
+
const STATE = "signed-state-token";
|
|
16
|
+
// The redirectUri the callback route presents = the return_to it minted
|
|
17
|
+
// (state included) — handleCallback compares the echo byte-for-byte.
|
|
18
|
+
const RETURN_TO = steamReturnTo(CALLBACK, STATE);
|
|
19
|
+
const STEAM_ID = "76561197960435530";
|
|
20
|
+
|
|
21
|
+
/** A well-formed positive assertion; tests override fields to break it. */
|
|
22
|
+
function provenQuery(
|
|
23
|
+
overrides: Record<string, string> = {},
|
|
24
|
+
): Record<string, string> {
|
|
25
|
+
return {
|
|
26
|
+
"openid.ns": "http://specs.openid.net/auth/2.0",
|
|
27
|
+
"openid.mode": "id_res",
|
|
28
|
+
"openid.op_endpoint": STEAM_OPENID_ENDPOINT,
|
|
29
|
+
"openid.claimed_id": `https://steamcommunity.com/openid/id/${STEAM_ID}`,
|
|
30
|
+
"openid.identity": `https://steamcommunity.com/openid/id/${STEAM_ID}`,
|
|
31
|
+
"openid.return_to": RETURN_TO,
|
|
32
|
+
"openid.response_nonce": "2026-08-13T00:00:00Znonce",
|
|
33
|
+
"openid.assoc_handle": "1234567890",
|
|
34
|
+
"openid.signed":
|
|
35
|
+
"signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle",
|
|
36
|
+
"openid.sig": "fixture-signature",
|
|
37
|
+
state: STATE,
|
|
38
|
+
...overrides,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const CHECK_AUTH_ROUTE = `POST ${STEAM_OPENID_ENDPOINT}`;
|
|
43
|
+
const SUMMARIES_ROUTE =
|
|
44
|
+
"GET https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/";
|
|
45
|
+
|
|
46
|
+
test("authorizeUrl carries identifier_select, the configured realm and a return_to containing the state", async () => {
|
|
47
|
+
const url = new URL(
|
|
48
|
+
await steamAccountLink({ realm: REALM }).authorizeUrl({
|
|
49
|
+
state: STATE,
|
|
50
|
+
redirectUri: CALLBACK,
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
assert.equal(url.origin + url.pathname, STEAM_OPENID_ENDPOINT);
|
|
54
|
+
assert.equal(url.searchParams.get("openid.mode"), "checkid_setup");
|
|
55
|
+
assert.equal(
|
|
56
|
+
url.searchParams.get("openid.claimed_id"),
|
|
57
|
+
"http://specs.openid.net/auth/2.0/identifier_select",
|
|
58
|
+
);
|
|
59
|
+
assert.equal(
|
|
60
|
+
url.searchParams.get("openid.identity"),
|
|
61
|
+
"http://specs.openid.net/auth/2.0/identifier_select",
|
|
62
|
+
);
|
|
63
|
+
assert.equal(url.searchParams.get("openid.realm"), REALM);
|
|
64
|
+
const returnTo = url.searchParams.get("openid.return_to");
|
|
65
|
+
assert.ok(returnTo);
|
|
66
|
+
assert.equal(new URL(returnTo).searchParams.get("state"), STATE);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("a proven callback yields a 17-digit providerUserId, no verifiedEmail and no tokens", async () => {
|
|
70
|
+
const { fetchImpl, calls } = fakeFetch({
|
|
71
|
+
[CHECK_AUTH_ROUTE]: { text: steamFixtures.checkAuthenticationValid },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const identity = await steamAccountLink({ realm: REALM }).handleCallback({
|
|
75
|
+
query: provenQuery(),
|
|
76
|
+
redirectUri: RETURN_TO,
|
|
77
|
+
fetchImpl,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
assert.match(identity.providerUserId, /^\d{17}$/);
|
|
81
|
+
assert.equal(identity.providerUserId, STEAM_ID);
|
|
82
|
+
assert.equal(identity.verifiedEmail, undefined);
|
|
83
|
+
assert.equal(identity.tokens, undefined);
|
|
84
|
+
|
|
85
|
+
// The proof round-trip went to Steam's HARDCODED endpoint (never a
|
|
86
|
+
// callback-supplied op_endpoint) with the mode rewritten.
|
|
87
|
+
assert.equal(calls.length, 1);
|
|
88
|
+
assert.equal(calls[0]?.url, STEAM_OPENID_ENDPOINT);
|
|
89
|
+
const body = new URLSearchParams(calls[0]?.body ?? "");
|
|
90
|
+
assert.equal(body.get("openid.mode"), "check_authentication");
|
|
91
|
+
assert.equal(body.get("openid.sig"), "fixture-signature");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("an is_valid:false answer is exchange_failed, not a link", async () => {
|
|
95
|
+
const { fetchImpl } = fakeFetch({
|
|
96
|
+
[CHECK_AUTH_ROUTE]: { text: steamFixtures.checkAuthenticationInvalid },
|
|
97
|
+
});
|
|
98
|
+
await assert.rejects(
|
|
99
|
+
() =>
|
|
100
|
+
steamAccountLink({ realm: REALM }).handleCallback({
|
|
101
|
+
query: provenQuery(),
|
|
102
|
+
redirectUri: RETURN_TO,
|
|
103
|
+
fetchImpl,
|
|
104
|
+
}),
|
|
105
|
+
(err: unknown) =>
|
|
106
|
+
err instanceof AccountLinkCallbackError &&
|
|
107
|
+
err.reason === "exchange_failed",
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("a web api key adds the GetPlayerSummaries profile pull", async () => {
|
|
112
|
+
const { fetchImpl, calls } = fakeFetch({
|
|
113
|
+
[CHECK_AUTH_ROUTE]: { text: steamFixtures.checkAuthenticationValid },
|
|
114
|
+
[SUMMARIES_ROUTE]: { body: steamFixtures.playerSummaries },
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const identity = await steamAccountLink({
|
|
118
|
+
realm: REALM,
|
|
119
|
+
webApiKey: "fixture-web-api-key",
|
|
120
|
+
}).handleCallback({
|
|
121
|
+
query: provenQuery(),
|
|
122
|
+
redirectUri: RETURN_TO,
|
|
123
|
+
fetchImpl,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
assert.equal(identity.providerUserId, STEAM_ID);
|
|
127
|
+
assert.equal(identity.username, "Robin");
|
|
128
|
+
assert.equal(identity.avatarUrl, "https://avatars.steamstatic.com/full.jpg");
|
|
129
|
+
const summaries = calls.find((c) => c.url.includes("GetPlayerSummaries"));
|
|
130
|
+
assert.ok(summaries);
|
|
131
|
+
const url = new URL(summaries.url);
|
|
132
|
+
assert.equal(url.searchParams.get("steamids"), STEAM_ID);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("a failing GetPlayerSummaries still yields a proven identity with no username", async () => {
|
|
136
|
+
const { fetchImpl } = fakeFetch({
|
|
137
|
+
[CHECK_AUTH_ROUTE]: { text: steamFixtures.checkAuthenticationValid },
|
|
138
|
+
[SUMMARIES_ROUTE]: { status: 500, body: { error: "upstream down" } },
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const identity = await steamAccountLink({
|
|
142
|
+
realm: REALM,
|
|
143
|
+
webApiKey: "fixture-web-api-key",
|
|
144
|
+
}).handleCallback({
|
|
145
|
+
query: provenQuery(),
|
|
146
|
+
redirectUri: RETURN_TO,
|
|
147
|
+
fetchImpl,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// A cosmetic pull must never fail a proven link.
|
|
151
|
+
assert.equal(identity.providerUserId, STEAM_ID);
|
|
152
|
+
assert.equal(identity.username, undefined);
|
|
153
|
+
assert.equal(identity.avatarUrl, undefined);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("sync is present because a web api key is configured, and its every is hours(24)", () => {
|
|
157
|
+
const withKey = steamAccountLink({ realm: REALM, webApiKey: "k" });
|
|
158
|
+
assert.equal(typeof withKey.sync?.read, "function");
|
|
159
|
+
assert.deepEqual(withKey.sync?.every, hours(24));
|
|
160
|
+
// The widen-not-enable proof from the provider side: no key, no sync — but
|
|
161
|
+
// the provider itself still exists and links.
|
|
162
|
+
const withoutKey = steamAccountLink({ realm: REALM });
|
|
163
|
+
assert.equal(withoutKey.sync, undefined);
|
|
164
|
+
assert.equal(withoutKey.capabilities?.tokens, undefined);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("the security assertions hold end to end", async () => {
|
|
168
|
+
const provider = steamAccountLink({ realm: REALM });
|
|
169
|
+
|
|
170
|
+
// Foreign op_endpoint: refused BEFORE any network call — the round-trip is
|
|
171
|
+
// never posted to a verifier the attacker names.
|
|
172
|
+
{
|
|
173
|
+
const { fetchImpl, calls } = fakeFetch({});
|
|
174
|
+
await assert.rejects(
|
|
175
|
+
() =>
|
|
176
|
+
provider.handleCallback({
|
|
177
|
+
query: provenQuery({
|
|
178
|
+
"openid.op_endpoint": "https://evil.example.com/openid/login",
|
|
179
|
+
}),
|
|
180
|
+
redirectUri: RETURN_TO,
|
|
181
|
+
fetchImpl,
|
|
182
|
+
}),
|
|
183
|
+
(err: unknown) =>
|
|
184
|
+
err instanceof AccountLinkCallbackError &&
|
|
185
|
+
err.reason === "state_invalid",
|
|
186
|
+
);
|
|
187
|
+
assert.equal(calls.length, 0);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Mismatched return_to: a cross-flow replay, refused before any network.
|
|
191
|
+
{
|
|
192
|
+
const { fetchImpl, calls } = fakeFetch({});
|
|
193
|
+
await assert.rejects(
|
|
194
|
+
() =>
|
|
195
|
+
provider.handleCallback({
|
|
196
|
+
query: provenQuery({
|
|
197
|
+
"openid.return_to": steamReturnTo(CALLBACK, "other-state"),
|
|
198
|
+
}),
|
|
199
|
+
redirectUri: RETURN_TO,
|
|
200
|
+
fetchImpl,
|
|
201
|
+
}),
|
|
202
|
+
(err: unknown) =>
|
|
203
|
+
err instanceof AccountLinkCallbackError &&
|
|
204
|
+
err.reason === "state_invalid",
|
|
205
|
+
);
|
|
206
|
+
assert.equal(calls.length, 0);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// mode=cancel: the player's own "no" is denied, never state_invalid.
|
|
210
|
+
{
|
|
211
|
+
const { fetchImpl, calls } = fakeFetch({});
|
|
212
|
+
await assert.rejects(
|
|
213
|
+
() =>
|
|
214
|
+
provider.handleCallback({
|
|
215
|
+
query: { "openid.mode": "cancel", state: STATE },
|
|
216
|
+
redirectUri: RETURN_TO,
|
|
217
|
+
fetchImpl,
|
|
218
|
+
}),
|
|
219
|
+
(err: unknown) =>
|
|
220
|
+
err instanceof AccountLinkCallbackError && err.reason === "denied",
|
|
221
|
+
);
|
|
222
|
+
assert.equal(calls.length, 0);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Malformed claimed_id (prefix smuggling): parsed and refused pre-network.
|
|
226
|
+
{
|
|
227
|
+
const { fetchImpl, calls } = fakeFetch({});
|
|
228
|
+
await assert.rejects(
|
|
229
|
+
() =>
|
|
230
|
+
provider.handleCallback({
|
|
231
|
+
query: provenQuery({
|
|
232
|
+
"openid.claimed_id": `https://evil.example/?x=https://steamcommunity.com/openid/id/${STEAM_ID}`,
|
|
233
|
+
}),
|
|
234
|
+
redirectUri: RETURN_TO,
|
|
235
|
+
fetchImpl,
|
|
236
|
+
}),
|
|
237
|
+
(err: unknown) =>
|
|
238
|
+
err instanceof AccountLinkCallbackError &&
|
|
239
|
+
err.reason === "state_invalid",
|
|
240
|
+
);
|
|
241
|
+
assert.equal(calls.length, 0);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type AccountLinkProvider, steamOpenIdLink } from "@hogsend/core";
|
|
2
|
+
|
|
3
|
+
export interface SteamAccountLinkConfig {
|
|
4
|
+
/**
|
|
5
|
+
* Steam Web API key (`https://steamcommunity.com/dev/apikey`). OPTIONAL —
|
|
6
|
+
* it never gates the link itself. Absent: linking works and the identity is
|
|
7
|
+
* the bare 17-digit steamid64. Present: the best-effort
|
|
8
|
+
* `GetPlayerSummaries` pull fills `username`/`avatarUrl` and the
|
|
9
|
+
* `steam_playtime_2wk` property sync attaches.
|
|
10
|
+
*/
|
|
11
|
+
webApiKey?: string;
|
|
12
|
+
/** The `openid.realm` this deployment presents. `API_PUBLIC_URL`, trailing slash stripped. */
|
|
13
|
+
realm: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The Steam account-link provider: pure CONFIG over the `steamOpenIdLink()`
|
|
18
|
+
* preset. "Sign in through Steam" is OpenID 2.0 `checkid_setup`, so three
|
|
19
|
+
* things a reader will come looking for are structurally absent:
|
|
20
|
+
*
|
|
21
|
+
* - There is NO `code` and NO PKCE — OpenID 2.0 has neither, so PRD 07 skips
|
|
22
|
+
* its PKCE mint for this provider (`capabilities.pkce` absent).
|
|
23
|
+
* - There is NO token — OpenID 2.0 issues nothing to store, so
|
|
24
|
+
* `capabilities.tokens` is absent and PRD 03 writes a null
|
|
25
|
+
* `linked_accounts.tokens`.
|
|
26
|
+
* - The relying party presents NO credential: no app registration, no client
|
|
27
|
+
* id, no secret. The entire security model is the preset's server-side
|
|
28
|
+
* `check_authentication` round-trip against Steam's HARDCODED endpoint
|
|
29
|
+
* (`STEAM_OPENID_ENDPOINT`), never the callback-echoed `openid.op_endpoint`.
|
|
30
|
+
*
|
|
31
|
+
* All protocol mechanics — the `checkid_setup` URL, the round-trip, the strict
|
|
32
|
+
* `parseSteamClaimedId` anchor, the `return_to` echo check, the `mode=cancel`
|
|
33
|
+
* mapping, the playtime sync reader — live in the preset
|
|
34
|
+
* (`@hogsend/core`, `providers/account-link-presets.ts`), not here.
|
|
35
|
+
*/
|
|
36
|
+
export function steamAccountLink(
|
|
37
|
+
config: SteamAccountLinkConfig,
|
|
38
|
+
): AccountLinkProvider {
|
|
39
|
+
return steamOpenIdLink({
|
|
40
|
+
meta: {
|
|
41
|
+
id: "steam",
|
|
42
|
+
name: "Steam",
|
|
43
|
+
description: "Sign in through Steam (OpenID 2.0)",
|
|
44
|
+
},
|
|
45
|
+
realm: config.realm,
|
|
46
|
+
...(config.webApiKey ? { webApiKey: config.webApiKey } : {}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -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,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AccountLinkCallbackError,
|
|
3
|
+
type AccountLinkProvider,
|
|
4
|
+
type LinkedIdentity,
|
|
5
|
+
oauth2Link,
|
|
6
|
+
} from "@hogsend/core";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Microsoft (Xbox) app credentials, from an application registered at
|
|
10
|
+
* `https://portal.azure.com` → App registrations with OAuth redirect URL
|
|
11
|
+
* `<API_PUBLIC_URL>/v1/accounts/xbox/callback`. The app MUST allow
|
|
12
|
+
* "personal Microsoft accounts" (consumers tenant). Env spelling:
|
|
13
|
+
* `ACCOUNT_LINK_XBOX_CLIENT_ID` / `ACCOUNT_LINK_XBOX_CLIENT_SECRET`
|
|
14
|
+
* (see `lib/account-links-from-env.ts` — registered only when BOTH are set).
|
|
15
|
+
*/
|
|
16
|
+
export interface XboxAccountLinkConfig {
|
|
17
|
+
clientId: string;
|
|
18
|
+
clientSecret: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The slice of a Microsoft Graph `GET /v1.0/me` response this mapping reads. */
|
|
22
|
+
interface GraphUser {
|
|
23
|
+
id?: string;
|
|
24
|
+
displayName?: string;
|
|
25
|
+
userPrincipalName?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Map a Microsoft Graph `GET /v1.0/me` response onto a {@link LinkedIdentity}.
|
|
30
|
+
* Exported for direct unit testing — it is the only real logic in this file.
|
|
31
|
+
*
|
|
32
|
+
* - `providerUserId` is Graph's `id` (the IMMUTABLE Azure AD object id). This
|
|
33
|
+
* is the same identity that owns the Xbox account — personal Microsoft
|
|
34
|
+
* accounts are the backing identity for Xbox Live.
|
|
35
|
+
* - `userPrincipalName` is a PROPERTY, never an identity key and never
|
|
36
|
+
* `verifiedEmail`: Graph returns the primary email but DECISIONS §6.4 makes
|
|
37
|
+
* a provider-reported email a property regardless. It lands in
|
|
38
|
+
* `properties.xbox_email`.
|
|
39
|
+
*/
|
|
40
|
+
export function mapXboxUser(json: unknown): Omit<LinkedIdentity, "tokens"> {
|
|
41
|
+
const user = json as GraphUser | null | undefined;
|
|
42
|
+
if (!user?.id) {
|
|
43
|
+
throw new AccountLinkCallbackError(
|
|
44
|
+
"exchange_failed",
|
|
45
|
+
"Microsoft Graph /me returned no id for the presented token",
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
providerUserId: user.id,
|
|
50
|
+
...(user.displayName ? { username: user.displayName } : {}),
|
|
51
|
+
...(user.userPrincipalName
|
|
52
|
+
? { properties: { xbox_email: user.userPrincipalName } }
|
|
53
|
+
: {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The Xbox account-link provider: pure CONFIG over the `oauth2Link()` preset
|
|
59
|
+
* (OAuth2 authorization code + PKCE, tokens sealed). All protocol mechanics —
|
|
60
|
+
* authorize URL, token exchange, `error=access_denied` mapping, the userinfo
|
|
61
|
+
* fetch — live in the preset (`@hogsend/core`, `providers/account-link-presets.ts`),
|
|
62
|
+
* not here.
|
|
63
|
+
*
|
|
64
|
+
* Uses the Microsoft `/consumers` tenant so ONLY personal Microsoft accounts
|
|
65
|
+
* (which back Xbox Live) are accepted — work/school accounts are rejected at
|
|
66
|
+
* the authorize step by Microsoft itself.
|
|
67
|
+
*/
|
|
68
|
+
export function xboxAccountLink(
|
|
69
|
+
config: XboxAccountLinkConfig,
|
|
70
|
+
): AccountLinkProvider {
|
|
71
|
+
return oauth2Link({
|
|
72
|
+
meta: {
|
|
73
|
+
id: "xbox",
|
|
74
|
+
name: "Xbox",
|
|
75
|
+
description: "Link an Xbox account (OAuth2 via Microsoft)",
|
|
76
|
+
},
|
|
77
|
+
authorizeEndpoint:
|
|
78
|
+
"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize",
|
|
79
|
+
tokenEndpoint:
|
|
80
|
+
"https://login.microsoftonline.com/consumers/oauth2/v2.0/token",
|
|
81
|
+
clientId: config.clientId,
|
|
82
|
+
clientSecret: config.clientSecret,
|
|
83
|
+
scopes: ["User.Read", "offline_access"],
|
|
84
|
+
usePkce: true,
|
|
85
|
+
storeTokens: true,
|
|
86
|
+
authorizeParams: { prompt: "select_account" },
|
|
87
|
+
userInfo: {
|
|
88
|
+
url: "https://graph.microsoft.com/v1.0/me",
|
|
89
|
+
map: mapXboxUser,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
package/src/app.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { errorHandler } from "./middleware/error-handler.js";
|
|
|
16
16
|
import { clientIpKey, createRateLimit } from "./middleware/rate-limit.js";
|
|
17
17
|
import { requestLogger } from "./middleware/request-logger.js";
|
|
18
18
|
import { registerRoutes } from "./routes/index.js";
|
|
19
|
+
import { SNIPPET_PATH } from "./routes/snippet.js";
|
|
19
20
|
import {
|
|
20
21
|
type DefinedWebhookSource,
|
|
21
22
|
webhookSourceToConnector,
|
|
@@ -81,7 +82,14 @@ export function createApp(
|
|
|
81
82
|
await next();
|
|
82
83
|
});
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
// secureHeaders sets `Cross-Origin-Resource-Policy: same-origin` AFTER the
|
|
86
|
+
// handler runs (a route cannot override it), which would block a cross-origin
|
|
87
|
+
// `<script src>` load of the drop-in SDK. That one path is the SDK itself,
|
|
88
|
+
// meant to be embedded on other origins, so it skips the middleware.
|
|
89
|
+
const secure = secureHeaders();
|
|
90
|
+
app.use("*", (c, next) =>
|
|
91
|
+
c.req.path === SNIPPET_PATH ? next() : secure(c, next),
|
|
92
|
+
);
|
|
85
93
|
app.use(
|
|
86
94
|
"*",
|
|
87
95
|
cors({
|