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