]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/oauth-clients.ts
eb7942fd68247ce9f093ecac92de189fcce54c2c
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
1 import express from 'express'
2 import { isTestOrDevInstance } from '@server/helpers/core-utils'
3 import { OAuthClientModel } from '@server/models/oauth/oauth-client'
4 import { HttpStatusCode, OAuthClientLocal } from '@shared/models'
5 import { logger } from '../../helpers/logger'
6 import { CONFIG } from '../../initializers/config'
7 import { asyncMiddleware, openapiOperationDoc } from '../../middlewares'
8
9 const oauthClientsRouter = express.Router()
10
11 oauthClientsRouter.get('/local',
12 openapiOperationDoc({ operationId: 'getOAuthClient' }),
13 asyncMiddleware(getLocalClient)
14 )
15
16 // Get the client credentials for the PeerTube front end
17 async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
18 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
19 const serverPort = CONFIG.WEBSERVER.PORT
20 let headerHostShouldBe = serverHostname
21 if (serverPort !== 80 && serverPort !== 443) {
22 headerHostShouldBe += ':' + serverPort
23 }
24
25 // Don't make this check if this is a test instance
26 if (!isTestOrDevInstance() && req.get('host') !== headerHostShouldBe) {
27 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
28 return res.fail({
29 status: HttpStatusCode.FORBIDDEN_403,
30 message: `Getting client tokens for host ${req.get('host')} is forbidden`
31 })
32 }
33
34 const client = await OAuthClientModel.loadFirstClient()
35 if (!client) throw new Error('No client available.')
36
37 const json: OAuthClientLocal = {
38 client_id: client.clientId,
39 client_secret: client.clientSecret
40 }
41 return res.json(json)
42 }
43
44 // ---------------------------------------------------------------------------
45
46 export {
47 oauthClientsRouter
48 }