UNPKG

next-auth

Version:

Authentication for Next.js

197 lines (157 loc) 5.59 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultCookies = defaultCookies; exports.SessionStore = void 0; 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: { 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 } }, state: { name: `${cookiePrefix}next-auth.state`, 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); if (!req) return; for (const name in req.cookies) { if (name.startsWith(option.name)) { (0, _classPrivateFieldGet3.default)(this, _chunks)[name] = req.cookies[name]; } } } get value() { var _Object$values; return (_Object$values = Object.values((0, _classPrivateFieldGet3.default)(this, _chunks))) === null || _Object$values === void 0 ? void 0 : _Object$values.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; }