]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/clients.js
Server: host -> hostname (host = hostname + port)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / clients.js
CommitLineData
6606150c
C
1'use strict'
2
6606150c
C
3const express = require('express')
4const mongoose = require('mongoose')
5
f253b1c1 6const constants = require('../../initializers/constants')
e861452f 7
6606150c
C
8const Client = mongoose.model('OAuthClient')
9
10const router = express.Router()
11
12router.get('/local', getLocalClient)
13
14// Get the client credentials for the PeerTube front end
15function getLocalClient (req, res, next) {
3737bbaf 16 const serverHostname = constants.CONFIG.WEBSERVER.HOSTNAME
e861452f 17 const serverPort = constants.CONFIG.WEBSERVER.PORT
3737bbaf 18 let headerHostShouldBe = serverHostname
6606150c
C
19 if (serverPort !== 80 && serverPort !== 443) {
20 headerHostShouldBe += ':' + serverPort
21 }
22
23 // Don't make this check if this is a test instance
24 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
25 return res.type('json').status(403).end()
26 }
27
28 Client.loadFirstClient(function (err, client) {
29 if (err) return next(err)
30 if (!client) return next(new Error('No client available.'))
31
32 res.json({
33 client_id: client._id,
34 client_secret: client.clientSecret
35 })
36 })
37}
38
39// ---------------------------------------------------------------------------
40
41module.exports = router