RIFT-14989: Login fixes for Accounts and RO Config pages
[osm/UI.git] / skyquake / framework / core / modules / routes / configuration.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21 * Node configuration routes module.
22 * Provides a RESTful API to provide configuration
23 * details such as api_server.
24 * @module framework/core/modules/routes/configuration
25 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
26 */
27
28 var cors = require('cors');
29 var bodyParser = require('body-parser');
30 var configurationAPI = require('../api/configuration');
31 var Router = require('express').Router();
32 var utils = require('../../api_utils/utils');
33 var CONSTANTS = require('../../api_utils/constants.js');
34 var request = require('request');
35 var _ = require('lodash');
36
37 Router.use(bodyParser.json());
38 Router.use(cors());
39 Router.use(bodyParser.urlencoded({
40 extended: true
41 }));
42
43 Router.put('/server-configuration', cors(), function(req, res) {
44 configurationAPI.update(req).then(function(data) {
45 utils.sendSuccessResponse(data, res);
46 }, function(error) {
47 utils.sendErrorResponse(error, res);
48 });
49 });
50
51 Router.get('/server-configuration', cors(), function(req, res) {
52 configurationAPI.get(req).then(function(data) {
53 utils.sendSuccessResponse(data, res);
54 }, function(error) {
55 utils.sendErrorResponse(error, res);
56 });
57 });
58
59 Router.get('/check-auth', function(req, res) {
60 console.log('testing auth')
61 var api_server = req.query["api_server"];
62 var uri = utils.confdPort(api_server) + '/api/config/';
63
64 checkAuth(uri, req).then(function(data) {
65 utils.sendSuccessResponse(data, res);
66 }, function(error) {
67 utils.sendErrorResponse(error, res);
68 });
69 });
70
71 function checkAuth(uri, req){
72 return new Promise(function(resolve, reject) {
73 request({
74 uri: uri,
75 method: 'GET',
76 headers: _.extend({}, {
77 'Authorization': req.get('Authorization'),
78 forever: CONSTANTS.FOREVER_ON,
79 rejectUnauthorized: false,
80 })
81 }, function(error, response, body) {
82 console.log(arguments)
83 if( response.statusCode == 401) {
84 reject({statusCode: 401, error: response.body});
85 } else {
86 resolve({statusCode:200, data:response.body})
87 }
88 });
89 });
90 }
91
92
93 module.exports = Router;