Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/server/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CreateGameInputSchema } from "../core/WorkerSchemas";
import { archive, finalizeGameRecord } from "./Archive";
import { Client } from "./Client";
import { GameManager } from "./GameManager";
import { getUserMe, verifyClientToken } from "./jwt";
import { getUserMe, TokenPayload, verifyClientToken } from "./jwt";
import { logger } from "./Logger";

import { GameEnv } from "../core/configuration/Config";
Expand Down Expand Up @@ -282,19 +282,29 @@ export async function startWorker() {
return;
}

// Verify token signature
const result = await verifyClientToken(clientMsg.token, config);
if (result.type === "error") {
log.warn(`Invalid token: ${result.message}`, {
clientID: clientMsg.clientID,
});
ws.close(
1002,
`Unauthorized: invalid token for client ${clientMsg.clientID}`,
);
return;
// Verify token signature (skip in dev mode)
let persistentId: string;
let claims: TokenPayload | null;

if (config.env() !== GameEnv.Dev) {
const result = await verifyClientToken(clientMsg.token, config);
if (result.type === "error") {
log.warn(`Invalid token: ${result.message}`, {
clientID: clientMsg.clientID,
});
ws.close(
1002,
`Unauthorized: invalid token for client ${clientMsg.clientID}`,
);
return;
}
persistentId = result.persistentId;
claims = result.claims;
} else {
// In dev mode, use clientID as persistentId
persistentId = clientMsg.clientID;
claims = null;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const { persistentId, claims } = result;

if (clientMsg.type === "rejoin") {
log.info("rejoining game", {
Expand Down Expand Up @@ -323,8 +333,8 @@ export async function startWorker() {
ws.close(1002, "Unauthorized");
return;
}
} else {
// Verify token and get player permissions
} else if (config.env() !== GameEnv.Dev) {
Comment thread
ryanbarlow97 marked this conversation as resolved.
// Verify token and get player permissions (skip in dev mode)
const result = await getUserMe(clientMsg.token, config);
if (result.type === "error") {
log.warn(`Unauthorized: ${result.message}`, {
Expand Down
2 changes: 2 additions & 0 deletions src/server/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { GameEnv, ServerConfig } from "../core/configuration/Config";
import { PersistentIdSchema } from "../core/Schemas";

export type { TokenPayload };

type TokenVerificationResult =
| {
type: "success";
Expand Down
Loading