forked from cezane/wilma-pep-proxy-sgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (76 loc) · 2.58 KB
/
server.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
var config = require('./config'),
fs = require('fs'),
https = require('https'),
Root = require('./controllers/root').Root,
IDM = require("./lib/idm.js").IDM,
errorhandler = require('errorhandler');
config.azf = config.azf || {};
config.https = config.https || {};
config.sgx_attest = config.sgx_attest || {};
var log = require('./lib/logger').logger.getLogger("Server");
var express = require('express');
process.on('uncaughtException', function (err) {
log.error('Caught exception: ' + err);
});
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var app = express();
//app.use(express.bodyParser());
app.use (function(req, res, next) {
var bodyChunks = [];
req.on('data', function(chunk) {
bodyChunks.push(chunk);
});
req.on('end', function() {
if (bodyChunks.length > 0) {
req.body = Buffer.concat(bodyChunks);
};
next();
});
});
app.use(errorhandler({log: log.error}))
app.use(function (req, res, next) {
"use strict";
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'HEAD, POST, PUT, GET, OPTIONS, DELETE');
res.header('Access-Control-Allow-Headers', 'origin, content-type, X-Auth-Token, Tenant-ID, Authorization');
//log.debug("New Request: ", req.method);
if (req.method == 'OPTIONS') {
log.debug("CORS request");
res.statusCode = 200;
res.header('Content-Length', '0');
res.send();
res.end();
}
else {
next();
}
});
var port = config.pep_port || 80;
if (config.https.enabled) port = config.https.port || 443;
app.set('port', port);
for (var p in config.public_paths) {
log.debug('Public paths', config.public_paths[p]);
app.all(config.public_paths[p], Root.public);
}
app.all('/*', Root.pep);
if (config.tokens_engine === 'keystone' && config.azf.enabled === true) {
log.error('Keystone token engine is not compatible with AuthZForce. Please review configuration file.');
return;
}
log.info('Starting PEP proxy in port ' + port + '. Keystone authentication ...');
IDM.authenticate (function (token) {
log.info('Success authenticating PEP proxy. Proxy Auth-token: ', token);
}, function (status, e) {
log.error('Error in keystone communication', e);
});
if (config.https.enabled === true) {
var options = {
key: fs.readFileSync(config.https.key_file),
cert: fs.readFileSync(config.https.cert_file)
};
https.createServer(options, function(req,res) {
app.handle(req, res);
}).listen(app.get('port'));
} else {
app.listen(app.get('port'));
}