2020-04-22 09:07:04 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { OAUTH_LIFETIME } from '@server/initializers/constants'
|
|
|
|
import * as OAuthServer from 'express-oauth-server'
|
|
|
|
import { PluginManager } from '@server/lib/plugins/plugin-manager'
|
|
|
|
import { RegisterServerAuthPassOptions } from '@shared/models/plugins/register-server-auth.model'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { UserRole } from '@shared/models'
|
2020-04-23 04:36:50 -05:00
|
|
|
import { revokeToken } from '@server/lib/oauth-model'
|
2020-04-24 04:33:01 -05:00
|
|
|
import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
|
2020-04-22 09:07:04 -05:00
|
|
|
|
|
|
|
const oAuthServer = new OAuthServer({
|
|
|
|
useErrorHandler: true,
|
|
|
|
accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
|
|
|
|
refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
|
|
|
|
continueMiddleware: true,
|
|
|
|
model: require('./oauth-model')
|
|
|
|
})
|
|
|
|
|
|
|
|
function onExternalAuthPlugin (npmName: string, username: string, email: string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleIdAndPassLogin (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2020-04-24 04:33:01 -05:00
|
|
|
const grantType = req.body.grant_type
|
|
|
|
|
|
|
|
if (grantType === 'password') await proxifyPasswordGrant(req, res)
|
|
|
|
else if (grantType === 'refresh_token') await proxifyRefreshGrant(req, res)
|
|
|
|
|
|
|
|
return forwardTokenReq(req, res, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleTokenRevocation (req: express.Request, res: express.Response) {
|
|
|
|
const token = res.locals.oauth.token
|
|
|
|
|
|
|
|
res.locals.explicitLogout = true
|
|
|
|
await revokeToken(token)
|
|
|
|
|
|
|
|
// FIXME: uncomment when https://github.com/oauthjs/node-oauth2-server/pull/289 is released
|
|
|
|
// oAuthServer.revoke(req, res, err => {
|
|
|
|
// if (err) {
|
|
|
|
// logger.warn('Error in revoke token handler.', { err })
|
|
|
|
//
|
|
|
|
// return res.status(err.status)
|
|
|
|
// .json({
|
|
|
|
// error: err.message,
|
|
|
|
// code: err.name
|
|
|
|
// })
|
|
|
|
// .end()
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
|
|
|
|
return res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
oAuthServer,
|
|
|
|
handleIdAndPassLogin,
|
|
|
|
onExternalAuthPlugin,
|
|
|
|
handleTokenRevocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function forwardTokenReq (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
return oAuthServer.token()(req, res, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.warn('Login error.', { err })
|
|
|
|
|
|
|
|
return res.status(err.status)
|
|
|
|
.json({
|
|
|
|
error: err.message,
|
|
|
|
code: err.name
|
|
|
|
})
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function proxifyRefreshGrant (req: express.Request, res: express.Response) {
|
|
|
|
const refreshToken = req.body.refresh_token
|
|
|
|
if (!refreshToken) return
|
|
|
|
|
|
|
|
const tokenModel = await OAuthTokenModel.loadByRefreshToken(refreshToken)
|
|
|
|
if (tokenModel?.authName) res.locals.refreshTokenAuthName = tokenModel.authName
|
|
|
|
}
|
|
|
|
|
|
|
|
async function proxifyPasswordGrant (req: express.Request, res: express.Response) {
|
2020-04-22 09:07:04 -05:00
|
|
|
const plugins = PluginManager.Instance.getIdAndPassAuths()
|
|
|
|
const pluginAuths: { npmName?: string, registerAuthOptions: RegisterServerAuthPassOptions }[] = []
|
|
|
|
|
|
|
|
for (const plugin of plugins) {
|
|
|
|
const auths = plugin.idAndPassAuths
|
|
|
|
|
|
|
|
for (const auth of auths) {
|
|
|
|
pluginAuths.push({
|
|
|
|
npmName: plugin.npmName,
|
|
|
|
registerAuthOptions: auth
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginAuths.sort((a, b) => {
|
|
|
|
const aWeight = a.registerAuthOptions.getWeight()
|
|
|
|
const bWeight = b.registerAuthOptions.getWeight()
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
// DESC weight order
|
2020-04-22 09:07:04 -05:00
|
|
|
if (aWeight === bWeight) return 0
|
2020-04-23 04:36:50 -05:00
|
|
|
if (aWeight < bWeight) return 1
|
2020-04-22 09:07:04 -05:00
|
|
|
return -1
|
|
|
|
})
|
|
|
|
|
|
|
|
const loginOptions = {
|
|
|
|
id: req.body.username,
|
|
|
|
password: req.body.password
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const pluginAuth of pluginAuths) {
|
2020-04-23 04:36:50 -05:00
|
|
|
const authOptions = pluginAuth.registerAuthOptions
|
|
|
|
|
2020-04-22 09:07:04 -05:00
|
|
|
logger.debug(
|
2020-04-23 04:36:50 -05:00
|
|
|
'Using auth method %s of plugin %s to login %s with weight %d.',
|
|
|
|
authOptions.authName, pluginAuth.npmName, loginOptions.id, authOptions.getWeight()
|
2020-04-22 09:07:04 -05:00
|
|
|
)
|
|
|
|
|
2020-04-23 04:36:50 -05:00
|
|
|
const loginResult = await authOptions.login(loginOptions)
|
2020-04-22 09:07:04 -05:00
|
|
|
if (loginResult) {
|
2020-04-23 04:36:50 -05:00
|
|
|
logger.info(
|
|
|
|
'Login success with auth method %s of plugin %s for %s.',
|
|
|
|
authOptions.authName, pluginAuth.npmName, loginOptions.id
|
|
|
|
)
|
2020-04-22 09:07:04 -05:00
|
|
|
|
|
|
|
res.locals.bypassLogin = {
|
|
|
|
bypass: true,
|
|
|
|
pluginName: pluginAuth.npmName,
|
2020-04-23 04:36:50 -05:00
|
|
|
authName: authOptions.authName,
|
2020-04-22 09:07:04 -05:00
|
|
|
user: {
|
|
|
|
username: loginResult.username,
|
|
|
|
email: loginResult.email,
|
|
|
|
role: loginResult.role || UserRole.USER,
|
|
|
|
displayName: loginResult.displayName || loginResult.username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 04:33:01 -05:00
|
|
|
return
|
2020-04-22 09:07:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|