]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/oauth-clients.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
da854ddd 2import { OAuthClientLocal } from '../../../shared'
2d53be02 3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
da854ddd 4import { logger } from '../../helpers/logger'
6dd9de95 5import { CONFIG } from '../../initializers/config'
eb080476 6import { asyncMiddleware } from '../../middlewares'
3fd3ab2d 7import { OAuthClientModel } from '../../models/oauth/oauth-client'
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)
2d53be02 27 return res.type('json').status(HttpStatusCode.FORBIDDEN_403).end()
6606150c
C
28 }
29
3fd3ab2d 30 const client = await OAuthClientModel.loadFirstClient()
eb080476
C
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}