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