add exception for sessionSecret

This commit is contained in:
HorizonCode 2023-05-14 20:22:37 +02:00
parent 8a3683d2e5
commit 2e6ce88e80

9
mod.ts
View File

@ -6,6 +6,7 @@ import * as path from "https://deno.land/std@0.185.0/path/mod.ts";
import * as cookie from "https://deno.land/std@0.185.0/http/cookie.ts"; import * as cookie from "https://deno.land/std@0.185.0/http/cookie.ts";
import { Aes } from "https://deno.land/x/crypto/aes.ts"; import { Aes } from "https://deno.land/x/crypto/aes.ts";
import { Cbc, Padding } from "https://deno.land/x/crypto/block-modes.ts"; import { Cbc, Padding } from "https://deno.land/x/crypto/block-modes.ts";
import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/mod.ts";
type HTTPServerOptions = { type HTTPServerOptions = {
port: number; port: number;
@ -61,6 +62,12 @@ export class HTTPServer {
settings?: HTTPServerOptions; settings?: HTTPServerOptions;
async listen(options: HTTPServerOptions) { async listen(options: HTTPServerOptions) {
if (options.sessionSecret) {
if (![16, 24, 32].includes(options.sessionSecret.length)) {
const randomString = cryptoRandomString({ length: 32 });
throw new Error("\nInvalid key size (must be either 16, 24 or 32 bytes)\nHere is a pregenerated key: " + randomString);
}
}
this.settings = options; this.settings = options;
this.server = Deno.listen({ this.server = Deno.listen({
port: options.port, port: options.port,
@ -286,7 +293,7 @@ export class HTTPServer {
routeReply.cookie("session", encodedSession, { routeReply.cookie("session", encodedSession, {
maxAge: this.settings.sessionExpire ?? undefined, maxAge: this.settings.sessionExpire ?? undefined,
}); });
}else{ } else {
routeReply.cookie("session", undefined); routeReply.cookie("session", undefined);
} }
} }