1 import * as express from 'express'
3 import { CONFIG } from '../../initializers'
4 import { logger } from '../../helpers'
5 import { database as db } from '../../initializers/database'
6 import { ClientLocal } from '../../../shared'
8 const clientsRouter = express.Router()
10 clientsRouter.get('/local', getLocalClient)
12 // Get the client credentials for the PeerTube front end
13 function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
14 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
15 const serverPort = CONFIG.WEBSERVER.PORT
16 let headerHostShouldBe = serverHostname
17 if (serverPort !== 80 && serverPort !== 443) {
18 headerHostShouldBe += ':' + serverPort
21 // Don't make this check if this is a test instance
22 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
23 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
24 return res.type('json').status(403).end()
27 db.OAuthClient.loadFirstClient(function (err, client) {
28 if (err) return next(err)
29 if (!client) return next(new Error('No client available.'))
31 const json: ClientLocal = {
32 client_id: client.clientId,
33 client_secret: client.clientSecret
39 // ---------------------------------------------------------------------------