]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | ||
3 | import { CONFIG } from '../../initializers' | |
4 | import { logger } from '../../helpers' | |
5 | import { asyncMiddleware } from '../../middlewares' | |
6 | import { database as db } from '../../initializers/database' | |
7 | import { OAuthClientLocal } from '../../../shared' | |
8 | ||
9 | const oauthClientsRouter = express.Router() | |
10 | ||
11 | oauthClientsRouter.get('/local', | |
12 | asyncMiddleware(getLocalClient) | |
13 | ) | |
14 | ||
15 | // Get the client credentials for the PeerTube front end | |
16 | async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) { | |
17 | const serverHostname = CONFIG.WEBSERVER.HOSTNAME | |
18 | const serverPort = CONFIG.WEBSERVER.PORT | |
19 | let headerHostShouldBe = serverHostname | |
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) { | |
26 | logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe) | |
27 | return res.type('json').status(403).end() | |
28 | } | |
29 | ||
30 | const client = await db.OAuthClient.loadFirstClient() | |
31 | if (!client) throw new Error('No client available.') | |
32 | ||
33 | const json: OAuthClientLocal = { | |
34 | client_id: client.clientId, | |
35 | client_secret: client.clientSecret | |
36 | } | |
37 | return res.json(json) | |
38 | } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
42 | export { | |
43 | oauthClientsRouter | |
44 | } |