Add video channels
[oweals/peertube.git] / server / controllers / api / oauth-clients.ts
1 import * as express from 'express'
2
3 import { CONFIG } from '../../initializers'
4 import { logger } from '../../helpers'
5 import { database as db } from '../../initializers/database'
6 import { OAuthClientLocal } from '../../../shared'
7
8 const oauthClientsRouter = express.Router()
9
10 oauthClientsRouter.get('/local', getLocalClient)
11
12 // Get the client credentials for the PeerTube front end
13 function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
14   const serverHostname = CONFIG.WEBSERVER.HOSTNAME
15   const serverPort = CONFIG.WEBSERVER.PORT
16   let headerHostShouldBe = serverHostname
17   if (serverPort !== 80 && serverPort !== 443) {
18     headerHostShouldBe += ':' + serverPort
19   }
20
21   // Don't make this check if this is a test instance
22   if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
23     logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
24     return res.type('json').status(403).end()
25   }
26
27   db.OAuthClient.loadFirstClient()
28     .then(client => {
29       if (!client) throw new Error('No client available.')
30
31       const json: OAuthClientLocal = {
32         client_id: client.clientId,
33         client_secret: client.clientSecret
34       }
35       res.json(json)
36     })
37     .catch(err => next(err))
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43   oauthClientsRouter
44 }