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