]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/oauth.js
Update README
[github/Chocobozzz/PeerTube.git] / server / middlewares / oauth.js
CommitLineData
9457bf88
C
1'use strict'
2
3const OAuthServer = require('express-oauth-server')
4
2f372a86 5const constants = require('../initializers/constants')
0c1cbbfe
C
6const logger = require('../helpers/logger')
7
8const oAuthServer = new OAuthServer({
2f372a86
C
9 accessTokenLifetime: constants.OAUTH_LIFETIME.ACCESS_TOKEN,
10 refreshTokenLifetime: constants.OAUTH_LIFETIME.REFRESH_TOKEN,
69b0a27c 11 model: require('../lib/oauth-model')
9457bf88
C
12})
13
69b0a27c 14const oAuth = {
c4403b29
C
15 authenticate,
16 token
0c1cbbfe
C
17}
18
19function authenticate (req, res, next) {
20 oAuthServer.authenticate()(req, res, function (err) {
21 if (err) {
22 logger.error('Cannot authenticate.', { error: err })
23 return res.sendStatus(500)
24 }
25
f9b2d2ce 26 if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) return res.end()
0c1cbbfe
C
27
28 return next()
29 })
30}
31
32function token (req, res, next) {
33 return oAuthServer.token()(req, res, next)
34}
35
9457bf88
C
36// ---------------------------------------------------------------------------
37
69b0a27c 38module.exports = oAuth