-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecsignid-server.js
More file actions
259 lines (216 loc) · 9.4 KB
/
Copy pathsecsignid-server.js
File metadata and controls
259 lines (216 loc) · 9.4 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// $Id: secsignid-server.js,v 1.2 2015/04/16 15:51:36 titus Exp $
/*!
* (c) 2015 SecSign Technologies Inc.
*/
//
// An example how a webserver based on Node.js could fetch authentication session requested by a website.
// The authentication will be requested, checked etc and the result is sent to browser
//
var path = require("path");
var fs = require("fs");
var url = require("url");
var http = require('http');
var querystring = require('querystring');
// include SecSign ID Api for node.js
var SecSignIDApi = require('./SecSignIDApi.nodejs.js');
// start webserver to listen for requests...
var server = http.createServer(function(request, response) {
// reads in the file and replace specified replacements
var getHtmlFile = function(filename, replacements){
var file = fs.readFileSync(filename, 'utf8');
if(replacements){
for(var key in replacements){
if(replacements.hasOwnProperty(key)){
var re = new RegExp(key, 'g');
file = file.replace(re, replacements[key]);
}
}
}
return file;
};
//
// function to handle either parameter or requested files...
//
var handleParameter = function(data){
// check secsign id parameter exists. otherwise serve the requestor with the requested files
var hostname = request.headers.host;
var uri = url.parse(request.url).pathname;
var file = path.join(path.join(process.cwd(), "/web-example/"), uri);
var pathname = path.parse(file).dir;
var exists = fs.existsSync(file);
if(exists && fs.statSync(file).isDirectory()){
pathname = file;
}
if(data){
// check whether some parameter were sent which we need to redirect to id-server
if(data.secsignid && data.login){
// login for a secsign id is requested...
// create new instance of SecSign ID Api
var secSignIDApi = new SecSignIDApi();
secSignIDApi.requestAuthSession({"secsignid" : data.secsignid,
"servicename" : "Node.js Test Website",
"serviceaddress" : hostname}, function(receivedAuthSession){
if(receivedAuthSession.error){
// error occured: get error html and sent it to browser
var f = getHtmlFile(pathname + "/error.html", {"%errormsg%" : receivedAuthSession.errormsg});
response.writeHead(200);
response.write(f);
response.end();
} else {
// received an authentication session
var f = getHtmlFile(pathname + "/access-pass.html", {
"%requestid%" : receivedAuthSession.requestid,
"%secsignid%" : receivedAuthSession.secsignid,
"%authsessionid%" : receivedAuthSession.authsessionid,
"%servicename%" : receivedAuthSession.servicename,
"%serviceaddress%" : receivedAuthSession.serviceaddress,
"%icondata%" : receivedAuthSession.authsessionicondata,
"%msg%" : ""
});
response.writeHead(200);
response.write(f);
response.end();
}
});
return;
} else if(data.secsignid && data.requestid && data.cancel){
// need to cancel the authentication session
var secSignIDApi = new SecSignIDApi();
secSignIDApi.cancelAuthSession({"secsignid" : data.secsignid,
"requestid" : data.requestid,
"authsessionid" : data.authsessionid}, function(canceledAuthSession){
var msg = "The auth session was canceled";
if(canceledAuthSession.error){
msg = canceledAuthSession.errormsg;
}
var f = getHtmlFile(pathname + "/error.html", {"%errormsg%" : msg});
response.writeHead(200);
response.write(f);
response.end();
});
return;
} else if(data.secsignid && data.requestid && data.check){
// need to check the authentication session
var secSignIDApi = new SecSignIDApi();
secSignIDApi.getAuthSessionState({"secsignid" : data.secsignid,
"requestid" : data.requestid,
"authsessionid" : data.authsessionid}, function(checkedAuthSession){
if(checkedAuthSession.error){
// error occured: get error html and sent it to browser
var f = getHtmlFile(pathname + "/error.html", {"%errormsg%" : checkedAuthSession.errormsg});
response.writeHead(200);
response.write(f);
response.end();
} else {
if(checkedAuthSession.authsessionstate == SecSignIDApi.AuthSession.PENDING ||
checkedAuthSession.authsessionstate == SecSignIDApi.AuthSession.FETCHED){
// get the access pass page and send it to the caller/browser
var f = getHtmlFile(pathname + "/access-pass.html", {
"%requestid%" : data.requestid,
"%secsignid%" : data.secsignid,
"%authsessionid%" : data.authsessionid,
"%servicename%" : data.servicename,
"%serviceaddress%" : data.serviceaddress,
"%icondata%" : data.authsessionicondata,
"%msg%" : "The authentication session is still pending... It has neither be accepted nor denied. "
});
response.writeHead(200);
response.write(f);
response.end();
} else if(checkedAuthSession.authsessionstate == SecSignIDApi.AuthSession.AUTHENTICATED) {
//
//
// the authentication was accepted by the user. he picked the correct access pass on his smartphone.
// now the user could be logged in into a cms or the website or something else.
// the user - secsign id mapping is the job of the system.
// e.g.:
// logging in into wordpress at this point the authenticated secsign id will be checked if it is assigned to a wordpress user
// in that case the wordpress user will be logged in at wordpress by setting flags and indicators of the user session
// therefor the authentication process according secsign id ends here. the session can be released so it will be removed at the id server.
// in case the session state must be checked later, the session should not be released now.
//
//
//
// release the auth session
//
var secSignIDApi = new SecSignIDApi();
secSignIDApi.releaseAuthSession({"secsignid" : data.secsignid,
"requestid" : data.requestid,
"authsessionid" : data.authsessionid});
// get success page and send to browser
var f = getHtmlFile(pathname + "/login-success.html", {"%secsignid%" : data.secsignid});
response.writeHead(200);
response.write(f);
response.end();
} else {
// something happend... the session state is invalid, expired, suspended, denied, canceled or something else...
// go back to start
var authSession = new SecSignIDApi.AuthSession(checkedAuthSession);
var f = getHtmlFile(pathname + "/error.html", {"%errormsg%" : "Canceled process because session was in state: " + authSession.getStateName()});
response.writeHead(200);
response.write(f);
response.end();
}
}
});
return
}
}
if(! exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if(fs.statSync(file).isDirectory()){
file += '/index.html';
}
// read requested file and sent to browser
fs.readFile(file, "binary", function(error, file) {
if(error) {
// probably the file does not exists
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
};
//
// check the request and read in parameter
// parameter are sent either by post or by get
//
var r = request;
if(r.method === 'POST'){
// parse post data...
var queryData = "";
r.on('data', function(data){
queryData += data;
if(queryData.length > 1e6) {
queryData = "";
// data was to loong
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
r.connection.destroy();
}
});
// finished reading post data
r.on('end', function() {
r.post = querystring.parse(queryData);
// handle parameter and sent result to caller/browser
handleParameter(r.post);
});
} else if(r.method === 'GET'){
if(r.url.indexOf("?") > -1){
r.get = querystring.parse(r.url.substring(r.url.indexOf("?")+1));
} else {
r.get = {};
}
// handle parameter and sent result to caller/browser
handleParameter(r.get);
}
});
// start webserver
server.listen(8080);