]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
cef534ed | 2 | import { Socket } from 'socket.io' |
f43db2f4 | 3 | import { getAccessToken } from '@server/lib/auth/oauth-model' |
c0e8b12e | 4 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
f43db2f4 C |
5 | import { logger } from '../helpers/logger' |
6 | import { handleOAuthAuthenticate } from '../lib/auth/oauth' | |
9457bf88 | 7 | |
eccf70f0 | 8 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction, authenticateInQuery = false) { |
f43db2f4 C |
9 | handleOAuthAuthenticate(req, res, authenticateInQuery) |
10 | .then((token: any) => { | |
11 | res.locals.oauth = { token } | |
12 | res.locals.authenticated = true | |
13 | ||
14 | return next() | |
15 | }) | |
16 | .catch(err => { | |
0883b324 C |
17 | logger.warn('Cannot authenticate.', { err }) |
18 | ||
76148b27 RK |
19 | return res.fail({ |
20 | status: err.status, | |
21 | message: 'Token is invalid', | |
22 | type: err.name | |
23 | }) | |
f43db2f4 | 24 | }) |
0c1cbbfe C |
25 | } |
26 | ||
cef534ed | 27 | function authenticateSocket (socket: Socket, next: (err?: any) => void) { |
fce7fe04 | 28 | const accessToken = socket.handshake.query['accessToken'] |
cef534ed C |
29 | |
30 | logger.debug('Checking socket access token %s.', accessToken) | |
31 | ||
3acc5084 | 32 | if (!accessToken) return next(new Error('No access token provided')) |
fbd51e69 | 33 | if (typeof accessToken !== 'string') return next(new Error('Access token is invalid')) |
3acc5084 | 34 | |
cef534ed C |
35 | getAccessToken(accessToken) |
36 | .then(tokenDB => { | |
37 | const now = new Date() | |
38 | ||
39 | if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) { | |
40 | return next(new Error('Invalid access token.')) | |
41 | } | |
42 | ||
fbd51e69 | 43 | socket.handshake.auth.user = tokenDB.User |
cef534ed C |
44 | |
45 | return next() | |
46 | }) | |
a1587156 | 47 | .catch(err => logger.error('Cannot get access token.', { err })) |
cef534ed C |
48 | } |
49 | ||
eccf70f0 | 50 | function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) { |
ba5a8d89 | 51 | return new Promise<void>(resolve => { |
8d427346 | 52 | // Already authenticated? (or tried to) |
faa9d434 | 53 | if (res.locals.oauth?.token.User) return resolve() |
8d427346 | 54 | |
76148b27 RK |
55 | if (res.locals.authenticated === false) { |
56 | return res.fail({ | |
57 | status: HttpStatusCode.UNAUTHORIZED_401, | |
58 | message: 'Not authenticated' | |
59 | }) | |
60 | } | |
8d427346 | 61 | |
eccf70f0 | 62 | authenticate(req, res, () => resolve(), authenticateInQuery) |
8d427346 C |
63 | }) |
64 | } | |
65 | ||
0883b324 C |
66 | function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { |
67 | if (req.header('authorization')) return authenticate(req, res, next) | |
68 | ||
8d427346 C |
69 | res.locals.authenticated = false |
70 | ||
0883b324 C |
71 | return next() |
72 | } | |
73 | ||
9457bf88 C |
74 | // --------------------------------------------------------------------------- |
75 | ||
65fcc311 C |
76 | export { |
77 | authenticate, | |
cef534ed | 78 | authenticateSocket, |
8d427346 | 79 | authenticatePromiseIfNeeded, |
7fed6375 | 80 | optionalAuthenticate |
65fcc311 | 81 | } |