diff options
Diffstat (limited to 'server/middlewares/auth.ts')
-rw-r--r-- | server/middlewares/auth.ts | 113 |
1 files changed, 0 insertions, 113 deletions
diff --git a/server/middlewares/auth.ts b/server/middlewares/auth.ts deleted file mode 100644 index 39a7b2998..000000000 --- a/server/middlewares/auth.ts +++ /dev/null | |||
@@ -1,113 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { Socket } from 'socket.io' | ||
3 | import { getAccessToken } from '@server/lib/auth/oauth-model' | ||
4 | import { RunnerModel } from '@server/models/runner/runner' | ||
5 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' | ||
6 | import { logger } from '../helpers/logger' | ||
7 | import { handleOAuthAuthenticate } from '../lib/auth/oauth' | ||
8 | import { ServerErrorCode } from '@shared/models' | ||
9 | |||
10 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
11 | handleOAuthAuthenticate(req, res) | ||
12 | .then((token: any) => { | ||
13 | res.locals.oauth = { token } | ||
14 | res.locals.authenticated = true | ||
15 | |||
16 | return next() | ||
17 | }) | ||
18 | .catch(err => { | ||
19 | logger.info('Cannot authenticate.', { err }) | ||
20 | |||
21 | return res.fail({ | ||
22 | status: err.status, | ||
23 | message: 'Token is invalid', | ||
24 | type: err.name | ||
25 | }) | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | function authenticateSocket (socket: Socket, next: (err?: any) => void) { | ||
30 | const accessToken = socket.handshake.query['accessToken'] | ||
31 | |||
32 | logger.debug('Checking access token in runner.') | ||
33 | |||
34 | if (!accessToken) return next(new Error('No access token provided')) | ||
35 | if (typeof accessToken !== 'string') return next(new Error('Access token is invalid')) | ||
36 | |||
37 | getAccessToken(accessToken) | ||
38 | .then(tokenDB => { | ||
39 | const now = new Date() | ||
40 | |||
41 | if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) { | ||
42 | return next(new Error('Invalid access token.')) | ||
43 | } | ||
44 | |||
45 | socket.handshake.auth.user = tokenDB.User | ||
46 | |||
47 | return next() | ||
48 | }) | ||
49 | .catch(err => logger.error('Cannot get access token.', { err })) | ||
50 | } | ||
51 | |||
52 | function authenticatePromise (options: { | ||
53 | req: express.Request | ||
54 | res: express.Response | ||
55 | errorMessage?: string | ||
56 | errorStatus?: HttpStatusCode | ||
57 | errorType?: ServerErrorCode | ||
58 | }) { | ||
59 | const { req, res, errorMessage = 'Not authenticated', errorStatus = HttpStatusCode.UNAUTHORIZED_401, errorType } = options | ||
60 | return new Promise<void>(resolve => { | ||
61 | // Already authenticated? (or tried to) | ||
62 | if (res.locals.oauth?.token.User) return resolve() | ||
63 | |||
64 | if (res.locals.authenticated === false) { | ||
65 | return res.fail({ | ||
66 | status: errorStatus, | ||
67 | type: errorType, | ||
68 | message: errorMessage | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | authenticate(req, res, () => resolve()) | ||
73 | }) | ||
74 | } | ||
75 | |||
76 | function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
77 | if (req.header('authorization')) return authenticate(req, res, next) | ||
78 | |||
79 | res.locals.authenticated = false | ||
80 | |||
81 | return next() | ||
82 | } | ||
83 | |||
84 | // --------------------------------------------------------------------------- | ||
85 | |||
86 | function authenticateRunnerSocket (socket: Socket, next: (err?: any) => void) { | ||
87 | const runnerToken = socket.handshake.auth['runnerToken'] | ||
88 | |||
89 | logger.debug('Checking runner token in socket.') | ||
90 | |||
91 | if (!runnerToken) return next(new Error('No runner token provided')) | ||
92 | if (typeof runnerToken !== 'string') return next(new Error('Runner token is invalid')) | ||
93 | |||
94 | RunnerModel.loadByToken(runnerToken) | ||
95 | .then(runner => { | ||
96 | if (!runner) return next(new Error('Invalid runner token.')) | ||
97 | |||
98 | socket.handshake.auth.runner = runner | ||
99 | |||
100 | return next() | ||
101 | }) | ||
102 | .catch(err => logger.error('Cannot get runner token.', { err })) | ||
103 | } | ||
104 | |||
105 | // --------------------------------------------------------------------------- | ||
106 | |||
107 | export { | ||
108 | authenticate, | ||
109 | authenticateSocket, | ||
110 | authenticatePromise, | ||
111 | optionalAuthenticate, | ||
112 | authenticateRunnerSocket | ||
113 | } | ||