aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/oauth-clients.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/oauth-clients.ts')
-rw-r--r--server/controllers/api/oauth-clients.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/server/controllers/api/oauth-clients.ts b/server/controllers/api/oauth-clients.ts
new file mode 100644
index 000000000..b9bc0534f
--- /dev/null
+++ b/server/controllers/api/oauth-clients.ts
@@ -0,0 +1,43 @@
1import * as express from 'express'
2
3import { CONFIG } from '../../initializers'
4import { logger } from '../../helpers'
5import { database as db } from '../../initializers/database'
6import { OAuthClientLocal } from '../../../shared'
7
8const oauthClientsRouter = express.Router()
9
10oauthClientsRouter.get('/local', getLocalClient)
11
12// Get the client credentials for the PeerTube front end
13function getLocalClient (req: express.Request, res: express.Response, next: express.NextFunction) {
14 const serverHostname = CONFIG.WEBSERVER.HOSTNAME
15 const serverPort = CONFIG.WEBSERVER.PORT
16 let headerHostShouldBe = serverHostname
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) {
23 logger.info('Getting client tokens for host %s is forbidden (expected %s).', req.get('host'), headerHostShouldBe)
24 return res.type('json').status(403).end()
25 }
26
27 db.OAuthClient.loadFirstClient(function (err, client) {
28 if (err) return next(err)
29 if (!client) return next(new Error('No client available.'))
30
31 const json: OAuthClientLocal = {
32 client_id: client.clientId,
33 client_secret: client.clientSecret
34 }
35 res.json(json)
36 })
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 oauthClientsRouter
43}