Align TS session tokens with Python parity
This commit is contained in:
parent
1e37841726
commit
bad0ff9408
3 changed files with 61 additions and 2 deletions
|
|
@ -209,6 +209,9 @@ The security helpers intentionally mirror CultLib's shape:
|
|||
- 12-byte nonce
|
||||
- authentication tag prefixed onto ciphertext
|
||||
- session token format: `base64url(payload).base64url(signature)`
|
||||
- versioned C#/Python-compatible session token payloads:
|
||||
`userGuidN|expiresUnix|sessionVersion`, while legacy two-field tokens still
|
||||
validate with session version `0`
|
||||
|
||||
That means a TypeScript runtime can follow the same cryptographic and session
|
||||
story as the C# version instead of inventing one more house religion around
|
||||
|
|
|
|||
|
|
@ -96,6 +96,7 @@ export class CultNetServerSecurityOptions extends CultNetClientSecurityOptions {
|
|||
export interface ValidatedCultNetSessionToken {
|
||||
userId: string;
|
||||
expiresAtUtc: Date;
|
||||
sessionVersion: number;
|
||||
}
|
||||
|
||||
export const CultNetSecret = {
|
||||
|
|
@ -166,8 +167,11 @@ export const CultNetSecret = {
|
|||
userId: string,
|
||||
expiresAtUtc: Date,
|
||||
options: CultNetServerSecurityOptions,
|
||||
sessionVersion?: number,
|
||||
): string {
|
||||
const payload = `${userId}|${Math.floor(expiresAtUtc.getTime() / 1000)}`;
|
||||
const payload = sessionVersion === undefined
|
||||
? `${userId}|${Math.floor(expiresAtUtc.getTime() / 1000)}`
|
||||
: `${normalizeSessionUserId(userId)}|${Math.floor(expiresAtUtc.getTime() / 1000)}|${Math.trunc(sessionVersion)}`;
|
||||
const payloadBytes = Buffer.from(payload, "utf8");
|
||||
const signatureBytes = hmacSha256(options.getSessionSigningKey(), payloadBytes);
|
||||
return `${toBase64Url(payloadBytes)}.${toBase64Url(signatureBytes)}`;
|
||||
|
|
@ -197,7 +201,7 @@ export const CultNetSecret = {
|
|||
|
||||
const payload = payloadBytes.toString("utf8");
|
||||
const payloadParts = payload.split("|");
|
||||
if (payloadParts.length !== 2) {
|
||||
if (payloadParts.length !== 2 && payloadParts.length !== 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -206,6 +210,10 @@ export const CultNetSecret = {
|
|||
if (!Number.isFinite(expiresAtSeconds)) {
|
||||
return null;
|
||||
}
|
||||
const sessionVersion = payloadParts.length === 3 ? Number.parseInt(payloadParts[2]!, 10) : 0;
|
||||
if (!Number.isFinite(sessionVersion)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const expiresAtUtc = new Date(expiresAtSeconds * 1000);
|
||||
if (expiresAtUtc.getTime() <= Date.now()) {
|
||||
|
|
@ -215,6 +223,7 @@ export const CultNetSecret = {
|
|||
return {
|
||||
userId,
|
||||
expiresAtUtc,
|
||||
sessionVersion,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
|
|
@ -247,6 +256,15 @@ function hmacSha256(key: Uint8Array, input: Uint8Array): Buffer {
|
|||
return createHmac("sha256", key).update(input).digest();
|
||||
}
|
||||
|
||||
function normalizeSessionUserId(userId: string): string {
|
||||
const normalized = userId.replace(/-/g, "").toLowerCase();
|
||||
if (!/^[0-9a-f]{32}$/u.test(normalized)) {
|
||||
throw new Error("Versioned session tokens require a Guid-compatible user id.");
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function toBase64Url(input: Uint8Array): string {
|
||||
return Buffer.from(input)
|
||||
.toString("base64")
|
||||
|
|
|
|||
|
|
@ -84,6 +84,44 @@ test("CultNet secret helpers round-trip encrypted strings and validate sessions"
|
|||
const validated = CultNetSecret.tryValidateSessionToken(token, serverSecurity);
|
||||
assert.ok(validated);
|
||||
assert.equal(validated?.userId, "runtime-face");
|
||||
assert.equal(validated?.sessionVersion, 0);
|
||||
});
|
||||
|
||||
test("CultNet secret helpers validate C# and Python compatible versioned sessions", () => {
|
||||
const serverSecurity = CultNetServerSecurityOptions.development();
|
||||
const expires = new Date(Date.now() + 60_000);
|
||||
const token = CultNetSecret.createSessionToken(
|
||||
"318fb4b6-ff5e-4c4f-b911-d81807de53a8",
|
||||
expires,
|
||||
serverSecurity,
|
||||
42,
|
||||
);
|
||||
const [payload] = token.split(".");
|
||||
assert.equal(
|
||||
new TextDecoder().decode(CultNetSecret.fromBase64Url(payload!)),
|
||||
`318fb4b6ff5e4c4fb911d81807de53a8|${Math.floor(expires.getTime() / 1000)}|42`,
|
||||
);
|
||||
|
||||
const validated = CultNetSecret.tryValidateSessionToken(token, serverSecurity);
|
||||
assert.ok(validated);
|
||||
assert.equal(validated?.userId, "318fb4b6ff5e4c4fb911d81807de53a8");
|
||||
assert.equal(validated?.sessionVersion, 42);
|
||||
assert.throws(
|
||||
() => CultNetSecret.createSessionToken("runtime-face", expires, serverSecurity, 1),
|
||||
/Guid-compatible/,
|
||||
);
|
||||
});
|
||||
|
||||
test("CultNet secret helpers validate Python-created versioned sessions", () => {
|
||||
const token = [
|
||||
"MzE4ZmI0YjZmZjVlNGM0ZmI5MTFkODE4MDdkZTUzYTh8MjA1MTIyMjQwMHw3Nw",
|
||||
"jRrUiE5Om7NQVKMJP4PkBkLFVLXqNb8Uu9jg4VG13pU",
|
||||
].join(".");
|
||||
|
||||
const validated = CultNetSecret.tryValidateSessionToken(token, CultNetServerSecurityOptions.development());
|
||||
assert.ok(validated);
|
||||
assert.equal(validated?.userId, "318fb4b6ff5e4c4fb911d81807de53a8");
|
||||
assert.equal(validated?.sessionVersion, 77);
|
||||
});
|
||||
|
||||
test("CultNet peer frames and decodes typed messages over a direct pipe", async () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue