X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Foauth.ts;h=ffc1b7ca9532a780d255ac5ad856fdfa5a25f17f;hb=83338e45e3d583b09a5f6920ee5516b64448d9c3;hp=468e418106c69c84ceb596b705665ebadfd7de40;hpb=4d4e5cd4dca78480ec7f40e747f424cd107376a4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 468e41810..ffc1b7ca9 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -1,34 +1,77 @@ -import * as OAuthServer from 'express-oauth-server' +import * as express from 'express' +import { Socket } from 'socket.io' +import { oAuthServer } from '@server/lib/auth' +import { logger } from '../helpers/logger' +import { getAccessToken } from '../lib/oauth-model' +import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' -import { OAUTH_LIFETIME } from '../initializers' -import { logger } from '../helpers' +function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { + const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {} -const oAuthServer = new OAuthServer({ - accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN, - refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN, - model: require('../lib/oauth-model') -}) - -function authenticate (req, res, next) { - oAuthServer.authenticate()(req, res, function (err) { + oAuthServer.authenticate(options)(req, res, err => { if (err) { - logger.error('Cannot authenticate.', { error: err }) - return res.sendStatus(500) + logger.warn('Cannot authenticate.', { err }) + + return res.status(err.status) + .json({ + error: 'Token is invalid.', + code: err.name + }) + .end() } - if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) return res.end() + res.locals.authenticated = true return next() }) } -function token (req, res, next) { - return oAuthServer.token()(req, res, next) +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')) + + 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.query['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(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, - token + authenticateSocket, + authenticatePromiseIfNeeded, + optionalAuthenticate }