]>
Commit | Line | Data |
---|---|---|
69818c93 C |
1 | import 'express-validator' |
2 | import * as express from 'express' | |
4d4e5cd4 | 3 | import * as OAuthServer from 'express-oauth-server' |
da854ddd | 4 | import { logger } from '../helpers/logger' |
e02643f3 | 5 | import { OAUTH_LIFETIME } from '../initializers' |
0c1cbbfe C |
6 | |
7 | const oAuthServer = new OAuthServer({ | |
e02643f3 C |
8 | accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN, |
9 | refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN, | |
69b0a27c | 10 | model: require('../lib/oauth-model') |
9457bf88 C |
11 | }) |
12 | ||
69818c93 | 13 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { |
075f16ca | 14 | oAuthServer.authenticate()(req, res, err => { |
0c1cbbfe | 15 | if (err) { |
ad0997ad | 16 | logger.error('Cannot authenticate.', err) |
0c1cbbfe C |
17 | return res.sendStatus(500) |
18 | } | |
19 | ||
eec63bbc C |
20 | if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) { |
21 | return res.json({ | |
22 | error: 'Authentication failed.' | |
23 | }).end() | |
24 | } | |
0c1cbbfe C |
25 | |
26 | return next() | |
27 | }) | |
28 | } | |
29 | ||
69818c93 | 30 | function token (req: express.Request, res: express.Response, next: express.NextFunction) { |
0c1cbbfe C |
31 | return oAuthServer.token()(req, res, next) |
32 | } | |
33 | ||
9457bf88 C |
34 | // --------------------------------------------------------------------------- |
35 | ||
65fcc311 C |
36 | export { |
37 | authenticate, | |
38 | token | |
39 | } |