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