]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/oauth-clients.ts
Move zxx to its own group in select-languages component (#4664)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
CommitLineData
41fb13c3 1import express from 'express'
da854ddd 2import { OAuthClientLocal } from '../../../shared'
c0e8b12e 3import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
da854ddd 4import { logger } from '../../helpers/logger'
6dd9de95 5import { CONFIG } from '../../initializers/config'
1333ab1f 6import { asyncMiddleware, openapiOperationDoc } from '../../middlewares'
3fd3ab2d 7import { OAuthClientModel } from '../../models/oauth/oauth-client'
e861452f 8
0a381679 9const oauthClientsRouter = express.Router()
6606150c 10
eb080476 11oauthClientsRouter.get('/local',
1333ab1f 12 openapiOperationDoc({ operationId: 'getOAuthClient' }),
eb080476
C
13 asyncMiddleware(getLocalClient)
14)
6606150c
C
15
16// Get the client credentials for the PeerTube front end
eb080476 17async function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
18 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
19 const serverPort = CONFIG.WEBSERVER.PORT
3737bbaf 20 let headerHostShouldBe = serverHostname
6606150c
C
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 (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
b1233aa8 27 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
76148b27
RK
28 return res.fail({
29 status: HttpStatusCode.FORBIDDEN_403,
30 message: `Getting client tokens for host ${req.get('host')} is forbidden`
31 })
6606150c
C
32 }
33
3fd3ab2d 34 const client = await OAuthClientModel.loadFirstClient()
eb080476
C
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)
6606150c
C
42}
43
44// ---------------------------------------------------------------------------
45
65fcc311 46export {
0a381679 47 oauthClientsRouter
65fcc311 48}