2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
|
|
|
import RateLimit from 'express-rate-limit'
|
2021-03-12 09:20:46 -05:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2020-04-23 05:36:50 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2021-03-12 09:20:46 -05:00
|
|
|
import { getAuthNameFromRefreshGrant, getBypassFromExternalAuth, getBypassFromPasswordGrant } from '@server/lib/auth/external-auth'
|
|
|
|
import { handleOAuthToken } from '@server/lib/auth/oauth'
|
|
|
|
import { BypassLogin, revokeToken } from '@server/lib/auth/oauth-model'
|
2020-04-23 05:36:50 -04:00
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
2021-06-04 03:16:23 -04:00
|
|
|
import { asyncMiddleware, authenticate, openapiOperationDoc } from '@server/middlewares'
|
2021-12-17 05:58:15 -05:00
|
|
|
import { buildUUID } from '@shared/core-utils'
|
2020-08-13 09:07:23 -04:00
|
|
|
import { ScopedToken } from '@shared/models/users/user-scoped-token'
|
2020-04-23 05:36:50 -04:00
|
|
|
|
|
|
|
const tokensRouter = express.Router()
|
|
|
|
|
|
|
|
const loginRateLimiter = RateLimit({
|
|
|
|
windowMs: CONFIG.RATES_LIMIT.LOGIN.WINDOW_MS,
|
|
|
|
max: CONFIG.RATES_LIMIT.LOGIN.MAX
|
|
|
|
})
|
|
|
|
|
|
|
|
tokensRouter.post('/token',
|
|
|
|
loginRateLimiter,
|
2021-06-04 03:16:23 -04:00
|
|
|
openapiOperationDoc({ operationId: 'getOAuthToken' }),
|
2021-03-12 09:20:46 -05:00
|
|
|
asyncMiddleware(handleToken)
|
2020-04-23 05:36:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
tokensRouter.post('/revoke-token',
|
2021-06-04 03:16:23 -04:00
|
|
|
openapiOperationDoc({ operationId: 'revokeOAuthToken' }),
|
2020-04-23 05:36:50 -04:00
|
|
|
authenticate,
|
2020-04-24 05:33:01 -04:00
|
|
|
asyncMiddleware(handleTokenRevocation)
|
2020-04-23 05:36:50 -04:00
|
|
|
)
|
|
|
|
|
2020-08-13 09:07:23 -04:00
|
|
|
tokensRouter.get('/scoped-tokens',
|
|
|
|
authenticate,
|
|
|
|
getScopedTokens
|
|
|
|
)
|
|
|
|
|
|
|
|
tokensRouter.post('/scoped-tokens',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(renewScopedTokens)
|
|
|
|
)
|
|
|
|
|
2020-04-23 05:36:50 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
tokensRouter
|
|
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-03-12 09:20:46 -05:00
|
|
|
async function handleToken (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const grantType = req.body.grant_type
|
|
|
|
|
|
|
|
try {
|
|
|
|
const bypassLogin = await buildByPassLogin(req, grantType)
|
|
|
|
|
|
|
|
const refreshTokenAuthName = grantType === 'refresh_token'
|
|
|
|
? await getAuthNameFromRefreshGrant(req.body.refresh_token)
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
refreshTokenAuthName,
|
|
|
|
bypassLogin
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = await handleOAuthToken(req, options)
|
|
|
|
|
|
|
|
res.set('Cache-Control', 'no-store')
|
|
|
|
res.set('Pragma', 'no-cache')
|
|
|
|
|
2021-11-24 08:33:14 -05:00
|
|
|
Hooks.runAction('action:api.user.oauth2-got-token', { username: token.user.username, ip: req.ip, req, res })
|
2021-03-12 09:20:46 -05:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
token_type: 'Bearer',
|
2020-04-23 05:36:50 -04:00
|
|
|
|
2021-03-12 09:20:46 -05:00
|
|
|
access_token: token.accessToken,
|
|
|
|
refresh_token: token.refreshToken,
|
|
|
|
|
|
|
|
expires_in: token.accessTokenExpiresIn,
|
|
|
|
refresh_token_expires_in: token.refreshTokenExpiresIn
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Login error', { err })
|
|
|
|
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: err.code,
|
|
|
|
message: err.message,
|
|
|
|
type: err.name
|
2021-03-12 09:20:46 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleTokenRevocation (req: express.Request, res: express.Response) {
|
|
|
|
const token = res.locals.oauth.token
|
|
|
|
|
2021-03-12 11:19:02 -05:00
|
|
|
const result = await revokeToken(token, { req, explicitLogout: true })
|
2021-03-12 09:20:46 -05:00
|
|
|
|
|
|
|
return res.json(result)
|
2020-04-23 05:36:50 -04:00
|
|
|
}
|
2020-08-13 09:07:23 -04:00
|
|
|
|
|
|
|
function getScopedTokens (req: express.Request, res: express.Response) {
|
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
feedToken: user.feedToken
|
|
|
|
} as ScopedToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renewScopedTokens (req: express.Request, res: express.Response) {
|
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
|
2021-06-28 11:30:59 -04:00
|
|
|
user.feedToken = buildUUID()
|
2020-08-13 09:07:23 -04:00
|
|
|
await user.save()
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
feedToken: user.feedToken
|
|
|
|
} as ScopedToken)
|
|
|
|
}
|
2021-03-12 09:20:46 -05:00
|
|
|
|
|
|
|
async function buildByPassLogin (req: express.Request, grantType: string): Promise<BypassLogin> {
|
|
|
|
if (grantType !== 'password') return undefined
|
|
|
|
|
|
|
|
if (req.body.externalAuthToken) {
|
|
|
|
// Consistency with the getBypassFromPasswordGrant promise
|
|
|
|
return getBypassFromExternalAuth(req.body.username, req.body.externalAuthToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getBypassFromPasswordGrant(req.body.username, req.body.password)
|
|
|
|
}
|