diff options
author | Gérald Niel <gerald.niel@gegeweb.org> | 2018-04-19 19:28:55 +0200 |
---|---|---|
committer | Gérald Niel <gerald.niel@gegeweb.org> | 2018-04-19 19:28:55 +0200 |
commit | 0db1a226507a39b0a663e3e63f04851836a44d5a (patch) | |
tree | 8c8dad658ea5608afaae238cfb6fe803292f908f /server/middlewares/oauth.ts | |
parent | 0030284b0df2983914291d6fe83675e2aa892e6a (diff) | |
parent | d4f3fea659163749f9ea879a5e1dd313106353c6 (diff) | |
download | PeerTube-0db1a226507a39b0a663e3e63f04851836a44d5a.tar.gz PeerTube-0db1a226507a39b0a663e3e63f04851836a44d5a.tar.zst PeerTube-0db1a226507a39b0a663e3e63f04851836a44d5a.zip |
Merge branch 'develop' of framagit.org:chocobozzz/PeerTube into develop
Diffstat (limited to 'server/middlewares/oauth.ts')
-rw-r--r-- | server/middlewares/oauth.ts | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 41a3fb718..a6f28dd5b 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts | |||
@@ -2,6 +2,7 @@ import * as express from 'express' | |||
2 | import * as OAuthServer from 'express-oauth-server' | 2 | import * as OAuthServer from 'express-oauth-server' |
3 | import 'express-validator' | 3 | import 'express-validator' |
4 | import { OAUTH_LIFETIME } from '../initializers' | 4 | import { OAUTH_LIFETIME } from '../initializers' |
5 | import { logger } from '../helpers/logger' | ||
5 | 6 | ||
6 | const oAuthServer = new OAuthServer({ | 7 | const oAuthServer = new OAuthServer({ |
7 | useErrorHandler: true, | 8 | useErrorHandler: true, |
@@ -13,6 +14,8 @@ const oAuthServer = new OAuthServer({ | |||
13 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { | 14 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { |
14 | oAuthServer.authenticate()(req, res, err => { | 15 | oAuthServer.authenticate()(req, res, err => { |
15 | if (err) { | 16 | if (err) { |
17 | logger.warn('Cannot authenticate.', { err }) | ||
18 | |||
16 | return res.status(err.status) | 19 | return res.status(err.status) |
17 | .json({ | 20 | .json({ |
18 | error: 'Token is invalid.', | 21 | error: 'Token is invalid.', |
@@ -25,6 +28,12 @@ function authenticate (req: express.Request, res: express.Response, next: expres | |||
25 | }) | 28 | }) |
26 | } | 29 | } |
27 | 30 | ||
31 | function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
32 | if (req.header('authorization')) return authenticate(req, res, next) | ||
33 | |||
34 | return next() | ||
35 | } | ||
36 | |||
28 | function token (req: express.Request, res: express.Response, next: express.NextFunction) { | 37 | function token (req: express.Request, res: express.Response, next: express.NextFunction) { |
29 | return oAuthServer.token()(req, res, err => { | 38 | return oAuthServer.token()(req, res, err => { |
30 | if (err) { | 39 | if (err) { |
@@ -44,5 +53,6 @@ function token (req: express.Request, res: express.Response, next: express.NextF | |||
44 | 53 | ||
45 | export { | 54 | export { |
46 | authenticate, | 55 | authenticate, |
56 | optionalAuthenticate, | ||
47 | token | 57 | token |
48 | } | 58 | } |