Repository navigation
Expand file tree
/
Copy pathcookieSession.ts
More file actions
112 lines (100 loc) · 3.12 KB
/
Copy pathcookieSession.ts
File metadata and controls
112 lines (100 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*!
* cookie-session
* Copyright(c) 2020 João Moura
* MIT Licensed
*/
import { setCookie, getCookies } from "https://deno.land/std@0.63.0/http/cookie.ts"
import { v4 } from "https://deno.land/std@0.63.0/uuid/mod.ts";
import { Middleware, IResponse, IRequest, NextFunction } from "./mod.ts"
import debug from 'https://raw.githubusercontent.com/rista404/deno-debug/75400f612c8051b1f57ecc5c037df9138507592a/debug.ts'
let logger = debug('cookie_session')
type session = {
[key: string]: any
}
export interface options {
name?: string,
keys?: string[],
secret?: string,
maxAge?: number,
expires?: Date,
path?: string,
domain?: string,
sameSite?: "Strict" | "Lax" | "None" | undefined,
secure?: boolean,
httpOnly?: boolean,
signed?: boolean,
overwrite?: boolean
}
let configurationOptions: options
/**
* Module exports.
* @public
*/
/**
* Create a new cookie session middleware.
*
* @param {object} [options]
* @param {boolean} [options.httpOnly=true]
* @param {array} [options.keys]
* @param {string} [options.name=session] Name of the cookie to use
* @param {boolean} [options.overwrite=true]
* @param {string} [options.secret]
* @param {boolean} [options.signed=true]
* @return {function} middleware
* @public
*/
export function cookieSession (options: options): Middleware {
// cookie name
configurationOptions = options
configurationOptions.name = configurationOptions.name || 'mith'
// secrets
if (!configurationOptions.keys && !configurationOptions.secret) {
throw "You should specify keys or secret properties for the cookie session configuration"
}
configurationOptions.keys = configurationOptions.keys || [configurationOptions.secret || '']
logger('session options %j', configurationOptions)
return (req: IRequest, res: IResponse, next: NextFunction) => {
logger('running')
let authString = getCookies(req.serverRequest)[(configurationOptions.name as string)]
let authData: session = {}
logger('authString %s', authString)
if (authString) {
authData = JSON.parse(authString)
}
if (!authData.id) {
authData.id = v4.generate()
}
const handler = {
get: (target: session, property: string): session | string | number => {
if (typeof target[property] === 'object' && target[property] !== null) {
return new Proxy(target[property], handler)
} else {
return target[property];
}
},
set: (target: session, property: string, newValue: session | string | number) => {
target[property] = newValue
setCookie(res, {
name: (configurationOptions.name as string),
value: JSON.stringify(req.session),
...configurationOptions
})
return true
}
}
req.session = new Proxy(authData, handler)
next()
}
}
export function cookieSessionSave (): Middleware {
return async (req, res, next) => {
logger('Saving session %O', )
logger(JSON.stringify(req.session))
setCookie(res, {
name: (configurationOptions.name as string),
value: JSON.stringify(req.session),
...configurationOptions
})
next()
}
}