aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/v1/users.js24
-rw-r--r--server/models/users.js5
2 files changed, 29 insertions, 0 deletions
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js
index f45b47077..1125b9faa 100644
--- a/server/controllers/api/v1/users.js
+++ b/server/controllers/api/v1/users.js
@@ -1,13 +1,16 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
3const express = require('express') 4const express = require('express')
4const oAuth2 = require('../../../middlewares/oauth2') 5const oAuth2 = require('../../../middlewares/oauth2')
5 6
6const middleware = require('../../../middlewares') 7const middleware = require('../../../middlewares')
7const cacheMiddleware = middleware.cache 8const cacheMiddleware = middleware.cache
9const Users = require('../../../models/users')
8 10
9const router = express.Router() 11const router = express.Router()
10 12
13router.get('/client', cacheMiddleware.cache(false), getAngularClient)
11router.post('/token', cacheMiddleware.cache(false), oAuth2.token, success) 14router.post('/token', cacheMiddleware.cache(false), oAuth2.token, success)
12 15
13// --------------------------------------------------------------------------- 16// ---------------------------------------------------------------------------
@@ -16,6 +19,27 @@ module.exports = router
16 19
17// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
18 21
22function getAngularClient (req, res, next) {
23 const server_host = config.get('webserver.host')
24 const server_port = config.get('webserver.port')
25 let header_host_should_be = server_host
26 if (server_port !== 80 && server_port !== 443) {
27 header_host_should_be += ':' + server_port
28 }
29
30 if (req.get('host') !== header_host_should_be) return res.type('json').status(403).end()
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
19function success (req, res, next) { 43function success (req, res, next) {
20 res.end() 44 res.end()
21} 45}
diff --git a/server/models/users.js b/server/models/users.js
index 046fe462d..a852bf25b 100644
--- a/server/models/users.js
+++ b/server/models/users.js
@@ -35,6 +35,7 @@ const Users = {
35 getAccessToken: getAccessToken, 35 getAccessToken: getAccessToken,
36 getClient: getClient, 36 getClient: getClient,
37 getClients: getClients, 37 getClients: getClients,
38 getFirstClient: getFirstClient,
38 getRefreshToken: getRefreshToken, 39 getRefreshToken: getRefreshToken,
39 getUser: getUser, 40 getUser: getUser,
40 getUsers: getUsers, 41 getUsers: getUsers,
@@ -64,6 +65,10 @@ function getAccessToken (bearerToken, callback) {
64 return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user') 65 return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user')
65} 66}
66 67
68function getFirstClient (callback) {
69 return OAuthClientsDB.findOne({}, callback)
70}
71
67function getClient (clientId, clientSecret) { 72function getClient (clientId, clientSecret) {
68 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') 73 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
69 74