diff options
Diffstat (limited to 'server/middlewares/oauth.ts')
-rw-r--r-- | server/middlewares/oauth.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts new file mode 100644 index 000000000..31ae1e000 --- /dev/null +++ b/server/middlewares/oauth.ts | |||
@@ -0,0 +1,34 @@ | |||
1 | import OAuthServer = require('express-oauth-server') | ||
2 | |||
3 | const constants = require('../initializers/constants') | ||
4 | const logger = require('../helpers/logger') | ||
5 | |||
6 | const oAuthServer = new OAuthServer({ | ||
7 | accessTokenLifetime: constants.OAUTH_LIFETIME.ACCESS_TOKEN, | ||
8 | refreshTokenLifetime: constants.OAUTH_LIFETIME.REFRESH_TOKEN, | ||
9 | model: require('../lib/oauth-model') | ||
10 | }) | ||
11 | |||
12 | function authenticate (req, res, next) { | ||
13 | oAuthServer.authenticate()(req, res, function (err) { | ||
14 | if (err) { | ||
15 | logger.error('Cannot authenticate.', { error: err }) | ||
16 | return res.sendStatus(500) | ||
17 | } | ||
18 | |||
19 | if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) return res.end() | ||
20 | |||
21 | return next() | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | function token (req, res, next) { | ||
26 | return oAuthServer.token()(req, res, next) | ||
27 | } | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | export { | ||
32 | authenticate, | ||
33 | token | ||
34 | } | ||