diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-04-14 22:06:11 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-04-14 22:06:11 +0200 |
commit | 0c1cbbfe29d91c95f9c574b57adf067654b8b5b4 (patch) | |
tree | b61e48caedea8881cbfd1159295f6c311b35be90 /server/middlewares | |
parent | c9bf7d30bd688e84b12dca03029dde6e9645a997 (diff) | |
download | PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.gz PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.zst PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.zip |
Add authentications for routes that need it and adapts the tests
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/oauth2.js | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/server/middlewares/oauth2.js b/server/middlewares/oauth2.js index a1fa61fbb..1defdc02e 100644 --- a/server/middlewares/oauth2.js +++ b/server/middlewares/oauth2.js | |||
@@ -2,10 +2,34 @@ | |||
2 | 2 | ||
3 | const OAuthServer = require('express-oauth-server') | 3 | const OAuthServer = require('express-oauth-server') |
4 | 4 | ||
5 | const oAuth2 = new OAuthServer({ | 5 | const logger = require('../helpers/logger') |
6 | |||
7 | const oAuthServer = new OAuthServer({ | ||
6 | model: require('../models/users') | 8 | model: require('../models/users') |
7 | }) | 9 | }) |
8 | 10 | ||
11 | const oAuth2 = { | ||
12 | authenticate: authenticate, | ||
13 | token: token | ||
14 | } | ||
15 | |||
16 | function authenticate (req, res, next) { | ||
17 | oAuthServer.authenticate()(req, res, function (err) { | ||
18 | if (err) { | ||
19 | logger.error('Cannot authenticate.', { error: err }) | ||
20 | return res.sendStatus(500) | ||
21 | } | ||
22 | |||
23 | if (res.statusCode === 401 || res.statusCode === 400) return res.end() | ||
24 | |||
25 | return next() | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | function token (req, res, next) { | ||
30 | return oAuthServer.token()(req, res, next) | ||
31 | } | ||
32 | |||
9 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
10 | 34 | ||
11 | module.exports = oAuth2 | 35 | module.exports = oAuth2 |