gancio-upstream/server/api/oauth.js

42 lines
1 KiB
JavaScript
Raw Normal View History

const express = require('express')
const OAuthServer = require('express-oauth-server')
const oauth = express.Router()
const oauthController = require('./controller/oauth')
2021-03-05 14:17:10 +01:00
const log = require('../log')
const oauthServer = new OAuthServer({
model: oauthController.model,
2020-01-21 01:24:10 +01:00
allowEmptyState: true,
useErrorHandler: true,
2020-01-21 01:24:10 +01:00
continueMiddleware: false,
debug: true,
2020-01-27 00:47:03 +01:00
requireClientAuthentication: { password: false },
2020-01-21 01:24:10 +01:00
authenticateHandler: {
2022-02-26 21:27:40 +01:00
handle (req, res) {
if (!res.locals.user) {
2020-01-21 01:24:10 +01:00
throw new Error('Not authenticated!')
}
2022-02-26 21:27:40 +01:00
return res.locals.user
2020-01-21 01:24:10 +01:00
}
}
})
2020-01-21 01:24:10 +01:00
oauth.oauthServer = oauthServer
2020-01-03 22:24:27 +01:00
oauth.use(express.json())
2020-01-21 01:24:10 +01:00
oauth.use(express.urlencoded({ extended: false }))
2020-01-03 22:24:27 +01:00
oauth.post('/token', oauthServer.token())
2020-01-27 00:47:03 +01:00
oauth.post('/login', oauthServer.token())
2020-01-21 01:24:10 +01:00
oauth.get('/authorize', oauthServer.authorize())
2020-01-27 00:47:03 +01:00
oauth.use((req, res) => res.sendStatus(404))
oauth.use((err, req, res, next) => {
2020-01-21 01:24:10 +01:00
const error_msg = err.toString()
log.warn('[OAUTH USE] ' + error_msg)
2020-01-21 01:24:10 +01:00
res.status(500).send(error_msg)
})
2020-01-21 01:24:10 +01:00
module.exports = oauth