PeerTube/server/controllers/api/clients.ts

44 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2016-08-05 16:09:39 +02:00
2017-06-10 22:15:25 +02:00
import { CONFIG } from '../../initializers'
2017-05-15 22:22:03 +02:00
import { logger } from '../../helpers'
2017-05-22 20:58:25 +02:00
import { database as db } from '../../initializers/database'
2017-06-17 11:28:11 +02:00
import { ClientLocal } from '../../../shared'
2016-08-19 21:34:51 +02:00
2017-05-15 22:22:03 +02:00
const clientsRouter = express.Router()
2016-08-05 16:09:39 +02:00
2017-05-15 22:22:03 +02:00
clientsRouter.get('/local', getLocalClient)
2016-08-05 16:09:39 +02:00
// Get the client credentials for the PeerTube front end
2017-06-10 22:15:25 +02:00
function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-15 22:22:03 +02:00
const serverHostname = CONFIG.WEBSERVER.HOSTNAME
const serverPort = CONFIG.WEBSERVER.PORT
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) {
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.'))
2017-06-17 11:28:11 +02:00
const json: ClientLocal = {
2016-12-11 21:50:51 +01:00
client_id: client.clientId,
2016-08-05 16:09:39 +02:00
client_secret: client.clientSecret
2017-06-17 11:28:11 +02:00
}
res.json(json)
2016-08-05 16:09:39 +02:00
})
}
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
clientsRouter
}