1 import * as express from 'express'
2 import { OAuthClientLocal } from '../../../shared'
3 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 import { logger } from '../../helpers/logger'
5 import { CONFIG } from '../../initializers/config'
6 import { asyncMiddleware } from '../../middlewares'
7 import { OAuthClientModel } from '../../models/oauth/oauth-client'
9 const oauthClientsRouter = express.Router()
11 oauthClientsRouter.get('/local',
12 asyncMiddleware(getLocalClient)
15 // Get the client credentials for the PeerTube front end
16 async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
17 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
18 const serverPort = CONFIG.WEBSERVER.PORT
19 let headerHostShouldBe = serverHostname
20 if (serverPort !== 80 && serverPort !== 443) {
21 headerHostShouldBe += ':' + serverPort
24 // Don't make this check if this is a test instance
25 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
26 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
28 status: HttpStatusCode.FORBIDDEN_403,
29 message: `Getting client tokens for host ${req.get('host')} is forbidden`
33 const client = await OAuthClientModel.loadFirstClient()
34 if (!client) throw new Error('No client available.')
36 const json: OAuthClientLocal = {
37 client_id: client.clientId,
38 client_secret: client.clientSecret
43 // ---------------------------------------------------------------------------