big oauth improvements

This commit is contained in:
les
2020-01-21 01:24:10 +01:00
parent d1dbbebffb
commit e0b3dd8d4a
18 changed files with 289 additions and 172 deletions

View File

@@ -2,41 +2,36 @@ const express = require('express')
const OAuthServer = require('express-oauth-server')
const oauth = express.Router()
const oauthController = require('./controller/oauth')
const debug = require('debug')('oauth')
const oauthServer = new OAuthServer({
model: oauthController.model,
allowEmptyState: true,
useErrorHandler: true,
continueMiddleware: false,
debug: true,
authenticateHandler: { handle(req) { return req.user } }
authenticateHandler: {
handle (req) {
if (!req.user) {
throw new Error('Not authenticated!')
}
return req.user
}
}
})
oauth.oauth = oauthServer
oauth.use(express.urlencoded({ extended: false }))
oauth.oauthServer = oauthServer
oauth.use(express.json())
oauth.use(express.urlencoded({ extended: false }))
// post token
oauth.post('/token', oauthServer.token())
oauth.get('/authorize', async (req, res, next) => {
if (!req.user) {
return res.redirect(`/login?redirect=${req.path}&client_id=${req.query.client_id}&redirect_uri=${req.query.redirect_uri}`)
}
return oauthServer.authorize()
})
oauth.post('/authorize', (req, res, next) => {
if (!req.user) {
return res.redirect(`/login?redirect=${req.path}&client_id=${req.query.client_id}&redirect_uri=${req.query.redirect_uri}`)
}
return oauthServer.authorize()
})
oauth.get('/authorize', oauthServer.authorize())
oauth.use((err, req, res, next) => {
res.status(500).json(err)
const error_msg = err.toString()
debug(err)
res.status(500).send(error_msg)
})
// oauth.post('/login', )
module.exports = oauth
module.exports = oauth