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