]>
Commit | Line | Data |
---|---|---|
9457bf88 C |
1 | 'use strict' |
2 | ||
3 | const OAuthServer = require('express-oauth-server') | |
4 | ||
2f372a86 | 5 | const constants = require('../initializers/constants') |
0c1cbbfe C |
6 | const logger = require('../helpers/logger') |
7 | ||
8 | const 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 | 14 | const oAuth = { |
c4403b29 C |
15 | authenticate, |
16 | token | |
0c1cbbfe C |
17 | } |
18 | ||
19 | function 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 | ||
32 | function token (req, res, next) { | |
33 | return oAuthServer.token()(req, res, next) | |
34 | } | |
35 | ||
9457bf88 C |
36 | // --------------------------------------------------------------------------- |
37 | ||
69b0a27c | 38 | module.exports = oAuth |