diff options
Diffstat (limited to 'server/middlewares/oauth.ts')
-rw-r--r-- | server/middlewares/oauth.ts | 30 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as OAuthServer from 'express-oauth-server' | ||
3 | import { OAUTH_LIFETIME } from '../initializers/constants' | ||
4 | import { logger } from '../helpers/logger' | 2 | import { logger } from '../helpers/logger' |
5 | import { Socket } from 'socket.io' | 3 | import { Socket } from 'socket.io' |
6 | import { getAccessToken } from '../lib/oauth-model' | 4 | import { getAccessToken } from '../lib/oauth-model' |
7 | 5 | import { oAuthServer } from '@server/lib/auth' | |
8 | const 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 | ||
16 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { | 7 | function 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 | ||
56 | function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) { | 48 | function 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 | ||
75 | function 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 | ||
92 | export { | 69 | export { |
93 | authenticate, | 70 | authenticate, |
94 | authenticateSocket, | 71 | authenticateSocket, |
95 | authenticatePromiseIfNeeded, | 72 | authenticatePromiseIfNeeded, |
96 | optionalAuthenticate, | 73 | optionalAuthenticate |
97 | token | ||
98 | } | 74 | } |