squash new oauth2 flow

This commit is contained in:
lesion
2022-11-04 12:22:21 +01:00
parent 19ca1b0283
commit 44e9533828
24 changed files with 935 additions and 427 deletions

View File

@@ -1,41 +1,51 @@
const express = require('express')
const OAuthServer = require('express-oauth-server')
const oauth = express.Router()
const oauthController = require('./controller/oauth')
const log = require('../log')
// const express = require('express')
// // const OAuthServer = require('express-oauth-server')
// const oauth2orize = require('oauth2orize')
// // const oauth = express.Router()
// // const oauthController = require('./controller/oauth')
// // const OauthClient = require('./models/oauth_client')
// // const log = require('../log')
const oauthServer = new OAuthServer({
model: oauthController.model,
allowEmptyState: true,
useErrorHandler: true,
continueMiddleware: false,
debug: true,
requireClientAuthentication: { password: false },
authenticateHandler: {
handle (_req, res) {
if (!res.locals.user) {
throw new Error('Not authenticated!')
}
return res.locals.user
}
}
})
// // const oauthServer = oauth2orize.createServer()
oauth.oauthServer = oauthServer
oauth.use(express.json())
oauth.use(express.urlencoded({ extended: false }))
// /* model: oauthController.model, */
// /* allowEmptyState: true, */
// /* useErrorHandler: true, */
// /* continueMiddleware: false, */
// /* debug: true, */
// /* requireClientAuthentication: { password: false }, */
// /* authenticateHandler: { */
// /* handle (_req, res) { */
// /* if (!res.locals.user) { */
// /* throw new Error('Not authenticated!') */
// /* } */
// /* return res.locals.user */
// /* } */
// /* } */
// /* }) */
oauth.post('/token', oauthServer.token())
oauth.post('/login', oauthServer.token())
// // oauth.oauthServer = oauthServer
// // oauth.use(express.json())
// // oauth.use(express.urlencoded({ extended: false }))
oauth.get('/authorize', oauthServer.authorize())
oauth.use((req, res) => res.sendStatus(404))
// oauthServer.serializeClient((client, done) => done(null, client.id))
// oauthServer.deserializeClient(async (id, done) => {
// const client = await OAuthServer.findByPk(id)
// done(null, client)
// })
oauth.use((err, req, res, next) => {
const error_msg = err.toString()
log.warn('[OAUTH USE] ' + error_msg)
res.status(500).send(error_msg)
})
module.exports = oauth
// oauth.post('/token', oauthController.token)
// oauth.post('/login', oauthController.token)
// oauth.get('/authorize', oauthController.authorize)
// oauth.use((req, res) => res.sendStatus(404))
// oauth.use((err, req, res, next) => {
// const error_msg = err.toString()
// log.warn('[OAUTH USE] ' + error_msg)
// res.status(500).send(error_msg)
// })
// module.exports = oauth