173 lines
4.8 KiB
TypeScript
173 lines
4.8 KiB
TypeScript
import {
|
|
createHmac,
|
|
randomBytes,
|
|
timingSafeEqual,
|
|
} from "node:crypto";
|
|
|
|
const TOKEN_CONTEXT = "admin-sales:booking-manage:v1";
|
|
const MAX_TOKEN_LENGTH = 2_048;
|
|
const MAX_LIFETIME_SECONDS = 90 * 86_400;
|
|
|
|
export type AdminBookingManageClaims = {
|
|
readonly bookingId: string;
|
|
readonly adminUserId: string;
|
|
readonly version: number;
|
|
readonly expiresAt: Date;
|
|
readonly nonce: string;
|
|
};
|
|
|
|
type SerializedClaims = {
|
|
readonly b: string;
|
|
readonly a: string;
|
|
readonly v: number;
|
|
readonly exp: number;
|
|
readonly n: string;
|
|
};
|
|
|
|
export function issueAdminBookingManageToken(input: {
|
|
readonly bookingId: string;
|
|
readonly adminUserId: string;
|
|
readonly version: number;
|
|
readonly expiresAt: Date;
|
|
readonly secret: string;
|
|
readonly now?: Date;
|
|
readonly nonce?: string;
|
|
}): string {
|
|
const now = input.now ?? new Date();
|
|
const claims = normalizeClaims({
|
|
bookingId: input.bookingId,
|
|
adminUserId: input.adminUserId,
|
|
version: input.version,
|
|
expiresAt: input.expiresAt,
|
|
nonce: input.nonce ?? randomBytes(24).toString("base64url"),
|
|
});
|
|
assertSecret(input.secret);
|
|
const lifetime = Math.ceil(
|
|
(claims.expiresAt.getTime() - now.getTime()) / 1_000
|
|
);
|
|
if (lifetime <= 0 || lifetime > MAX_LIFETIME_SECONDS) {
|
|
throw new AdminBookingManageTokenError("invalid_expiry");
|
|
}
|
|
const payload = Buffer.from(
|
|
JSON.stringify(serialize(claims)),
|
|
"utf8"
|
|
).toString("base64url");
|
|
return `${payload}.${sign(payload, input.secret)}`;
|
|
}
|
|
|
|
export function verifyAdminBookingManageToken(input: {
|
|
readonly token: string;
|
|
readonly secret: string;
|
|
readonly now?: Date;
|
|
}): AdminBookingManageClaims {
|
|
assertSecret(input.secret);
|
|
if (!input.token || input.token.length > MAX_TOKEN_LENGTH) {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
const [payload, signature, ...extra] = input.token.split(".");
|
|
if (!payload || !signature || extra.length > 0) {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
const expected = Buffer.from(sign(payload, input.secret), "base64url");
|
|
const actual = Buffer.from(signature, "base64url");
|
|
if (
|
|
expected.length !== actual.length ||
|
|
!timingSafeEqual(expected, actual)
|
|
) {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
let serialized: unknown;
|
|
try {
|
|
serialized = JSON.parse(
|
|
Buffer.from(payload, "base64url").toString("utf8")
|
|
);
|
|
} catch {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
const claims = deserialize(serialized);
|
|
if (claims.expiresAt.getTime() <= (input.now ?? new Date()).getTime()) {
|
|
throw new AdminBookingManageTokenError("expired_token");
|
|
}
|
|
return claims;
|
|
}
|
|
|
|
export function hashAdminBookingManageNonce(nonce: string): string {
|
|
if (!/^[A-Za-z0-9_-]{32,128}$/u.test(nonce)) {
|
|
throw new AdminBookingManageTokenError("invalid_nonce");
|
|
}
|
|
return createHmac("sha256", TOKEN_CONTEXT)
|
|
.update(nonce, "utf8")
|
|
.digest("hex");
|
|
}
|
|
|
|
function serialize(claims: AdminBookingManageClaims): SerializedClaims {
|
|
return {
|
|
b: claims.bookingId,
|
|
a: claims.adminUserId,
|
|
v: claims.version,
|
|
exp: Math.floor(claims.expiresAt.getTime() / 1_000),
|
|
n: claims.nonce,
|
|
};
|
|
}
|
|
|
|
function deserialize(value: unknown): AdminBookingManageClaims {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
const record = value as Record<string, unknown>;
|
|
if (
|
|
Object.keys(record).sort().join(",") !== "a,b,exp,n,v" ||
|
|
typeof record.b !== "string" ||
|
|
typeof record.a !== "string" ||
|
|
typeof record.v !== "number" ||
|
|
typeof record.exp !== "number" ||
|
|
typeof record.n !== "string"
|
|
) {
|
|
throw new AdminBookingManageTokenError("invalid_token");
|
|
}
|
|
return normalizeClaims({
|
|
bookingId: record.b,
|
|
adminUserId: record.a,
|
|
version: record.v,
|
|
expiresAt: new Date(record.exp * 1_000),
|
|
nonce: record.n,
|
|
});
|
|
}
|
|
|
|
function normalizeClaims(
|
|
claims: AdminBookingManageClaims
|
|
): AdminBookingManageClaims {
|
|
if (
|
|
!/^[A-Za-z0-9][A-Za-z0-9_:-]{0,190}$/u.test(claims.bookingId) ||
|
|
!/^[A-Za-z0-9][A-Za-z0-9_:-]{0,190}$/u.test(claims.adminUserId) ||
|
|
!Number.isSafeInteger(claims.version) ||
|
|
claims.version < 1 ||
|
|
!Number.isFinite(claims.expiresAt.getTime()) ||
|
|
!/^[A-Za-z0-9_-]{32,128}$/u.test(claims.nonce)
|
|
) {
|
|
throw new AdminBookingManageTokenError("invalid_claims");
|
|
}
|
|
return claims;
|
|
}
|
|
|
|
function sign(payload: string, secret: string): string {
|
|
return createHmac("sha256", secret)
|
|
.update(TOKEN_CONTEXT)
|
|
.update("\0")
|
|
.update(payload)
|
|
.digest("base64url");
|
|
}
|
|
|
|
function assertSecret(secret: string): void {
|
|
if (secret.length < 32) {
|
|
throw new AdminBookingManageTokenError("secret_unavailable");
|
|
}
|
|
}
|
|
|
|
export class AdminBookingManageTokenError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code);
|
|
this.name = "AdminBookingManageTokenError";
|
|
}
|
|
}
|