update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / core / modules / routes / sessions.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 sessions routes module.
22 * Provides a RESTful API to manage sessions.
23 * @module framework/core/modules/routes/sessions
24 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
25 */
26
27 var cors = require('cors');
28 var bodyParser = require('body-parser');
29 var sessionsAPI = require('../api/sessions');
30 var Router = require('express').Router();
31 var utils = require('../../api_utils/utils');
32 var CONSTANTS = require('../../api_utils/constants.js');
33 var request = require('request');
34 var _ = require('lodash');
35
36 var sessions = {};
37
38 sessions.routes = function(sessionsConfig) {
39 Router.use(bodyParser.json());
40 Router.use(cors());
41 Router.use(bodyParser.urlencoded({
42 extended: true
43 }));
44
45 // Overloaded get method to handle OpenIDConnect redirect to establish a session.
46 Router.get('/session*', cors(), /*sessionsConfig.authManager.passport.authenticate('main', {
47 noredirect: false
48 }), */function(req, res) {
49 req.query['api_server'] = sessionsConfig.api_server_protocol + '://' + sessionsConfig.api_server;
50 sessionsAPI.create(req, res).then(function(data) {
51 utils.sendSuccessResponse(data, res);
52 });
53 });
54
55 // For project switcher UI
56 Router.put('/session/:projectId', cors(), function(req, res) {
57 sessionsAPI.addProjectToSession(req, res).then(function(data) {
58 utils.sendSuccessResponse(data, res);
59 }, function(error) {
60 utils.sendErrorResponse(error, res);
61 });
62 });
63
64 Router.delete('/session', cors(), function(req, res) {
65 sessionsAPI.delete(req, res).then(function(data) {
66 utils.sendSuccessResponse(data, res);
67 });
68 });
69 }
70
71 sessions.router = Router;
72
73
74 module.exports = sessions;