2016-03-21 05:56:33 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const OAuthServer = require('express-oauth-server')
|
|
|
|
|
2016-04-14 15:06:11 -05:00
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
|
|
|
const oAuthServer = new OAuthServer({
|
2016-03-21 05:56:33 -05:00
|
|
|
model: require('../models/users')
|
|
|
|
})
|
|
|
|
|
2016-04-14 15:06:11 -05:00
|
|
|
const oAuth2 = {
|
|
|
|
authenticate: authenticate,
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
|
|
|
|
function authenticate (req, res, next) {
|
|
|
|
oAuthServer.authenticate()(req, res, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot authenticate.', { error: err })
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.statusCode === 401 || res.statusCode === 400) return res.end()
|
|
|
|
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function token (req, res, next) {
|
|
|
|
return oAuthServer.token()(req, res, next)
|
|
|
|
}
|
|
|
|
|
2016-03-21 05:56:33 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = oAuth2
|