2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
import { CONFIG } from '../../initializers';
|
|
|
|
import { logger } from '../../helpers'
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2016-08-19 14:34:51 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
const clientsRouter = express.Router()
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
clientsRouter.get('/local', getLocalClient)
|
2016-08-05 09:09:39 -05:00
|
|
|
|
|
|
|
// Get the client credentials for the PeerTube front end
|
|
|
|
function getLocalClient (req, res, next) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const serverHostname = CONFIG.WEBSERVER.HOSTNAME
|
|
|
|
const serverPort = CONFIG.WEBSERVER.PORT
|
2016-10-23 12:41:17 -05:00
|
|
|
let headerHostShouldBe = serverHostname
|
2016-08-05 09:09:39 -05: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 13:14:33 -05:00
|
|
|
logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
|
2016-08-05 09:09:39 -05:00
|
|
|
return res.type('json').status(403).end()
|
|
|
|
}
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
db.OAuthClient.loadFirstClient(function (err, client) {
|
2016-08-05 09:09:39 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
if (!client) return next(new Error('No client available.'))
|
|
|
|
|
|
|
|
res.json({
|
2016-12-11 14:50:51 -06:00
|
|
|
client_id: client.clientId,
|
2016-08-05 09:09:39 -05:00
|
|
|
client_secret: client.clientSecret
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
clientsRouter
|
|
|
|
}
|