X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Foauth.ts;h=9eef03bb43fc8bba3384dd350ebe977dcc25214a;hb=76314386aeafdd6849b7b70c517779d6b2013473;hp=77fb305dd5247063b538a502def2aa3df3d2e7a0;hpb=c8861d5dc0436ef4342ce517241e3591fa256a13;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 77fb305dd..9eef03bb4 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -9,11 +9,14 @@ const oAuthServer = new OAuthServer({ useErrorHandler: true, accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN, refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN, + continueMiddleware: true, model: require('../lib/oauth-model') }) -function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { - oAuthServer.authenticate()(req, res, err => { +function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { + const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {} + + oAuthServer.authenticate(options)(req, res, err => { if (err) { logger.warn('Cannot authenticate.', { err }) @@ -48,18 +51,17 @@ function authenticateSocket (socket: Socket, next: (err?: any) => void) { return next() }) + .catch(err => logger.error('Cannot get access token.', { err })) } -function authenticatePromiseIfNeeded (req: express.Request, res: express.Response) { +function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) { return new Promise(resolve => { // Already authenticated? (or tried to) if (res.locals.oauth && res.locals.oauth.token.User) return resolve() if (res.locals.authenticated === false) return res.sendStatus(401) - authenticate(req, res, () => { - return resolve() - }) + authenticate(req, res, () => resolve(), authenticateInQuery) }) }