X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Foauth.ts;h=280595acc3a08b27c8eab9fd3c0a2bd161e63e3a;hb=829523cfa5177a1810d9373f9a25bf3b18138d1e;hp=07bbded57d636d3a5999c46cc7b94621981c4c72;hpb=e02643f32e4c97ca307f8fc5b69be79c40d70a3b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 07bbded57..280595acc 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -1,34 +1,78 @@ -import OAuthServer = require('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')) + 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(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 }