]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/oauth-clients.ts
Federate video views
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
6606150c 2
69818c93 3import { CONFIG } from '../../initializers'
65fcc311 4import { logger } from '../../helpers'
eb080476 5import { asyncMiddleware } from '../../middlewares'
e02643f3 6import { database as db } from '../../initializers/database'
0a381679 7import { OAuthClientLocal } from '../../../shared'
e861452f 8
0a381679 9const oauthClientsRouter = express.Router()
6606150c 10
eb080476
C
11oauthClientsRouter.get('/local',
12 asyncMiddleware(getLocalClient)
13)
6606150c
C
14
15// Get the client credentials for the PeerTube front end
eb080476 16async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
17 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
18 const serverPort = 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
eb080476
C
30 const client = await db.OAuthClient.loadFirstClient()
31 if (!client) throw new Error('No client available.')
32
33 const json: OAuthClientLocal = {
34 client_id: client.clientId,
35 client_secret: client.clientSecret
36 }
37 return res.json(json)
6606150c
C
38}
39
40// ---------------------------------------------------------------------------
41
65fcc311 42export {
0a381679 43 oauthClientsRouter
65fcc311 44}