const { Middleware } = require('libflitter')

class RequireTrustMiddleware extends Middleware {
    async test(req, res, next, { scope, deplete = false }) {
        if ( !req.trust.has(scope) ) {
            req.trust.init_flow(scope, req.originalUrl)
            return res.redirect('/auth/trust/token/issue')
        }

        if ( deplete ) req.trust.deplete(scope)
        return next()
    }
}

module.exports = exports = RequireTrustMiddleware
