First version with PostgreSQL
[oweals/peertube.git] / server / controllers / api / clients.js
1 'use strict'
2
3 const express = require('express')
4
5 const constants = require('../../initializers/constants')
6 const db = require('../../initializers/database')
7 const logger = require('../../helpers/logger')
8
9 const router = express.Router()
10
11 router.get('/local', getLocalClient)
12
13 // Get the client credentials for the PeerTube front end
14 function getLocalClient (req, res, next) {
15   const serverHostname = constants.CONFIG.WEBSERVER.HOSTNAME
16   const serverPort = constants.CONFIG.WEBSERVER.PORT
17   let headerHostShouldBe = serverHostname
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) {
24     logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
25     return res.type('json').status(403).end()
26   }
27
28   db.OAuthClient.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.clientId,
34       client_secret: client.clientSecret
35     })
36   })
37 }
38
39 // ---------------------------------------------------------------------------
40
41 module.exports = router