diff options
Diffstat (limited to 'server/middlewares/auth.ts')
-rw-r--r-- | server/middlewares/auth.ts | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/server/middlewares/auth.ts b/server/middlewares/auth.ts index e6025c8ce..0eefa2a8e 100644 --- a/server/middlewares/auth.ts +++ b/server/middlewares/auth.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { Socket } from 'socket.io' | 2 | import { Socket } from 'socket.io' |
3 | import { getAccessToken } from '@server/lib/auth/oauth-model' | 3 | import { getAccessToken } from '@server/lib/auth/oauth-model' |
4 | import { RunnerModel } from '@server/models/runner/runner' | ||
4 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' | 5 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
5 | import { logger } from '../helpers/logger' | 6 | import { logger } from '../helpers/logger' |
6 | import { handleOAuthAuthenticate } from '../lib/auth/oauth' | 7 | import { handleOAuthAuthenticate } from '../lib/auth/oauth' |
@@ -27,7 +28,7 @@ function authenticate (req: express.Request, res: express.Response, next: expres | |||
27 | function authenticateSocket (socket: Socket, next: (err?: any) => void) { | 28 | function authenticateSocket (socket: Socket, next: (err?: any) => void) { |
28 | const accessToken = socket.handshake.query['accessToken'] | 29 | const accessToken = socket.handshake.query['accessToken'] |
29 | 30 | ||
30 | logger.debug('Checking socket access token %s.', accessToken) | 31 | logger.debug('Checking access token in runner.') |
31 | 32 | ||
32 | if (!accessToken) return next(new Error('No access token provided')) | 33 | if (!accessToken) return next(new Error('No access token provided')) |
33 | if (typeof accessToken !== 'string') return next(new Error('Access token is invalid')) | 34 | if (typeof accessToken !== 'string') return next(new Error('Access token is invalid')) |
@@ -73,9 +74,31 @@ function optionalAuthenticate (req: express.Request, res: express.Response, next | |||
73 | 74 | ||
74 | // --------------------------------------------------------------------------- | 75 | // --------------------------------------------------------------------------- |
75 | 76 | ||
77 | function authenticateRunnerSocket (socket: Socket, next: (err?: any) => void) { | ||
78 | const runnerToken = socket.handshake.auth['runnerToken'] | ||
79 | |||
80 | logger.debug('Checking runner token in socket.') | ||
81 | |||
82 | if (!runnerToken) return next(new Error('No runner token provided')) | ||
83 | if (typeof runnerToken !== 'string') return next(new Error('Runner token is invalid')) | ||
84 | |||
85 | RunnerModel.loadByToken(runnerToken) | ||
86 | .then(runner => { | ||
87 | if (!runner) return next(new Error('Invalid runner token.')) | ||
88 | |||
89 | socket.handshake.auth.runner = runner | ||
90 | |||
91 | return next() | ||
92 | }) | ||
93 | .catch(err => logger.error('Cannot get runner token.', { err })) | ||
94 | } | ||
95 | |||
96 | // --------------------------------------------------------------------------- | ||
97 | |||
76 | export { | 98 | export { |
77 | authenticate, | 99 | authenticate, |
78 | authenticateSocket, | 100 | authenticateSocket, |
79 | authenticatePromise, | 101 | authenticatePromise, |
80 | optionalAuthenticate | 102 | optionalAuthenticate, |
103 | authenticateRunnerSocket | ||
81 | } | 104 | } |