]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/oauth-clients.ts
3dcc023e6e20ba80ffc099e499cca56ac96feb7d
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
1 import * as express from 'express'
2 import { OAuthClientLocal } from '../../../shared'
3 import { logger } from '../../helpers/logger'
4 import { CONFIG } from '../../initializers'
5 import { asyncMiddleware } from '../../middlewares'
6 import { OAuthClientModel } from '../../models/oauth/oauth-client'
7
8 const oauthClientsRouter = express.Router()
9
10 oauthClientsRouter.get('/local',
11 asyncMiddleware(getLocalClient)
12 )
13
14 // Get the client credentials for the PeerTube front end
15 async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
16 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
17 const serverPort = CONFIG.WEBSERVER.PORT
18 let headerHostShouldBe = serverHostname
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 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
26 return res.type('json').status(403).end()
27 }
28
29 const client = await OAuthClientModel.loadFirstClient()
30 if (!client) throw new Error('No client available.')
31
32 const json: OAuthClientLocal = {
33 client_id: client.clientId,
34 client_secret: client.clientSecret
35 }
36 return res.json(json)
37 }
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 oauthClientsRouter
43 }