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