2016-08-05 16:09:39 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const express = require('express')
|
|
|
|
|
2016-10-21 12:16:28 +02:00
|
|
|
const constants = require('../../initializers/constants')
|
2016-12-11 21:50:51 +01:00
|
|
|
const db = require('../../initializers/database')
|
2016-11-01 19:14:33 +01:00
|
|
|
const logger = require('../../helpers/logger')
|
2016-08-19 21:34:51 +02:00
|
|
|
|
2016-08-05 16:09:39 +02:00
|
|
|
const router = express.Router()
|
|
|
|
|
|
|
|
router.get('/local', getLocalClient)
|
|
|
|
|
|
|
|
// Get the client credentials for the PeerTube front end
|
|
|
|
function getLocalClient (req, res, next) {
|
2016-10-23 19:41:17 +02:00
|
|
|
const serverHostname = constants.CONFIG.WEBSERVER.HOSTNAME
|
2016-08-19 21:34:51 +02:00
|
|
|
const serverPort = constants.CONFIG.WEBSERVER.PORT
|
2016-10-23 19:41:17 +02:00
|
|
|
let headerHostShouldBe = serverHostname
|
2016-08-05 16:09:39 +02:00
|
|
|
if (serverPort !== 80 && serverPort !== 443) {
|
|
|
|
headerHostShouldBe += ':' + serverPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't make this check if this is a test instance
|
|
|
|
if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
|
2016-11-01 19:14:33 +01:00
|
|
|
logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
|
2016-08-05 16:09:39 +02:00
|
|
|
return res.type('json').status(403).end()
|
|
|
|
}
|
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
db.OAuthClient.loadFirstClient(function (err, client) {
|
2016-08-05 16:09:39 +02:00
|
|
|
if (err) return next(err)
|
|
|
|
if (!client) return next(new Error('No client available.'))
|
|
|
|
|
|
|
|
res.json({
|
2016-12-11 21:50:51 +01:00
|
|
|
client_id: client.clientId,
|
2016-08-05 16:09:39 +02:00
|
|
|
client_secret: client.clientSecret
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = router
|