aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/oauth.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/oauth.ts')
-rw-r--r--server/middlewares/oauth.ts30
1 files changed, 3 insertions, 27 deletions
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts
index 749f5cccd..9d0eaa51f 100644
--- a/server/middlewares/oauth.ts
+++ b/server/middlewares/oauth.ts
@@ -1,17 +1,8 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as OAuthServer from 'express-oauth-server'
3import { OAUTH_LIFETIME } from '../initializers/constants'
4import { logger } from '../helpers/logger' 2import { logger } from '../helpers/logger'
5import { Socket } from 'socket.io' 3import { Socket } from 'socket.io'
6import { getAccessToken } from '../lib/oauth-model' 4import { getAccessToken } from '../lib/oauth-model'
7 5import { oAuthServer } from '@server/lib/auth'
8const oAuthServer = new OAuthServer({
9 useErrorHandler: true,
10 accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
11 refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
12 continueMiddleware: true,
13 model: require('../lib/oauth-model')
14})
15 6
16function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { 7function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) {
17 const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {} 8 const options = authenticateInQuery ? { allowBearerTokensInQueryString: true } : {}
@@ -51,6 +42,7 @@ function authenticateSocket (socket: Socket, next: (err?: any) => void) {
51 42
52 return next() 43 return next()
53 }) 44 })
45 .catch(err => logger.error('Cannot get access token.', { err }))
54} 46}
55 47
56function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) { 48function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) {
@@ -72,27 +64,11 @@ function optionalAuthenticate (req: express.Request, res: express.Response, next
72 return next() 64 return next()
73} 65}
74 66
75function token (req: express.Request, res: express.Response, next: express.NextFunction) {
76 return oAuthServer.token()(req, res, err => {
77 if (err) {
78 return res.status(err.status)
79 .json({
80 error: err.message,
81 code: err.name
82 })
83 .end()
84 }
85
86 return next()
87 })
88}
89
90// --------------------------------------------------------------------------- 67// ---------------------------------------------------------------------------
91 68
92export { 69export {
93 authenticate, 70 authenticate,
94 authenticateSocket, 71 authenticateSocket,
95 authenticatePromiseIfNeeded, 72 authenticatePromiseIfNeeded,
96 optionalAuthenticate, 73 optionalAuthenticate
97 token
98} 74}