1 import * as express from 'express'
2 import * as OAuthServer from 'express-oauth-server'
3 import 'express-validator'
4 import { OAUTH_LIFETIME } from '../initializers'
5 import { logger } from '../helpers/logger'
7 const oAuthServer = new OAuthServer({
9 accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
10 refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
11 model: require('../lib/oauth-model')
14 function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
15 oAuthServer.authenticate()(req, res, err => {
17 logger.warn('Cannot authenticate.', { err })
19 return res.status(err.status)
21 error: 'Token is invalid.',
31 function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
32 if (req.header('authorization')) return authenticate(req, res, next)
37 function token (req: express.Request, res: express.Response, next: express.NextFunction) {
38 return oAuthServer.token()(req, res, err => {
40 return res.status(err.status)
52 // ---------------------------------------------------------------------------