-
Notifications
You must be signed in to change notification settings - Fork 76
/
authservice_app.js
152 lines (131 loc) · 4.07 KB
/
authservice_app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
* Copyright (c) 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
var fs = require('fs');
var settings = JSON.parse(fs.readFileSync('settings.json', 'utf8'));
var log4js = require('log4js');
var logger = log4js.getLogger('authservice_app');
logger.setLevel(settings.loggerLevel);
var port = (process.env.VMC_APP_PORT || process.env.VCAP_APP_PORT || settings.authservice_port);
var host = (process.env.VCAP_APP_HOST || 'localhost');
logger.info("host:port=="+host+":"+port);
var dbtype = process.env.dbtype || "mongo";
//Calculate the backend datastore type if run inside BLuemix or cloud foundry
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
logger.info("env: %j",env);
var serviceKey = Object.keys(env)[0];
if (serviceKey && serviceKey.indexOf('cloudant')>-1)
dbtype="cloudant";
else if (serviceKey && serviceKey.indexOf('redis')>-1)
dbtype="redis";
}
logger.info("db type=="+dbtype);
var routes = new require('./authservice/routes/index.js')(dbtype,settings);
// call the packages we need
var express = require('express');
var app = express();
var morgan = require('morgan');
if (settings.useDevLogger)
app.use(morgan('dev')); // log every request to the console
var router = express.Router();
router.post('/byuserid/:user', createToken);
router.get('/:tokenid', validateToken);
router.get('/status', checkStatus);
router.delete('/:tokenid', invalidateToken);
// REGISTER OUR ROUTES so that all of routes will have prefix
app.use(settings.authContextRoot+'/authtoken', router);
var initialized = false;
var serverStarted = false;
initDB();
function initDB(){
if (initialized ) return;
routes.initializeDatabaseConnections(function(error) {
if (error) {
logger.error('Error connecting to database - exiting process: '+ error);
// Do not stop the process for debug in container service
//process.exit(1);
}else
{
initialized =true;
logger.info("Initialized database connections");
}
startServer();
});
}
function startServer() {
if (serverStarted ) return;
serverStarted = true;
app.listen(port);
console.log('Application started port ' + port);
}
function checkStatus(req, res){
res.sendStatus(200);
}
function createToken(req, res){
logger.debug('create token by user ' + req.params.user);
if (!initialized)
{
logger.info("please wait for db connection initialized then trigger again.");
initDB();
res.send(403);
}else
{
routes.createSessionInDB(req.params.user, function(error, cs){
if (error){
res.status(404).send(error);
}
else{
res.send(JSON.stringify(cs));
}
})
}
}
function validateToken(req, res){
logger.debug('validate token ' + req.params.tokenid);
if (!initialized)
{
logger.info("please wait for db connection initialized then trigger again.");
initDB();
res.send(403);
}else
{
routes.validateSessionInDB(req.params.tokenid, function(error, cs){
if (error){
res.status(404).send(error);
}
else{
res.send(JSON.stringify(cs));
}
})
}
}
function invalidateToken(req, res){
logger.debug('invalidate token ' + req.params.tokenid);
if (!initialized)
{
logger.info("please wait for db connection initialized then trigger again.");
initDB();
res.send(403);
}else
{
routes.invalidateSessionInDB(req.params.tokenid, function(error){
if (error){
res.status(404).send(error);
}
else res.sendStatus(200);
})
}
}