ludicord 2.2.5 → 3.0.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/README.md +8 -4
- package/dist/activity/game-loop.d.ts +16 -0
- package/dist/activity/game-loop.js +33 -0
- package/dist/activity/index.d.ts +12 -0
- package/dist/activity/index.js +6 -0
- package/dist/activity/lifecycle.d.ts +11 -0
- package/dist/activity/lifecycle.js +41 -0
- package/dist/activity/presence.d.ts +10 -0
- package/dist/activity/presence.js +15 -0
- package/dist/activity/query.d.ts +21 -0
- package/dist/activity/query.js +148 -0
- package/dist/activity/shared-state.d.ts +10 -0
- package/dist/activity/shared-state.js +100 -0
- package/dist/activity/storage.d.ts +16 -0
- package/dist/activity/storage.js +88 -0
- package/dist/api/runtime.d.ts +5 -2
- package/dist/api/runtime.js +53 -10
- package/dist/auth/client.d.ts +0 -1
- package/dist/auth/client.js +3 -5
- package/dist/auth/index.d.ts +0 -1
- package/dist/auth/index.js +0 -1
- package/dist/auth/server.d.ts +2 -1
- package/dist/auth/server.js +161 -16
- package/dist/auth/types.d.ts +5 -1
- package/dist/auth/types.js +0 -1
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli/index.js +116 -19
- package/dist/cli/project-commands.d.ts +1 -5
- package/dist/cli/project-commands.js +14 -162
- package/dist/compiler/build.d.ts +10 -1
- package/dist/compiler/build.js +124 -16
- package/dist/compiler/bundle-report.d.ts +17 -0
- package/dist/compiler/bundle-report.js +60 -0
- package/dist/compiler/client-entry.d.ts +0 -1
- package/dist/compiler/client-entry.js +28 -4
- package/dist/compiler/client-tooling.d.ts +0 -1
- package/dist/compiler/client-tooling.js +8 -2
- package/dist/compiler/index.d.ts +3 -1
- package/dist/compiler/index.js +2 -1
- package/dist/compiler/scanner.d.ts +0 -1
- package/dist/compiler/scanner.js +59 -4
- package/dist/compiler/server-bundle.d.ts +1 -2
- package/dist/compiler/server-bundle.js +11 -2
- package/dist/compiler/type-generation.d.ts +0 -1
- package/dist/compiler/type-generation.js +2 -1
- package/dist/compiler/write-manifests.d.ts +0 -1
- package/dist/compiler/write-manifests.js +0 -1
- package/dist/config/defaults.d.ts +0 -1
- package/dist/config/defaults.js +14 -2
- package/dist/config/environment.d.ts +0 -1
- package/dist/config/environment.js +0 -1
- package/dist/config/index.d.ts +0 -1
- package/dist/config/index.js +0 -1
- package/dist/config/loader.d.ts +0 -1
- package/dist/config/loader.js +0 -1
- package/dist/config/project-root.d.ts +0 -1
- package/dist/config/project-root.js +0 -1
- package/dist/config/types.d.ts +25 -1
- package/dist/config/types.js +0 -1
- package/dist/discord/assets.d.ts +5 -0
- package/dist/discord/assets.js +29 -0
- package/dist/discord/data.d.ts +38 -2
- package/dist/discord/data.js +178 -11
- package/dist/discord/events.d.ts +28 -2
- package/dist/discord/events.js +109 -39
- package/dist/discord/index.d.ts +6 -5
- package/dist/discord/index.js +3 -3
- package/dist/discord/payload.d.ts +6 -0
- package/dist/discord/payload.js +5 -0
- package/dist/discord/runtime.d.ts +0 -1
- package/dist/discord/runtime.js +0 -1
- package/dist/discord/safe-area.d.ts +0 -1
- package/dist/discord/safe-area.js +0 -1
- package/dist/discord/testing.d.ts +0 -1
- package/dist/discord/testing.js +0 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/lint/index.d.ts +6 -0
- package/dist/lint/index.js +71 -0
- package/dist/manifests/types.d.ts +11 -1
- package/dist/manifests/types.js +0 -1
- package/dist/metadata/index.d.ts +13 -0
- package/dist/metadata/index.js +4 -0
- package/dist/navigation/index.d.ts +3 -1
- package/dist/navigation/index.js +1 -1
- package/dist/navigation/link.d.ts +8 -0
- package/dist/navigation/link.js +38 -0
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.js +0 -1
- package/dist/runtime/client/dev-panel.d.ts +0 -1
- package/dist/runtime/client/dev-panel.js +722 -38
- package/dist/runtime/client/embed-router.d.ts +8 -1
- package/dist/runtime/client/embed-router.js +116 -62
- package/dist/runtime/client/files.d.ts +5 -1
- package/dist/runtime/client/files.js +0 -1
- package/dist/runtime/client/minimize-card.d.ts +0 -1
- package/dist/runtime/client/minimize-card.js +0 -1
- package/dist/runtime/client/minimize.d.ts +0 -1
- package/dist/runtime/client/minimize.js +0 -1
- package/dist/runtime/client/root.d.ts +5 -3
- package/dist/runtime/client/root.js +28 -3
- package/dist/runtime/index.d.ts +1 -2
- package/dist/runtime/index.js +0 -1
- package/dist/runtime/server/dev-server.d.ts +0 -1
- package/dist/runtime/server/dev-server.js +187 -30
- package/dist/runtime/server/diagnostics.d.ts +1 -1
- package/dist/runtime/server/diagnostics.js +5 -4
- package/dist/runtime/server/index.d.ts +0 -1
- package/dist/runtime/server/index.js +0 -1
- package/dist/runtime/server/production-server.d.ts +2 -1
- package/dist/runtime/server/production-server.js +145 -25
- package/dist/security/discord.d.ts +0 -1
- package/dist/security/discord.js +0 -1
- package/dist/security/index.d.ts +0 -1
- package/dist/security/index.js +0 -1
- package/dist/security/secret-scan.d.ts +0 -1
- package/dist/security/secret-scan.js +0 -1
- package/dist/server/discord-rest.d.ts +42 -7
- package/dist/server/discord-rest.js +135 -18
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +3 -1
- package/dist/server/instrumentation.d.ts +16 -0
- package/dist/server/instrumentation.js +3 -0
- package/dist/server/proxy.d.ts +21 -0
- package/dist/server/proxy.js +76 -0
- package/dist/server/session.d.ts +0 -1
- package/dist/server/session.js +0 -1
- package/dist/server/types.d.ts +0 -1
- package/dist/server/types.js +0 -1
- package/dist/sessions/cookies.d.ts +0 -1
- package/dist/sessions/cookies.js +0 -1
- package/dist/sessions/crypto.d.ts +0 -1
- package/dist/sessions/crypto.js +0 -1
- package/dist/sessions/data-store.d.ts +18 -0
- package/dist/sessions/data-store.js +45 -0
- package/dist/shared/errors.d.ts +0 -1
- package/dist/shared/errors.js +0 -1
- package/dist/testing/index.d.ts +0 -1
- package/dist/testing/index.js +2 -2
- package/dist/version.d.ts +1 -2
- package/dist/version.js +1 -2
- package/dist/ws/client.d.ts +0 -1
- package/dist/ws/client.js +0 -1
- package/dist/ws/index.d.ts +3 -2
- package/dist/ws/index.js +1 -1
- package/dist/ws/server.d.ts +5 -3
- package/dist/ws/server.js +65 -19
- package/dist/ws/shared-state.d.ts +13 -0
- package/dist/ws/shared-state.js +137 -0
- package/package.json +14 -1
package/dist/api/runtime.js
CHANGED
|
@@ -30,6 +30,8 @@ class BodyLimitError extends Error {
|
|
|
30
30
|
}
|
|
31
31
|
class MalformedBodyError extends Error {
|
|
32
32
|
}
|
|
33
|
+
class RouteTimeoutError extends Error {
|
|
34
|
+
}
|
|
33
35
|
async function requestBody(request, limit) {
|
|
34
36
|
if (request.method === "GET" || request.method === "HEAD")
|
|
35
37
|
return undefined;
|
|
@@ -49,7 +51,7 @@ async function requestBody(request, limit) {
|
|
|
49
51
|
}
|
|
50
52
|
return Buffer.concat(chunks);
|
|
51
53
|
}
|
|
52
|
-
function webRequest(request, params, sessionSecret, body, proxyVerified, allowDevelopment) {
|
|
54
|
+
function webRequest(request, params, sessionSecret, body, proxyVerified, allowDevelopment, signal) {
|
|
53
55
|
const origin = `${request.protocol}://${request.get("host") ?? "localhost"}`;
|
|
54
56
|
const headerEntries = [];
|
|
55
57
|
for (const [name, value] of Object.entries(request.headers)) {
|
|
@@ -63,6 +65,7 @@ function webRequest(request, params, sessionSecret, body, proxyVerified, allowDe
|
|
|
63
65
|
const web = new Request(new URL(request.originalUrl, origin), {
|
|
64
66
|
method: request.method,
|
|
65
67
|
headers: new Headers(headerEntries),
|
|
68
|
+
signal,
|
|
66
69
|
...(body === undefined ? {} : { body: body }),
|
|
67
70
|
});
|
|
68
71
|
const parseJson = web.json.bind(web);
|
|
@@ -123,6 +126,12 @@ async function defaultModuleLoader(absoluteFile) {
|
|
|
123
126
|
export function createApiMiddleware(options) {
|
|
124
127
|
const loadModule = options.loadModule ?? defaultModuleLoader;
|
|
125
128
|
const bodyLimit = parseBodyLimit(options.bodyLimit);
|
|
129
|
+
const requestTimeout = options.requestTimeout ?? 30_000;
|
|
130
|
+
if (!Number.isSafeInteger(requestTimeout) ||
|
|
131
|
+
requestTimeout < 1 ||
|
|
132
|
+
requestTimeout > 900_000) {
|
|
133
|
+
throw new Error("LUDICORD8003: server.requestTimeout must be an integer between 1 and 900000 milliseconds.");
|
|
134
|
+
}
|
|
126
135
|
return async (request, response, next) => {
|
|
127
136
|
const pathname = new URL(request.originalUrl, `${request.protocol}://${request.get("host") ?? "localhost"}`).pathname;
|
|
128
137
|
if (!pathname.startsWith("/api/") && pathname !== "/api") {
|
|
@@ -183,20 +192,48 @@ export function createApiMiddleware(options) {
|
|
|
183
192
|
response.status(405).json({ error: "LUDICORD_METHOD_NOT_ALLOWED" });
|
|
184
193
|
return;
|
|
185
194
|
}
|
|
186
|
-
const
|
|
195
|
+
const controller = new AbortController();
|
|
196
|
+
const abortRequest = () => {
|
|
197
|
+
if (!controller.signal.aborted) {
|
|
198
|
+
controller.abort(new Error("The client disconnected."));
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const abortUnfinishedResponse = () => {
|
|
202
|
+
if (!response.writableEnded)
|
|
203
|
+
abortRequest();
|
|
204
|
+
};
|
|
205
|
+
request.once("aborted", abortRequest);
|
|
206
|
+
response.once("close", abortUnfinishedResponse);
|
|
207
|
+
const ludicordRequest = webRequest(request, selected.match.params, options.sessionSecret, body, proxyVerified, options.allowDevelopmentSessions === true, controller.signal);
|
|
187
208
|
if (module.public !== true &&
|
|
188
209
|
module.auth !== false &&
|
|
189
210
|
ludicordRequest.ludicord === null) {
|
|
190
211
|
response.status(401).json({ error: "LUDICORD_AUTH_REQUIRED" });
|
|
191
212
|
return;
|
|
192
213
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
214
|
+
let timeout;
|
|
215
|
+
try {
|
|
216
|
+
const result = await Promise.race([
|
|
217
|
+
Promise.resolve(handler(ludicordRequest, {
|
|
218
|
+
params: selected.match.params,
|
|
219
|
+
})),
|
|
220
|
+
new Promise((_resolve, reject) => {
|
|
221
|
+
timeout = setTimeout(() => {
|
|
222
|
+
controller.abort(new Error("Ludicord API route timed out."));
|
|
223
|
+
reject(new RouteTimeoutError());
|
|
224
|
+
}, requestTimeout);
|
|
225
|
+
}),
|
|
226
|
+
]);
|
|
227
|
+
if (!(result instanceof Response)) {
|
|
228
|
+
throw new TypeError("LUDICORD8001: API handlers must return a Response.");
|
|
229
|
+
}
|
|
230
|
+
await sendWebResponse(response, result, method === "HEAD");
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
clearTimeout(timeout);
|
|
234
|
+
request.off("aborted", abortRequest);
|
|
235
|
+
response.off("close", abortUnfinishedResponse);
|
|
198
236
|
}
|
|
199
|
-
await sendWebResponse(response, result, method === "HEAD");
|
|
200
237
|
}
|
|
201
238
|
catch (error) {
|
|
202
239
|
if (error instanceof BodyLimitError) {
|
|
@@ -208,9 +245,15 @@ export function createApiMiddleware(options) {
|
|
|
208
245
|
response.status(400).json({ error: "LUDICORD_MALFORMED_BODY" });
|
|
209
246
|
return;
|
|
210
247
|
}
|
|
211
|
-
|
|
248
|
+
if (error instanceof RouteTimeoutError) {
|
|
249
|
+
response.status(504).json({ error: "LUDICORD_ROUTE_TIMEOUT" });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
await options.onError?.(error, selected.route.file, {
|
|
253
|
+
method: request.method,
|
|
254
|
+
url: request.originalUrl,
|
|
255
|
+
});
|
|
212
256
|
next(error);
|
|
213
257
|
}
|
|
214
258
|
};
|
|
215
259
|
}
|
|
216
|
-
//# sourceMappingURL=runtime.js.map
|
package/dist/auth/client.d.ts
CHANGED
package/dist/auth/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useMemo, useSyncExternalStore } from "react";
|
|
2
2
|
import { getDiscordSnapshot, } from "../discord/runtime.js";
|
|
3
3
|
import { setDiscordUserFromAuth, setDiscordGuildFromAuth, initializeDiscordEventsFromEnvironment, enrichDiscordContext, addDiscordDiagnostic } from "../discord/events.js";
|
|
4
|
+
import { discordUserAvatarUrl } from "../discord/assets.js";
|
|
4
5
|
class LudicordAuthStore {
|
|
5
6
|
snapshot = {
|
|
6
7
|
status: "idle",
|
|
@@ -133,11 +134,9 @@ async function performAuthentication(options) {
|
|
|
133
134
|
throw new Error("LUDICORD6004: Invalid confirmed session.");
|
|
134
135
|
setDiscordUserFromAuth({
|
|
135
136
|
...session.user,
|
|
136
|
-
avatar: session.user.
|
|
137
|
-
? session.user.avatar
|
|
138
|
-
: `https://cdn.discordapp.com/avatars/${session.user.id}/${session.user.avatar}.png`,
|
|
137
|
+
avatar: discordUserAvatarUrl(session.user.id, session.user.avatar),
|
|
139
138
|
});
|
|
140
|
-
setDiscordGuildFromAuth(session.guildId, session.guildName, session.guildIcon);
|
|
139
|
+
setDiscordGuildFromAuth(session.guildId, session.guildName, session.guildIcon, session.guildRaw);
|
|
141
140
|
authStore.update("authenticated", session, null);
|
|
142
141
|
void initializeDiscordEventsFromEnvironment(discord, session.scopes).catch(() => addDiscordDiagnostic("Some Discord capabilities are unavailable."));
|
|
143
142
|
if (globalThis.__LUDICORD_PUBLIC_CONFIG__?.discordServerData) {
|
|
@@ -229,4 +228,3 @@ export function resetLudicordAuthForTesting() {
|
|
|
229
228
|
authStore.dispose();
|
|
230
229
|
authStore = new LudicordAuthStore();
|
|
231
230
|
}
|
|
232
|
-
//# sourceMappingURL=client.js.map
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
export { authenticateLudicord, initializeLudicordAuthFromEnvironment, useAuthError, useAuthStatus, useLudicordSession, useLudicordAuth, logoutLudicord, } from "./client.js";
|
|
2
2
|
export type { LudicordAuthSnapshot, LudicordAuthStatus, LudicordSession, LudicordSessionUser, } from "./types.js";
|
|
3
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/auth/index.js
CHANGED
package/dist/auth/server.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Router } from "express";
|
|
2
|
+
import type { LudicordSessionDataStore } from "../sessions/data-store.js";
|
|
2
3
|
export interface LudicordAuthServerOptions {
|
|
3
4
|
readonly clientId?: string;
|
|
4
5
|
readonly clientSecret?: string;
|
|
@@ -9,6 +10,6 @@ export interface LudicordAuthServerOptions {
|
|
|
9
10
|
readonly activityInstanceVerification?: boolean;
|
|
10
11
|
readonly botToken?: string;
|
|
11
12
|
readonly activityInstanceCacheTtlMs?: number;
|
|
13
|
+
readonly sessionDataStore?: LudicordSessionDataStore;
|
|
12
14
|
}
|
|
13
15
|
export declare function createLudicordAuthRouter(options?: LudicordAuthServerOptions): Router;
|
|
14
|
-
//# sourceMappingURL=server.d.ts.map
|
package/dist/auth/server.js
CHANGED
|
@@ -4,6 +4,40 @@ import { OAUTH_STATE_COOKIE, PENDING_SESSION_COOKIE, SESSION_COOKIE, clearCookie
|
|
|
4
4
|
import { safeEqual, sealLudicordSession, unsealLudicordSession } from "../sessions/crypto.js";
|
|
5
5
|
import { verifyDiscordActivityInstance } from "../security/discord.js";
|
|
6
6
|
import { createDiscordRest, LudicordDiscordRestError } from "../server/discord-rest.js";
|
|
7
|
+
import { createMemorySessionDataStore } from "../sessions/data-store.js";
|
|
8
|
+
import { discordGuildIconUrl, discordUserAvatarUrl } from "../discord/assets.js";
|
|
9
|
+
function compactSession(session, dataId) {
|
|
10
|
+
const { raw: _userRaw, ...user } = session.user;
|
|
11
|
+
const { guildRaw: _guildRaw, ...claims } = session;
|
|
12
|
+
return { ...claims, user, __ludicordDataId: dataId };
|
|
13
|
+
}
|
|
14
|
+
function sessionDataId(session) {
|
|
15
|
+
const value = session.__ludicordDataId;
|
|
16
|
+
return typeof value === "string" && /^[A-Za-z0-9_-]{20,}$/.test(value) ? value : undefined;
|
|
17
|
+
}
|
|
18
|
+
async function hydrateSession(session, store) {
|
|
19
|
+
const id = sessionDataId(session);
|
|
20
|
+
if (id === undefined)
|
|
21
|
+
return session;
|
|
22
|
+
const { __ludicordDataId: _dataId, ...publicSession } = session;
|
|
23
|
+
const data = await store.get(id);
|
|
24
|
+
if (data === null)
|
|
25
|
+
return publicSession;
|
|
26
|
+
return {
|
|
27
|
+
...publicSession,
|
|
28
|
+
user: {
|
|
29
|
+
...publicSession.user,
|
|
30
|
+
...(data.userRaw === undefined ? {} : { raw: data.userRaw }),
|
|
31
|
+
},
|
|
32
|
+
...(data.guildRaw === undefined ? {} : { guildRaw: data.guildRaw }),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function extendedSessionData(session) {
|
|
36
|
+
return {
|
|
37
|
+
...(session.user.raw === undefined ? {} : { userRaw: session.user.raw }),
|
|
38
|
+
...(session.guildRaw === undefined ? {} : { guildRaw: session.guildRaw }),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
7
41
|
function optionalString(value) {
|
|
8
42
|
return typeof value === "string" && value !== "" ? value : undefined;
|
|
9
43
|
}
|
|
@@ -12,7 +46,8 @@ function safeUser(user) {
|
|
|
12
46
|
id: user.id,
|
|
13
47
|
username: user.username,
|
|
14
48
|
displayName: user.global_name ?? user.username,
|
|
15
|
-
avatar: user.avatar
|
|
49
|
+
avatar: discordUserAvatarUrl(user.id, user.avatar),
|
|
50
|
+
raw: { ...user },
|
|
16
51
|
};
|
|
17
52
|
}
|
|
18
53
|
function unavailable(response) {
|
|
@@ -39,6 +74,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
39
74
|
const verifyActivityInstance = options.activityInstanceVerification ?? false;
|
|
40
75
|
const botToken = options.botToken ?? process.env.LUDICORD_DISCORD_BOT_TOKEN;
|
|
41
76
|
const rest = createDiscordRest({ ...(botToken === undefined ? {} : { botToken }), fetch: fetchImplementation });
|
|
77
|
+
const sessionDataStore = options.sessionDataStore ?? createMemorySessionDataStore({ now });
|
|
42
78
|
const states = new Map();
|
|
43
79
|
const completions = new Map();
|
|
44
80
|
const remember = (entries, token, ttl) => {
|
|
@@ -50,7 +86,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
50
86
|
entries.set(token, now() + ttl);
|
|
51
87
|
};
|
|
52
88
|
router.use(express.json({ limit: "64kb" }));
|
|
53
|
-
router.get("/dev-login", (request, response) => {
|
|
89
|
+
router.get("/dev-login", async (request, response) => {
|
|
54
90
|
const address = request.socket.remoteAddress;
|
|
55
91
|
const loopback = address === "127.0.0.1" || address === "::1" || address === "::ffff:127.0.0.1";
|
|
56
92
|
if (production || !loopback || request.headers["x-forwarded-for"] !== undefined ||
|
|
@@ -67,6 +103,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
67
103
|
username: "local_player",
|
|
68
104
|
displayName: "Local Player",
|
|
69
105
|
avatar: null,
|
|
106
|
+
raw: { id: "local-development-user", username: "local_player", avatar: null },
|
|
70
107
|
},
|
|
71
108
|
applicationId: clientId,
|
|
72
109
|
instanceId: "local-development-instance",
|
|
@@ -74,7 +111,9 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
74
111
|
issuedAt: now,
|
|
75
112
|
expiresAt: now + 3_600_000,
|
|
76
113
|
};
|
|
77
|
-
|
|
114
|
+
const dataId = randomBytes(24).toString("base64url");
|
|
115
|
+
await sessionDataStore.set(dataId, extendedSessionData(session), session.expiresAt);
|
|
116
|
+
response.setHeader("Set-Cookie", serializeSessionCookie(sealLudicordSession(compactSession(session, dataId), sessionSecret), { production: false, maxAgeSeconds: 3600 }));
|
|
78
117
|
response.json(session);
|
|
79
118
|
});
|
|
80
119
|
router.get("/state", (request, response) => {
|
|
@@ -163,6 +202,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
163
202
|
let activityInstanceVerified = false;
|
|
164
203
|
let guildName;
|
|
165
204
|
let guildIcon;
|
|
205
|
+
let guildRaw;
|
|
166
206
|
if (verifyActivityInstance) {
|
|
167
207
|
if (botToken === undefined || botToken === "") {
|
|
168
208
|
unavailable(response);
|
|
@@ -218,7 +258,8 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
218
258
|
const guild = userGuilds.find((item) => item.id === guildId);
|
|
219
259
|
if (guild !== undefined) {
|
|
220
260
|
guildName = guild.name;
|
|
221
|
-
guildIcon =
|
|
261
|
+
guildIcon = discordGuildIconUrl(guildId, guild.icon);
|
|
262
|
+
guildRaw = { ...guild };
|
|
222
263
|
break;
|
|
223
264
|
}
|
|
224
265
|
if (userGuilds.length < 200)
|
|
@@ -242,6 +283,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
242
283
|
...(guildId === undefined ? {} : { guildId }),
|
|
243
284
|
...(guildName === undefined ? {} : { guildName }),
|
|
244
285
|
...(guildIcon === undefined ? {} : { guildIcon }),
|
|
286
|
+
...(guildRaw === undefined ? {} : { guildRaw }),
|
|
245
287
|
...(channelId === undefined ? {} : { channelId }),
|
|
246
288
|
...(instanceId === undefined ? {} : { instanceId }),
|
|
247
289
|
...(activityInstanceVerified ? { activityInstanceVerified: true } : {}),
|
|
@@ -252,8 +294,10 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
252
294
|
expiresAt: issuedAt + expiresInSeconds * 1_000,
|
|
253
295
|
};
|
|
254
296
|
const completionToken = randomBytes(32).toString("base64url");
|
|
297
|
+
const dataId = randomBytes(24).toString("base64url");
|
|
298
|
+
await sessionDataStore.set(dataId, extendedSessionData(session), issuedAt + 120_000);
|
|
255
299
|
remember(completions, completionToken, 120_000);
|
|
256
|
-
const sealed = sealLudicordSession({ ...session, kind: "pending", completionToken, sessionExpiresAt: session.expiresAt, expiresAt: issuedAt + 120_000 }, sessionSecret);
|
|
300
|
+
const sealed = sealLudicordSession({ ...compactSession(session, dataId), kind: "pending", completionToken, sessionExpiresAt: session.expiresAt, expiresAt: issuedAt + 120_000 }, sessionSecret);
|
|
257
301
|
response.append("Set-Cookie", serializeSessionCookie(sealed, {
|
|
258
302
|
production: production || secureRequest(request),
|
|
259
303
|
maxAgeSeconds: 120,
|
|
@@ -273,7 +317,7 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
273
317
|
});
|
|
274
318
|
}
|
|
275
319
|
});
|
|
276
|
-
router.post("/complete", (request, response) => {
|
|
320
|
+
router.post("/complete", async (request, response) => {
|
|
277
321
|
const value = parseCookies(request.headers.cookie)[PENDING_SESSION_COOKIE];
|
|
278
322
|
const token = optionalString(request.body?.completionToken);
|
|
279
323
|
const pending = value === undefined || sessionSecret === undefined ? null : unsealLudicordSession(value, sessionSecret);
|
|
@@ -285,14 +329,19 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
285
329
|
completions.delete(token);
|
|
286
330
|
const { kind: _kind, completionToken: _token, sessionExpiresAt, ...identity } = pending;
|
|
287
331
|
const expiresAt = Math.min(now() + 3_600_000, sessionExpiresAt ?? pending.expiresAt);
|
|
288
|
-
const
|
|
289
|
-
|
|
332
|
+
const compact = { ...identity, expiresAt };
|
|
333
|
+
const session = await hydrateSession(compact, sessionDataStore);
|
|
334
|
+
const dataId = sessionDataId(compact);
|
|
335
|
+
if (dataId !== undefined) {
|
|
336
|
+
await sessionDataStore.set(dataId, extendedSessionData(session), expiresAt);
|
|
337
|
+
}
|
|
338
|
+
response.append("Set-Cookie", serializeSessionCookie(sealLudicordSession(compact, sessionSecret), {
|
|
290
339
|
production: production || secureRequest(request), maxAgeSeconds: Math.max(0, Math.floor((expiresAt - now()) / 1000)),
|
|
291
340
|
}));
|
|
292
341
|
response.append("Set-Cookie", clearCookie(PENDING_SESSION_COOKIE, production || secureRequest(request)));
|
|
293
342
|
response.json(session);
|
|
294
343
|
});
|
|
295
|
-
router.get("/session", (request, response) => {
|
|
344
|
+
router.get("/session", async (request, response) => {
|
|
296
345
|
if (sessionSecret === undefined) {
|
|
297
346
|
unavailable(response);
|
|
298
347
|
return;
|
|
@@ -302,18 +351,25 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
302
351
|
response.status(401).json({ authenticated: false });
|
|
303
352
|
return;
|
|
304
353
|
}
|
|
305
|
-
response.json(session);
|
|
354
|
+
response.json(await hydrateSession(session, sessionDataStore));
|
|
306
355
|
});
|
|
307
|
-
router.post("/logout", (request, response) => {
|
|
356
|
+
router.post("/logout", async (request, response) => {
|
|
357
|
+
if (sessionSecret !== undefined) {
|
|
358
|
+
const session = parseSessionCookie(request.headers.cookie, sessionSecret, { allowDevelopment: !production });
|
|
359
|
+
const id = session === null ? undefined : sessionDataId(session);
|
|
360
|
+
if (id !== undefined)
|
|
361
|
+
await sessionDataStore.delete(id);
|
|
362
|
+
}
|
|
308
363
|
response.setHeader("Set-Cookie", [SESSION_COOKIE, PENDING_SESSION_COOKIE, OAUTH_STATE_COOKIE].map((name) => clearCookie(name, production || secureRequest(request))));
|
|
309
364
|
response.status(204).end();
|
|
310
365
|
});
|
|
311
366
|
router.get("/context", async (request, response) => {
|
|
312
|
-
const
|
|
313
|
-
if (!
|
|
367
|
+
const compact = sessionSecret === undefined ? null : parseSessionCookie(request.headers.cookie, sessionSecret);
|
|
368
|
+
if (!compact) {
|
|
314
369
|
response.status(401).json({ message: "Discord login is required." });
|
|
315
370
|
return;
|
|
316
371
|
}
|
|
372
|
+
const session = await hydrateSession(compact, sessionDataStore);
|
|
317
373
|
if (!botToken || !session.instanceId) {
|
|
318
374
|
response.status(501).json({ message: "Extended Discord context requires a bot token and verified Activity membership." });
|
|
319
375
|
return;
|
|
@@ -325,13 +381,103 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
325
381
|
return;
|
|
326
382
|
}
|
|
327
383
|
const { guildId, channelId } = verification.instance;
|
|
328
|
-
const [guild, channel] = await Promise.all([
|
|
329
|
-
|
|
384
|
+
const [guild, channel, member] = await Promise.all([
|
|
385
|
+
guildId ? rest.getGuild(guildId) : undefined,
|
|
386
|
+
channelId ? rest.getChannel(channelId) : undefined,
|
|
387
|
+
guildId ? rest.getGuildMember(guildId, session.user.id).catch(() => undefined) : undefined,
|
|
388
|
+
]);
|
|
389
|
+
response.json({
|
|
390
|
+
guild,
|
|
391
|
+
channel,
|
|
392
|
+
member,
|
|
393
|
+
permissions: {
|
|
394
|
+
guild: typeof session.guildRaw?.permissions === "string" ? session.guildRaw.permissions : null,
|
|
395
|
+
channel: typeof channel?.permissions === "string" ? channel.permissions : null,
|
|
396
|
+
member: typeof member?.permissions === "string" ? member.permissions : null,
|
|
397
|
+
},
|
|
398
|
+
});
|
|
330
399
|
}
|
|
331
400
|
catch (error) {
|
|
332
401
|
response.status(error instanceof LudicordDiscordRestError ? error.status : 502).json({ message: "Discord context is unavailable. Check the bot's access to this guild and channel." });
|
|
333
402
|
}
|
|
334
403
|
});
|
|
404
|
+
router.get("/guild-channels", async (request, response) => {
|
|
405
|
+
const session = sessionSecret === undefined ? null : parseSessionCookie(request.headers.cookie, sessionSecret);
|
|
406
|
+
if (!session) {
|
|
407
|
+
response.status(401).json({ message: "Discord login is required." });
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (!botToken || !session.instanceId) {
|
|
411
|
+
response.status(501).json({ message: "Guild channels require a bot token and verified Activity membership." });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
const verified = await verifyDiscordActivityInstance({ applicationId: session.applicationId, instanceId: session.instanceId, userId: session.user.id, botToken, fetch: fetchImplementation });
|
|
416
|
+
if (!verified.verified || !verified.instance.guildId) {
|
|
417
|
+
response.status(!verified.verified && verified.reason === "rate-limited" ? 429 : 403).json({ message: "The current user could not be verified in this guild Activity." });
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
response.json({ channels: await rest.listGuildChannels(verified.instance.guildId) });
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
if (error instanceof LudicordDiscordRestError && error.retryAfterMs !== undefined)
|
|
424
|
+
response.setHeader("Retry-After", String(Math.ceil(error.retryAfterMs / 1000)));
|
|
425
|
+
response.status(error instanceof LudicordDiscordRestError ? error.status : 502).json({ message: error instanceof LudicordDiscordRestError ? error.message : "Discord channels are temporarily unavailable." });
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
router.get("/guild-roles", async (request, response) => {
|
|
429
|
+
const session = sessionSecret === undefined ? null : parseSessionCookie(request.headers.cookie, sessionSecret);
|
|
430
|
+
if (!session) {
|
|
431
|
+
response.status(401).json({ message: "Discord login is required." });
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (!botToken || !session.instanceId) {
|
|
435
|
+
response.status(501).json({ message: "Guild roles require a bot token and verified Activity membership." });
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
const verified = await verifyDiscordActivityInstance({ applicationId: session.applicationId, instanceId: session.instanceId, userId: session.user.id, botToken, fetch: fetchImplementation });
|
|
440
|
+
if (!verified.verified || !verified.instance.guildId) {
|
|
441
|
+
response.status(!verified.verified && verified.reason === "rate-limited" ? 429 : 403).json({ message: "The current user could not be verified in this guild Activity." });
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
response.json({ roles: await rest.listGuildRoles(verified.instance.guildId) });
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
if (error instanceof LudicordDiscordRestError && error.retryAfterMs !== undefined)
|
|
448
|
+
response.setHeader("Retry-After", String(Math.ceil(error.retryAfterMs / 1000)));
|
|
449
|
+
response.status(error instanceof LudicordDiscordRestError ? error.status : 502).json({ message: error instanceof LudicordDiscordRestError ? error.message : "Discord roles are temporarily unavailable." });
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
router.get("/guild-member/:userId", async (request, response) => {
|
|
453
|
+
const session = sessionSecret === undefined ? null : parseSessionCookie(request.headers.cookie, sessionSecret);
|
|
454
|
+
if (!session) {
|
|
455
|
+
response.status(401).json({ message: "Discord login is required." });
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (!botToken || !session.instanceId) {
|
|
459
|
+
response.status(501).json({ message: "Guild members require a bot token and verified Activity membership." });
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const userId = request.params.userId;
|
|
463
|
+
if (typeof userId !== "string" || !/^\d+$/.test(userId)) {
|
|
464
|
+
response.status(400).json({ message: "A valid Discord user ID is required." });
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
try {
|
|
468
|
+
const verified = await verifyDiscordActivityInstance({ applicationId: session.applicationId, instanceId: session.instanceId, userId: session.user.id, botToken, fetch: fetchImplementation });
|
|
469
|
+
if (!verified.verified || !verified.instance.guildId) {
|
|
470
|
+
response.status(!verified.verified && verified.reason === "rate-limited" ? 429 : 403).json({ message: "The current user could not be verified in this guild Activity." });
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
response.json({ member: await rest.getGuildMember(verified.instance.guildId, userId) });
|
|
474
|
+
}
|
|
475
|
+
catch (error) {
|
|
476
|
+
if (error instanceof LudicordDiscordRestError && error.retryAfterMs !== undefined)
|
|
477
|
+
response.setHeader("Retry-After", String(Math.ceil(error.retryAfterMs / 1000)));
|
|
478
|
+
response.status(error instanceof LudicordDiscordRestError ? error.status : 502).json({ message: error instanceof LudicordDiscordRestError ? error.message : "Discord member information is temporarily unavailable." });
|
|
479
|
+
}
|
|
480
|
+
});
|
|
335
481
|
router.get("/guild-members", async (request, response) => {
|
|
336
482
|
const session = sessionSecret === undefined ? null : parseSessionCookie(request.headers.cookie, sessionSecret);
|
|
337
483
|
if (session === null) {
|
|
@@ -369,4 +515,3 @@ export function createLudicordAuthRouter(options = {}) {
|
|
|
369
515
|
});
|
|
370
516
|
return router;
|
|
371
517
|
}
|
|
372
|
-
//# sourceMappingURL=server.js.map
|
package/dist/auth/types.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import type { DiscordRawPayload } from "../discord/payload.js";
|
|
1
2
|
export interface LudicordSessionUser {
|
|
2
3
|
readonly id: string;
|
|
3
4
|
readonly username: string;
|
|
4
5
|
readonly displayName: string;
|
|
5
6
|
readonly avatar: string | null;
|
|
7
|
+
/** Complete `/users/@me` response, without OAuth credentials. */
|
|
8
|
+
readonly raw?: DiscordRawPayload;
|
|
6
9
|
}
|
|
7
10
|
export interface LudicordSession {
|
|
8
11
|
/** Development and pending credentials are never accepted as production sessions. */
|
|
@@ -15,6 +18,8 @@ export interface LudicordSession {
|
|
|
15
18
|
readonly guildId?: string;
|
|
16
19
|
readonly guildName?: string;
|
|
17
20
|
readonly guildIcon?: string | null;
|
|
21
|
+
/** Complete selected guild object returned by `/users/@me/guilds`. */
|
|
22
|
+
readonly guildRaw?: DiscordRawPayload;
|
|
18
23
|
readonly channelId?: string;
|
|
19
24
|
readonly instanceId?: string;
|
|
20
25
|
readonly activityInstanceVerified?: true;
|
|
@@ -28,4 +33,3 @@ export interface LudicordAuthSnapshot {
|
|
|
28
33
|
readonly session: LudicordSession | null;
|
|
29
34
|
readonly error: Error | null;
|
|
30
35
|
}
|
|
31
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/auth/types.js
CHANGED
package/dist/cli/index.d.ts
CHANGED