2020-01-27 00:47:03 +01:00
|
|
|
const debug = require('debug')('auth')
|
|
|
|
|
const oauth = require('./oauth')
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
|
const Auth = {
|
2019-10-30 14:58:40 +01:00
|
|
|
|
|
|
|
|
/** isAuth middleware
|
|
|
|
|
* req.user is filled in server/helper.js#initMiddleware
|
|
|
|
|
*/
|
2020-01-27 00:47:03 +01:00
|
|
|
isAuth (req, res, next) {
|
|
|
|
|
return oauth.oauthServer.authenticate()(req, res, next)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
2019-10-30 14:58:40 +01:00
|
|
|
|
|
|
|
|
/** isAdmin middleware */
|
2019-09-11 19:12:24 +02:00
|
|
|
isAdmin (req, res, next) {
|
2020-01-27 00:47:03 +01:00
|
|
|
oauth.oauthServer.authenticate()(req, res, () => {
|
|
|
|
|
req.user = res.locals.oauth.token.user
|
|
|
|
|
if (req.user.is_admin) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
res.status(404)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hasPerm (scope) {
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
|
debug(scope, req.path)
|
|
|
|
|
oauth.oauthServer.authenticate({ scope })(req, res, () => {
|
|
|
|
|
req.user = res.locals.oauth.token.user
|
|
|
|
|
next()
|
|
|
|
|
})
|
2019-06-06 23:54:32 +02:00
|
|
|
}
|
2019-09-11 19:12:24 +02:00
|
|
|
}
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = Auth
|