]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/oauth-clients.ts
Move ensureRegistrationEnabled to middlewares
[github/Chocobozzz/PeerTube.git] / server / controllers / api / oauth-clients.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
6606150c 2
69818c93 3import { CONFIG } from '../../initializers'
65fcc311 4import { logger } from '../../helpers'
e02643f3 5import { database as db } from '../../initializers/database'
0a381679 6import { OAuthClientLocal } from '../../../shared'
e861452f 7
0a381679 8const oauthClientsRouter = express.Router()
6606150c 9
0a381679 10oauthClientsRouter.get('/local', getLocalClient)
6606150c
C
11
12// Get the client credentials for the PeerTube front end
69818c93 13function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
14 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
15 const serverPort = CONFIG.WEBSERVER.PORT
3737bbaf 16 let headerHostShouldBe = serverHostname
6606150c
C
17 if (serverPort !== 80 && serverPort !== 443) {
18 headerHostShouldBe += ':' + serverPort
19 }
20
21 // Don't make this check if this is a test instance
22 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
b1233aa8 23 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
6606150c
C
24 return res.type('json').status(403).end()
25 }
26
feb4bdfd 27 db.OAuthClient.loadFirstClient(function (err, client) {
6606150c
C
28 if (err) return next(err)
29 if (!client) return next(new Error('No client available.'))
30
0a381679 31 const json: OAuthClientLocal = {
feb4bdfd 32 client_id: client.clientId,
6606150c 33 client_secret: client.clientSecret
154898b0
C
34 }
35 res.json(json)
6606150c
C
36 })
37}
38
39// ---------------------------------------------------------------------------
40
65fcc311 41export {
0a381679 42 oauthClientsRouter
65fcc311 43}