mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
Refactor auth flow
Reimplement some node-oauth2-server methods to remove hacky code needed by our external login workflow
This commit is contained in:
parent
cae2df6bdc
commit
f43db2f46e
24 changed files with 487 additions and 255 deletions
76
server/middlewares/auth.ts
Normal file
76
server/middlewares/auth.ts
Normal file
|
@ -0,0 +1,76 @@
|
|||
import * as express from 'express'
|
||||
import { Socket } from 'socket.io'
|
||||
import { getAccessToken } from '@server/lib/auth/oauth-model'
|
||||
import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
|
||||
import { logger } from '../helpers/logger'
|
||||
import { handleOAuthAuthenticate } from '../lib/auth/oauth'
|
||||
|
||||
function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
|
||||
handleOAuthAuthenticate(req, res, authenticateInQuery)
|
||||
.then((token: any) => {
|
||||
res.locals.oauth = { token }
|
||||
res.locals.authenticated = true
|
||||
|
||||
return next()
|
||||
})
|
||||
.catch(err => {
|
||||
logger.warn('Cannot authenticate.', { err })
|
||||
|
||||
return res.status(err.status)
|
||||
.json({
|
||||
error: 'Token is invalid.',
|
||||
code: err.name
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function authenticateSocket (socket: Socket, next: (err?: any) => void) {
|
||||
const accessToken = socket.handshake.query['accessToken']
|
||||
|
||||
logger.debug('Checking socket access token %s.', accessToken)
|
||||
|
||||
if (!accessToken) return next(new Error('No access token provided'))
|
||||
if (typeof accessToken !== 'string') return next(new Error('Access token is invalid'))
|
||||
|
||||
getAccessToken(accessToken)
|
||||
.then(tokenDB => {
|
||||
const now = new Date()
|
||||
|
||||
if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) {
|
||||
return next(new Error('Invalid access token.'))
|
||||
}
|
||||
|
||||
socket.handshake.auth.user = tokenDB.User
|
||||
|
||||
return next()
|
||||
})
|
||||
.catch(err => logger.error('Cannot get access token.', { err }))
|
||||
}
|
||||
|
||||
function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) {
|
||||
return new Promise<void>(resolve => {
|
||||
// Already authenticated? (or tried to)
|
||||
if (res.locals.oauth?.token.User) return resolve()
|
||||
|
||||
if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401)
|
||||
|
||||
authenticate(req, res, () => resolve(), authenticateInQuery)
|
||||
})
|
||||
}
|
||||
|
||||
function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
if (req.header('authorization')) return authenticate(req, res, next)
|
||||
|
||||
res.locals.authenticated = false
|
||||
|
||||
return next()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
authenticate,
|
||||
authenticateSocket,
|
||||
authenticatePromiseIfNeeded,
|
||||
optionalAuthenticate
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue