From 6606150c49f587bc7eb0ecec4263ce7fbb18bf15 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Aug 2016 16:09:39 +0200 Subject: Server: move clients in its own file --- server/controllers/api/v1/clients.js | 40 ++++++++++++++++++++++++++++++++++++ server/controllers/api/v1/index.js | 2 ++ server/controllers/api/v1/users.js | 28 +------------------------ 3 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 server/controllers/api/v1/clients.js (limited to 'server/controllers/api') diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js new file mode 100644 index 000000000..0d222634b --- /dev/null +++ b/server/controllers/api/v1/clients.js @@ -0,0 +1,40 @@ +'use strict' + +const config = require('config') +const express = require('express') +const mongoose = require('mongoose') + +const Client = mongoose.model('OAuthClient') + +const router = express.Router() + +router.get('/local', getLocalClient) + +// Get the client credentials for the PeerTube front end +function getLocalClient (req, res, next) { + const serverHost = config.get('webserver.host') + const serverPort = config.get('webserver.port') + let headerHostShouldBe = serverHost + if (serverPort !== 80 && serverPort !== 443) { + headerHostShouldBe += ':' + serverPort + } + + // Don't make this check if this is a test instance + if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { + return res.type('json').status(403).end() + } + + Client.loadFirstClient(function (err, client) { + if (err) return next(err) + if (!client) return next(new Error('No client available.')) + + res.json({ + client_id: client._id, + client_secret: client.clientSecret + }) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = router diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index e0c29a8a2..af41bc280 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js @@ -4,11 +4,13 @@ const express = require('express') const router = express.Router() +const clientsController = require('./clients') const podsController = require('./pods') const remoteController = require('./remote') const usersController = require('./users') const videosController = require('./videos') +router.use('/clients', clientsController) router.use('/pods', podsController) router.use('/remote', remoteController) router.use('/users', usersController) diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index e084974ce..fdbcc3ff5 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -1,7 +1,6 @@ 'use strict' const each = require('async/each') -const config = require('config') const express = require('express') const mongoose = require('mongoose') const waterfall = require('async/waterfall') @@ -14,7 +13,6 @@ const admin = middlewares.admin const oAuth = middlewares.oauth const validatorsUsers = middlewares.validators.users -const Client = mongoose.model('OAuthClient') const User = mongoose.model('User') const Video = mongoose.model('Video') @@ -41,7 +39,7 @@ router.delete('/:username', validatorsUsers.usersRemove, removeUser ) -router.get('/client', getAngularClient) + router.post('/token', oAuth.token, success) // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route @@ -65,30 +63,6 @@ function createUser (req, res, next) { }) } -function getAngularClient (req, res, next) { - const serverHost = config.get('webserver.host') - const serverPort = config.get('webserver.port') - let headerHostShouldBe = serverHost - if (serverPort !== 80 && serverPort !== 443) { - headerHostShouldBe += ':' + serverPort - } - - // Don't make this check if this is a test instance - if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { - return res.type('json').status(403).end() - } - - Client.loadFirstClient(function (err, client) { - if (err) return next(err) - if (!client) return next(new Error('No client available.')) - - res.json({ - client_id: client._id, - client_secret: client.clientSecret - }) - }) -} - function listUsers (req, res, next) { User.list(function (err, usersList) { if (err) return next(err) -- cgit v1.2.3