diff --git a/mod.ts b/mod.ts index 055aeef..d1a5d99 100644 --- a/mod.ts +++ b/mod.ts @@ -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 { Aes } from "https://deno.land/x/crypto/aes.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 = { port: number; @@ -61,6 +62,12 @@ export class HTTPServer { settings?: 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.server = Deno.listen({ port: options.port, @@ -286,7 +293,7 @@ export class HTTPServer { routeReply.cookie("session", encodedSession, { maxAge: this.settings.sessionExpire ?? undefined, }); - }else{ + } else { routeReply.cookie("session", undefined); } }