]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/v1/users.js
Update to standard 7. Goodbye snake_case, I used to love you
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / users.js
CommitLineData
9457bf88
C
1'use strict'
2
23a5a916 3const config = require('config')
0c1cbbfe
C
4const express = require('express')
5const oAuth2 = require('../../../middlewares/oauth2')
9457bf88
C
6
7const middleware = require('../../../middlewares')
8const cacheMiddleware = middleware.cache
23a5a916 9const Users = require('../../../models/users')
9457bf88
C
10
11const router = express.Router()
12
23a5a916 13router.get('/client', cacheMiddleware.cache(false), getAngularClient)
0c1cbbfe 14router.post('/token', cacheMiddleware.cache(false), oAuth2.token, success)
9457bf88
C
15
16// ---------------------------------------------------------------------------
17
18module.exports = router
19
20// ---------------------------------------------------------------------------
21
23a5a916 22function getAngularClient (req, res, next) {
bc503c2a
C
23 const serverHost = config.get('webserver.host')
24 const serverPort = config.get('webserver.port')
25 let headerHostShouldBe = serverHost
26 if (serverPort !== 80 && serverPort !== 443) {
27 headerHostShouldBe += ':' + serverPort
23a5a916
C
28 }
29
bc503c2a 30 if (req.get('host') !== headerHostShouldBe) return res.type('json').status(403).end()
23a5a916
C
31
32 Users.getFirstClient(function (err, client) {
33 if (err) return next(err)
34 if (!client) return next(new Error('No client available.'))
35
36 res.json({
37 client_id: client._id,
38 client_secret: client.clientSecret
39 })
40 })
41}
42
9457bf88
C
43function success (req, res, next) {
44 res.end()
45}