]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/auth.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / middlewares / auth.ts
CommitLineData
69818c93 1import * as express from 'express'
cef534ed 2import { Socket } from 'socket.io'
f43db2f4 3import { getAccessToken } from '@server/lib/auth/oauth-model'
2d53be02 4import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
f43db2f4
C
5import { logger } from '../helpers/logger'
6import { handleOAuthAuthenticate } from '../lib/auth/oauth'
9457bf88 7
eccf70f0 8function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
f43db2f4
C
9 handleOAuthAuthenticate(req, res, authenticateInQuery)
10 .then((token: any) => {
11 res.locals.oauth = { token }
12 res.locals.authenticated = true
13
14 return next()
15 })
16 .catch(err => {
0883b324
C
17 logger.warn('Cannot authenticate.', { err })
18
93e4a311
C
19 return res.status(err.status)
20 .json({
5960f923 21 error: 'Token is invalid.',
93e4a311
C
22 code: err.name
23 })
f43db2f4 24 })
0c1cbbfe
C
25}
26
cef534ed 27function authenticateSocket (socket: Socket, next: (err?: any) => void) {
fce7fe04 28 const accessToken = socket.handshake.query['accessToken']
cef534ed
C
29
30 logger.debug('Checking socket access token %s.', accessToken)
31
3acc5084 32 if (!accessToken) return next(new Error('No access token provided'))
fbd51e69 33 if (typeof accessToken !== 'string') return next(new Error('Access token is invalid'))
3acc5084 34
cef534ed
C
35 getAccessToken(accessToken)
36 .then(tokenDB => {
37 const now = new Date()
38
39 if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) {
40 return next(new Error('Invalid access token.'))
41 }
42
fbd51e69 43 socket.handshake.auth.user = tokenDB.User
cef534ed
C
44
45 return next()
46 })
a1587156 47 .catch(err => logger.error('Cannot get access token.', { err }))
cef534ed
C
48}
49
eccf70f0 50function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) {
ba5a8d89 51 return new Promise<void>(resolve => {
8d427346 52 // Already authenticated? (or tried to)
faa9d434 53 if (res.locals.oauth?.token.User) return resolve()
8d427346 54
2d53be02 55 if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401)
8d427346 56
eccf70f0 57 authenticate(req, res, () => resolve(), authenticateInQuery)
8d427346
C
58 })
59}
60
0883b324
C
61function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
62 if (req.header('authorization')) return authenticate(req, res, next)
63
8d427346
C
64 res.locals.authenticated = false
65
0883b324
C
66 return next()
67}
68
9457bf88
C
69// ---------------------------------------------------------------------------
70
65fcc311
C
71export {
72 authenticate,
cef534ed 73 authenticateSocket,
8d427346 74 authenticatePromiseIfNeeded,
7fed6375 75 optionalAuthenticate
65fcc311 76}