CoreID/app/routing/middleware/Traps.middleware.js

80 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-05-20 14:56:03 +00:00
const { Middleware } = require('libflitter')
class TrapUtility {
constructor(req, res, configs) {
this.request = req
this.session = req.session
2020-05-20 14:56:03 +00:00
this.response = res
this.user = req.user
this.configs = configs
}
async begin(trap_name, { session_only = false }) {
if ( session_only || !this.user ) {
this.session.trap = trap_name
} else {
this.user.trap = trap_name
await this.user.save()
}
if ( this.config().assume_trust )
this.request.trust.assume()
2020-05-20 14:56:03 +00:00
}
redirect() {
if ( this.config().assume_trust )
this.request.trust.assume()
2020-05-20 14:56:03 +00:00
return this.response.redirect(this.config().redirect_to)
}
async end() {
if ( this.config().assume_trust )
this.request.trust.unassume()
if ( this.user ) {
this.user.trap = ''
await this.user.save()
}
this.session.trap = ''
2020-05-20 14:56:03 +00:00
}
has_trap(name = '') {
if ( name )
return (this.user && this.user.trap === name) || this.session.trap === name
return (this.user && this.user.trap) || this.session.trap
2020-05-20 14:56:03 +00:00
}
get_trap() {
if ( this.session.trap ) return this.session.trap
else if ( this.user ) return this.user.trap
2020-05-20 14:56:03 +00:00
}
trap_exists(name) {
return !!this.configs[name]
}
2020-05-20 14:56:03 +00:00
config() {
return this.configs[this.get_trap()]
}
allows(route) {
const config = this.config()
return route.startsWith('/assets') || config.allowed_routes.includes(route.toLowerCase().trim())
}
}
class TrapsMiddleware extends Middleware {
static get services() {
return [...super.services, 'models', 'configs']
}
async test(req, res, next, args = {}) {
req.trap = new TrapUtility(req, res, this.configs.get('traps.types'))
if ( !req.trap.has_trap() ) return next()
else if ( req.trap.allows(req.path) ) return next()
else return req.trap.redirect()
}
}
module.exports = exports = TrapsMiddleware