]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/oauth.ts
Update FAQ
[github/Chocobozzz/PeerTube.git] / server / middlewares / oauth.ts
index 9d0eaa51f277dbbbbb60cb52ac06145eb90e3500..ffc1b7ca9532a780d255ac5ad856fdfa5a25f17f 100644 (file)
@@ -1,8 +1,9 @@
 import * as express from 'express'
-import { logger } from '../helpers/logger'
 import { Socket } from 'socket.io'
-import { getAccessToken } from '../lib/oauth-model'
 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'
 
 function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
   const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {}
@@ -19,12 +20,14 @@ function authenticate (req: express.Request, res: express.Response, next: expres
         .end()
     }
 
+    res.locals.authenticated = true
+
     return next()
   })
 }
 
 function authenticateSocket (socket: Socket, next: (err?: any) => void) {
-  const accessToken = socket.handshake.query.accessToken
+  const accessToken = socket.handshake.query['accessToken']
 
   logger.debug('Checking socket access token %s.', accessToken)
 
@@ -38,7 +41,7 @@ function authenticateSocket (socket: Socket, next: (err?: any) => void) {
         return next(new Error('Invalid access token.'))
       }
 
-      socket.handshake.query.user = tokenDB.User
+      socket.handshake.query['user'] = tokenDB.User
 
       return next()
     })
@@ -48,9 +51,9 @@ function authenticateSocket (socket: Socket, next: (err?: any) => void) {
 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.oauth?.token.User) return resolve()
 
-    if (res.locals.authenticated === false) return res.sendStatus(401)
+    if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401)
 
     authenticate(req, res, () => resolve(), authenticateInQuery)
   })