3 const express = require('express')
5 const constants = require('../../initializers/constants')
6 const db = require('../../initializers/database')
7 const logger = require('../../helpers/logger')
9 const router = express.Router()
11 router.get('/local', getLocalClient)
13 // Get the client credentials for the PeerTube front end
14 function getLocalClient (req, res, next) {
15 const serverHostname = constants.CONFIG.WEBSERVER.HOSTNAME
16 const serverPort = constants.CONFIG.WEBSERVER.PORT
17 let headerHostShouldBe = serverHostname
18 if (serverPort !== 80 && serverPort !== 443) {
19 headerHostShouldBe += ':' + serverPort
22 // Don't make this check if this is a test instance
23 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
24 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
25 return res.type('json').status(403).end()
28 db.OAuthClient.loadFirstClient(function (err, client) {
29 if (err) return next(err)
30 if (!client) return next(new Error('No client available.'))
33 client_id: client.clientId,
34 client_secret: client.clientSecret
39 // ---------------------------------------------------------------------------
41 module.exports = router