]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/oauth-clients.ts
Translated using Weblate (Vietnamese)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
... / ...
CommitLineData
1import express from 'express'
2import { OAuthClientModel } from '@server/models/oauth/oauth-client'
3import { HttpStatusCode, OAuthClientLocal } from '@shared/models'
4import { logger } from '../../helpers/logger'
5import { CONFIG } from '../../initializers/config'
6import { asyncMiddleware, openapiOperationDoc } from '../../middlewares'
7
8const oauthClientsRouter = express.Router()
9
10oauthClientsRouter.get('/local',
11 openapiOperationDoc({ operationId: 'getOAuthClient' }),
12 asyncMiddleware(getLocalClient)
13)
14
15// Get the client credentials for the PeerTube front end
16async 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
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) {
26 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
27 return res.fail({
28 status: HttpStatusCode.FORBIDDEN_403,
29 message: `Getting client tokens for host ${req.get('host')} is forbidden`
30 })
31 }
32
33 const client = await OAuthClientModel.loadFirstClient()
34 if (!client) throw new Error('No client available.')
35
36 const json: OAuthClientLocal = {
37 client_id: client.clientId,
38 client_secret: client.clientSecret
39 }
40 return res.json(json)
41}
42
43// ---------------------------------------------------------------------------
44
45export {
46 oauthClientsRouter
47}