diff options
Diffstat (limited to 'server/middlewares/oauth.js')
-rw-r--r-- | server/middlewares/oauth.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/server/middlewares/oauth.js b/server/middlewares/oauth.js new file mode 100644 index 000000000..3d7429f1d --- /dev/null +++ b/server/middlewares/oauth.js | |||
@@ -0,0 +1,35 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const OAuthServer = require('express-oauth-server') | ||
4 | |||
5 | const logger = require('../helpers/logger') | ||
6 | |||
7 | const oAuthServer = new OAuthServer({ | ||
8 | model: require('../lib/oauth-model') | ||
9 | }) | ||
10 | |||
11 | const oAuth = { | ||
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 | |||
33 | // --------------------------------------------------------------------------- | ||
34 | |||
35 | module.exports = oAuth | ||