UNPKG

next-auth

Version:

Authentication for Next.js

232 lines (192 loc) 6.8 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionStore = void 0; exports.defaultCookies = defaultCookies; var _classPrivateFieldGet3 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldGet")); var _classPrivateFieldSet2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldSet")); function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } const ALLOWED_COOKIE_SIZE = 4096; const ESTIMATED_EMPTY_COOKIE_SIZE = 163; const CHUNK_SIZE = ALLOWED_COOKIE_SIZE - ESTIMATED_EMPTY_COOKIE_SIZE; function defaultCookies(useSecureCookies) { const cookiePrefix = useSecureCookies ? "__Secure-" : ""; return { sessionToken: { name: `${cookiePrefix}next-auth.session-token`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies } }, callbackUrl: { name: `${cookiePrefix}next-auth.callback-url`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies } }, csrfToken: { name: `${useSecureCookies ? "__Host-" : ""}next-auth.csrf-token`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies } }, pkceCodeVerifier: { name: `${cookiePrefix}next-auth.pkce.code_verifier`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies, maxAge: 60 * 15 } }, state: { name: `${cookiePrefix}next-auth.state`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies, maxAge: 60 * 15 } }, nonce: { name: `${cookiePrefix}next-auth.nonce`, options: { httpOnly: true, sameSite: "lax", path: "/", secure: useSecureCookies } } }; } var _chunks = new WeakMap(); var _option = new WeakMap(); var _logger = new WeakMap(); var _chunk = new WeakSet(); var _clean = new WeakSet(); class SessionStore { constructor(option, req, logger) { _classPrivateMethodInitSpec(this, _clean); _classPrivateMethodInitSpec(this, _chunk); _classPrivateFieldInitSpec(this, _chunks, { writable: true, value: {} }); _classPrivateFieldInitSpec(this, _option, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _logger, { writable: true, value: void 0 }); (0, _classPrivateFieldSet2.default)(this, _logger, logger); (0, _classPrivateFieldSet2.default)(this, _option, option); const { cookies: _cookies } = req; const { name: cookieName } = option; if (typeof (_cookies === null || _cookies === void 0 ? void 0 : _cookies.getAll) === "function") { for (const { name, value } of _cookies.getAll()) { if (name.startsWith(cookieName)) { (0, _classPrivateFieldGet3.default)(this, _chunks)[name] = value; } } } else if (_cookies instanceof Map) { for (const name of _cookies.keys()) { if (name.startsWith(cookieName)) (0, _classPrivateFieldGet3.default)(this, _chunks)[name] = _cookies.get(name); } } else { for (const name in _cookies) { if (name.startsWith(cookieName)) (0, _classPrivateFieldGet3.default)(this, _chunks)[name] = _cookies[name]; } } } get value() { const sortedKeys = Object.keys((0, _classPrivateFieldGet3.default)(this, _chunks)).sort((a, b) => { var _a$split$pop, _b$split$pop; const aSuffix = parseInt((_a$split$pop = a.split(".").pop()) !== null && _a$split$pop !== void 0 ? _a$split$pop : "0"); const bSuffix = parseInt((_b$split$pop = b.split(".").pop()) !== null && _b$split$pop !== void 0 ? _b$split$pop : "0"); return aSuffix - bSuffix; }); return sortedKeys.map(key => (0, _classPrivateFieldGet3.default)(this, _chunks)[key]).join(""); } chunk(value, options) { const cookies = _classPrivateMethodGet(this, _clean, _clean2).call(this); const chunked = _classPrivateMethodGet(this, _chunk, _chunk2).call(this, { name: (0, _classPrivateFieldGet3.default)(this, _option).name, value, options: { ...(0, _classPrivateFieldGet3.default)(this, _option).options, ...options } }); for (const chunk of chunked) { cookies[chunk.name] = chunk; } return Object.values(cookies); } clean() { return Object.values(_classPrivateMethodGet(this, _clean, _clean2).call(this)); } } exports.SessionStore = SessionStore; function _chunk2(cookie) { const chunkCount = Math.ceil(cookie.value.length / CHUNK_SIZE); if (chunkCount === 1) { (0, _classPrivateFieldGet3.default)(this, _chunks)[cookie.name] = cookie.value; return [cookie]; } const cookies = []; for (let i = 0; i < chunkCount; i++) { const name = `${cookie.name}.${i}`; const value = cookie.value.substr(i * CHUNK_SIZE, CHUNK_SIZE); cookies.push({ ...cookie, name, value }); (0, _classPrivateFieldGet3.default)(this, _chunks)[name] = value; } (0, _classPrivateFieldGet3.default)(this, _logger).debug("CHUNKING_SESSION_COOKIE", { message: `Session cookie exceeds allowed ${ALLOWED_COOKIE_SIZE} bytes.`, emptyCookieSize: ESTIMATED_EMPTY_COOKIE_SIZE, valueSize: cookie.value.length, chunks: cookies.map(c => c.value.length + ESTIMATED_EMPTY_COOKIE_SIZE) }); return cookies; } function _clean2() { const cleanedChunks = {}; for (const name in (0, _classPrivateFieldGet3.default)(this, _chunks)) { var _classPrivateFieldGet2; (_classPrivateFieldGet2 = (0, _classPrivateFieldGet3.default)(this, _chunks)) === null || _classPrivateFieldGet2 === void 0 ? true : delete _classPrivateFieldGet2[name]; cleanedChunks[name] = { name, value: "", options: { ...(0, _classPrivateFieldGet3.default)(this, _option).options, maxAge: 0 } }; } return cleanedChunks; }