X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fauth.ts;h=904d47efd1fd019dfe805f456556f1b73bb9ad8e;hb=eb7b48ce84e5872d1333edd892f2e6104a18d7f1;hp=f38373624c1cccf006c3be35de067cdf7c552516;hpb=dc48fdbe68e9dd3a3a6028181e61d8595d98e654;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/auth.ts b/server/middlewares/auth.ts index f38373624..904d47efd 100644 --- a/server/middlewares/auth.ts +++ b/server/middlewares/auth.ts @@ -1,7 +1,7 @@ -import * as express from 'express' +import 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 { HttpStatusCode } from '../../shared/models/http/http-error-codes' import { logger } from '../helpers/logger' import { handleOAuthAuthenticate } from '../lib/auth/oauth' @@ -14,13 +14,13 @@ function authenticate (req: express.Request, res: express.Response, next: expres return next() }) .catch(err => { - logger.warn('Cannot authenticate.', { err }) + logger.info('Cannot authenticate.', { err }) - return res.status(err.status) - .json({ - error: 'Token is invalid.', - code: err.name - }) + return res.fail({ + status: err.status, + message: 'Token is invalid', + type: err.name + }) }) } @@ -47,12 +47,17 @@ function authenticateSocket (socket: Socket, next: (err?: any) => void) { .catch(err => logger.error('Cannot get access token.', { err })) } -function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) { +function authenticatePromise (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) + if (res.locals.authenticated === false) { + return res.fail({ + status: HttpStatusCode.UNAUTHORIZED_401, + message: 'Not authenticated' + }) + } authenticate(req, res, () => resolve(), authenticateInQuery) }) @@ -71,6 +76,6 @@ function optionalAuthenticate (req: express.Request, res: express.Response, next export { authenticate, authenticateSocket, - authenticatePromiseIfNeeded, + authenticatePromise, optionalAuthenticate }