diff options
Diffstat (limited to 'server/middlewares/oauth.ts')
-rw-r--r-- | server/middlewares/oauth.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 5233b66bd..8c1df2c3e 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts | |||
@@ -28,9 +28,24 @@ function authenticate (req: express.Request, res: express.Response, next: expres | |||
28 | }) | 28 | }) |
29 | } | 29 | } |
30 | 30 | ||
31 | function authenticatePromiseIfNeeded (req: express.Request, res: express.Response) { | ||
32 | return new Promise(resolve => { | ||
33 | // Already authenticated? (or tried to) | ||
34 | if (res.locals.oauth && res.locals.oauth.token.User) return resolve() | ||
35 | |||
36 | if (res.locals.authenticated === false) return res.sendStatus(401) | ||
37 | |||
38 | authenticate(req, res, () => { | ||
39 | return resolve() | ||
40 | }) | ||
41 | }) | ||
42 | } | ||
43 | |||
31 | function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { | 44 | function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { |
32 | if (req.header('authorization')) return authenticate(req, res, next) | 45 | if (req.header('authorization')) return authenticate(req, res, next) |
33 | 46 | ||
47 | res.locals.authenticated = false | ||
48 | |||
34 | return next() | 49 | return next() |
35 | } | 50 | } |
36 | 51 | ||
@@ -53,6 +68,7 @@ function token (req: express.Request, res: express.Response, next: express.NextF | |||
53 | 68 | ||
54 | export { | 69 | export { |
55 | authenticate, | 70 | authenticate, |
71 | authenticatePromiseIfNeeded, | ||
56 | optionalAuthenticate, | 72 | optionalAuthenticate, |
57 | token | 73 | token |
58 | } | 74 | } |