-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsrf.js
More file actions
29 lines (25 loc) · 880 Bytes
/
csrf.js
File metadata and controls
29 lines (25 loc) · 880 Bytes
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
var parseDomain = require('parse-domain')
var exists = require('101/exists')
var csurfMiddleware = require('csurf')()
module.exports.csrfValidator = function (req, res, next) {
if (!exists(req.headers.origin)) {
// Bypass because we don't have an Origin meaning it's not a CORS request
return next()
}
csurfMiddleware(req, res, next)
}
module.exports.injectCookie = function (res, req, domain) {
var parsedDomain = parseDomain(domain) || {}
res.cookie('CSRF-TOKEN', req.csrfToken(), {
httpOnly: false,
domain: '.' + parsedDomain.domain + '.' + parsedDomain.tld
})
}
module.exports.csrfCookieInjector = function (req, res, next) {
if (!exists(req.headers.origin)) {
return next()
}
module.exports.injectCookie(res, req, process.env.FULL_API_DOMAIN)
module.exports.injectCookie(res, req, process.env.SECONDARY_FULL_API_DOMAIN)
next()
}