]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
d17c7b4e C |
2 | import { OAuthClientModel } from '@server/models/oauth/oauth-client' |
3 | import { HttpStatusCode, OAuthClientLocal } from '@shared/models' | |
da854ddd | 4 | import { logger } from '../../helpers/logger' |
6dd9de95 | 5 | import { CONFIG } from '../../initializers/config' |
1333ab1f | 6 | import { asyncMiddleware, openapiOperationDoc } from '../../middlewares' |
e861452f | 7 | |
0a381679 | 8 | const oauthClientsRouter = express.Router() |
6606150c | 9 | |
eb080476 | 10 | oauthClientsRouter.get('/local', |
1333ab1f | 11 | openapiOperationDoc({ operationId: 'getOAuthClient' }), |
eb080476 C |
12 | asyncMiddleware(getLocalClient) |
13 | ) | |
6606150c C |
14 | |
15 | // Get the client credentials for the PeerTube front end | |
eb080476 | 16 | async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) { |
65fcc311 C |
17 | const serverHostname = CONFIG.WEBSERVER.HOSTNAME |
18 | const serverPort = CONFIG.WEBSERVER.PORT | |
3737bbaf | 19 | let headerHostShouldBe = serverHostname |
6606150c C |
20 | if (serverPort !== 80 && serverPort !== 443) { |
21 | headerHostShouldBe += ':' + serverPort | |
22 | } | |
23 | ||
24 | // Don't make this check if this is a test instance | |
25 | if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { | |
b1233aa8 | 26 | logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe) |
76148b27 RK |
27 | return res.fail({ |
28 | status: HttpStatusCode.FORBIDDEN_403, | |
29 | message: `Getting client tokens for host ${req.get('host')} is forbidden` | |
30 | }) | |
6606150c C |
31 | } |
32 | ||
3fd3ab2d | 33 | const client = await OAuthClientModel.loadFirstClient() |
eb080476 C |
34 | if (!client) throw new Error('No client available.') |
35 | ||
36 | const json: OAuthClientLocal = { | |
37 | client_id: client.clientId, | |
38 | client_secret: client.clientSecret | |
39 | } | |
40 | return res.json(json) | |
6606150c C |
41 | } |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
65fcc311 | 45 | export { |
0a381679 | 46 | oauthClientsRouter |
65fcc311 | 47 | } |