]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/oauth.ts
Remove octet stream fallback for video extension
[github/Chocobozzz/PeerTube.git] / server / middlewares / oauth.ts
1 import * as express from 'express'
2 import * as OAuthServer from 'express-oauth-server'
3 import { OAUTH_LIFETIME } from '../initializers/constants'
4 import { logger } from '../helpers/logger'
5 import { Socket } from 'socket.io'
6 import { getAccessToken } from '../lib/oauth-model'
7
8 const oAuthServer = new OAuthServer({
9 useErrorHandler: true,
10 accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
11 refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
12 continueMiddleware: true,
13 model: require('../lib/oauth-model')
14 })
15
16 function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
17 const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {}
18
19 oAuthServer.authenticate(options)(req, res, err => {
20 if (err) {
21 logger.warn('Cannot authenticate.', { err })
22
23 return res.status(err.status)
24 .json({
25 error: 'Token is invalid.',
26 code: err.name
27 })
28 .end()
29 }
30
31 return next()
32 })
33 }
34
35 function authenticateSocket (socket: Socket, next: (err?: any) => void) {
36 const accessToken = socket.handshake.query.accessToken
37
38 logger.debug('Checking socket access token %s.', accessToken)
39
40 if (!accessToken) return next(new Error('No access token provided'))
41
42 getAccessToken(accessToken)
43 .then(tokenDB => {
44 const now = new Date()
45
46 if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) {
47 return next(new Error('Invalid access token.'))
48 }
49
50 socket.handshake.query.user = tokenDB.User
51
52 return next()
53 })
54 }
55
56 function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) {
57 return new Promise(resolve => {
58 // Already authenticated? (or tried to)
59 if (res.locals.oauth && res.locals.oauth.token.User) return resolve()
60
61 if (res.locals.authenticated === false) return res.sendStatus(401)
62
63 authenticate(req, res, () => resolve(), authenticateInQuery)
64 })
65 }
66
67 function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
68 if (req.header('authorization')) return authenticate(req, res, next)
69
70 res.locals.authenticated = false
71
72 return next()
73 }
74
75 function token (req: express.Request, res: express.Response, next: express.NextFunction) {
76 return oAuthServer.token()(req, res, err => {
77 if (err) {
78 return res.status(err.status)
79 .json({
80 error: err.message,
81 code: err.name
82 })
83 .end()
84 }
85
86 return next()
87 })
88 }
89
90 // ---------------------------------------------------------------------------
91
92 export {
93 authenticate,
94 authenticateSocket,
95 authenticatePromiseIfNeeded,
96 optionalAuthenticate,
97 token
98 }