-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChatService.bal
663 lines (562 loc) · 24.9 KB
/
ChatService.bal
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Copyright (c) 2019 Miyuru Dayarathna All Rights Reserved.
//
// Miyuru Dayarathna licenses this file to you 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.
import ballerina/http;
import ballerina/log;
import ballerina/io;
import ballerina/config;
import ballerina/math;
import wso2/ethereum;
import wso2/utils;
listener http:Listener uiEP = new(9097);
listener http:Listener blockChainInterfaceEP = new(9096);
map<string> sessionMap = {};
map<boolean> authenticatedMap = {};
string chatBuffer = "";
ethereum:EthereumConfiguration ethereumConfig = {
jsonRpcEndpoint: "http://192.168.32.1:8081",
jsonRpcVersion: "2.0",
networkId: "2000"
};
string ethereumAccount = "0xee0727d9e94dbb230233ea4bd362e4c081aabf38";
string jsonRpcEndpoint = ethereumConfig.jsonRpcEndpoint;
http:Client ethereumClient = new(jsonRpcEndpoint, config = ethereumConfig.clientConfig);
boolean verifiableCredentialsFlag = true;
@http:ServiceConfig { basePath:"/" }
service uiService on uiEP {
@http:ResourceConfig {
methods:["GET"],
path:"/"
}
resource function sayHello(http:Caller caller, http:Request req, string name, string message) {
io:ReadableByteChannel readableByteChannel = io:openReadableFile("web/login.html");
var readableCharChannel = new io:ReadableCharacterChannel(readableByteChannel, "UTF-8");
var readableRecordsChannel = new io:ReadableTextRecordChannel(readableCharChannel);
string buffer = "";
while (readableRecordsChannel.hasNext()) {
var result = readableRecordsChannel.getNext();
if (result is string[]) {
string item = string.convert(result[0]);
buffer += item;
} else {
io:println("Error");
}
}
http:Response res = new;
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
}
res.setPayload(untaint buffer);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/logout",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function logout(http:Caller caller, http:Request req, string name, string message) {
map<string> requestVariableMap = req. getQueryParams();
var did = requestVariableMap["did"] ?: "";
authenticatedMap[did] = false;
string buffer = "http://localhost:9097";
http:Response res = new;
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
}
res.setPayload(untaint buffer);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/jsencrypt.js"
}
resource function sendJSEncrypt(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/jsencrypt.js", contentType = "text/javascript");
res.setContentType("text/javascript; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/browser-aes.js"
}
resource function sendJSBrowser(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/browser-aes.js", contentType = "text/javascript");
res.setContentType("text/javascript; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/bg.jpg"
}
resource function sendBGImage(http:Caller caller, http:Request req, string name, string message) {
http:Response res = new;
res.setFileAsPayload("web/images/bg.jpg", contentType = "image/jpeg");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods:["POST"],
path:"/authentication",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function authenticationPage(http:Caller caller, http:Request req, string name, string message) returns error?{
string buffer = "";
map<string> requestVariableMap = check req.getFormParams();
if (requestVariableMap["command"] == "authenticate") {
var did = requestVariableMap["did"] ?: "";
did = did.replace("%2C", ",");
int index2 = did.indexOf("\"id\": \"did:ethr:") + 16;
string didmid = did.substring(index2, index2+64);
index2 = did.indexOf("-----BEGIN PUBLIC KEY-----") + 26;
int index3 = did.indexOf("-----END PUBLIC KEY-----");
var publicKey = did.substring(index2, index3);
didmid = "0x" + didmid;
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_getTransactionByHash", "params":[untaint didmid]});
string finalResult = "";
string pkHash = "";
boolean errorFlag = false;
var httpResponse = ethereumClient -> post("/", request);
if (httpResponse is http:Response) {
int statusCode = httpResponse.statusCode;
var jsonResponse = httpResponse.getJsonPayload();
if (jsonResponse is json) {
if (jsonResponse["error"] == null) {
finalResult = jsonResponse.result.toString();
pkHash = jsonResponse.result["input"].toString();
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse["error"].toString();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse.reason();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError", { message: "Error occurred while invoking the Ethererum API" });
errorFlag = true;
}
string hexEncodedString = "0x" + utils:hashSHA256("-----BEGIN PUBLIC KEY-----" + publicKey + "-----END PUBLIC KEY-----");
if (hexEncodedString == pkHash) {
string randKey = generateRandomKey(16);
sessionMap[didmid] = randKey;
finalResult = utils:encryptRSAWithPublicKey(publicKey, randKey);
} else {
finalResult = "Failure in Key Verification";
}
http:Response res = new;
// A util method that can be used to set string payload.
res.setPayload(untaint finalResult);
res.setContentType("text/html; charset=utf-8");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Lang");
// Sends the response back to the client.
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
} else if (requestVariableMap["command"] == "encresponse") {
var did = requestVariableMap["did"] ?: "";
var encryptedval = requestVariableMap["encryptedval"] ?: "";
did = did.replace("%2C", ",");
int index2 = did.indexOf("\"id\": \"did:ethr:") + 16;
string didmid = did.substring(index2, index2+64);
index2 = did.indexOf("-----BEGIN PUBLIC KEY-----") + 26;
int index3 = did.indexOf("-----END PUBLIC KEY-----");
var publicKey = did.substring(index2, index3);
var didmidOrg = didmid;
didmid = "0x" + didmid;
string randKey = sessionMap[didmid]?: "";
if (encryptedval === randKey) {
io:println("Challenge response authentication was successful.");
var finalResult = "successful";
if(verifiableCredentialsFlag) {
io:println("Require verifiable credentials.");
finalResult = "successful|CountryCredential"; //Here we assume that chat service requires CountryCredential only.
}
http:Response res = new;
// A util method that can be used to set string payload.
res.setPayload(untaint finalResult);
res.setContentType("text/html; charset=utf-8");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Lang");
// Sends the response back to the client.
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
} else {
io:println("Challenge response authentication failed.");
}
} else if (requestVariableMap["command"] == "vcsubmit") {
var did = requestVariableMap["did"] ?: "";
var vc = requestVariableMap["vc"] ?: "";
int index2 = vc.indexOf("\"homeCountry\": {") + 41;
string didmid = vc.substring(index2, index2 + 66);
string hash = readHashFromBloackchain(didmid);
index2 = hash.indexOf("\"input\":\"") + 9;
hash = hash.substring(index2, index2 + 66);
string hexEncodedString = "0x" + utils:hashSHA256("USA");
string buffer2 = "";
string hostname = "localhost";
if (hash === hexEncodedString) {
authenticatedMap[did] = true;
buffer2 = "http://" + hostname + ":9097/home?did=" + did;
} else {
buffer2 = "http://" + hostname + ":9097";
}
http:Response res = new;
res.setPayload(untaint buffer2);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
@http:ResourceConfig {
methods:["GET"],
path:"/home"
}
resource function sendHomePage(http:Caller caller, http:Request req, string name, string message) {
map<string> requestVariableMap = req. getQueryParams();
io:ReadableByteChannel readableByteChannel = io:openReadableFile("web/home.html");
var readableCharChannel = new io:ReadableCharacterChannel(readableByteChannel, "UTF-8");
var readableRecordsChannel = new io:ReadableTextRecordChannel(readableCharChannel);
string buffer = "";
while (readableRecordsChannel.hasNext()) {
var result = readableRecordsChannel.getNext();
if (result is string[]) {
string item = string.convert(result[0]);
buffer += item;
} else {
io:println("Error");
}
}
http:Response res = new;
buffer = buffer.replace("uname", requestVariableMap["did"] ?: "abc");
if (caller.localAddress.host != "") {
buffer= buffer.replace("localhost", caller.localAddress.host);
}
res.setPayload(untaint buffer);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
@http:ServiceConfig { basePath:"/",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
service chainPage on blockChainInterfaceEP {
public function constructRequest (string jsonRPCVersion, int networkId, string method, json params) returns http:Request {
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":jsonRPCVersion, "id":networkId, "method":method, "params":params});
return request;
}
public function resultToString(json jsonPayload) returns string {
string result = jsonPayload["result"] != null ? jsonPayload["result"].toString() : "";
return result;
}
public function setResponseError(json jsonResponse) returns error {
map<string> details = { message: jsonResponse["error"].message.toString() };
error err = error("(wso2/ethereum)EthereumError", details);
return err;
}
@http:ResourceConfig {
methods:["POST"],
path:"/",
cors: {
allowOrigins: ["*"]
}
}
resource function respond(http:Caller caller, http:Request req, string name, string message) returns error? {
var requestVariableMap = check req.getFormParams();
string encryptedval = requestVariableMap["encryptedval"] ?: "";
string username = requestVariableMap["username"] ?: "";
var randKey = sessionMap[username] ?: "";
string decryptedval = utils:decryptAes(encryptedval, randKey);
if (decryptedval == "ack") {
io:println("Welcome " + username);
authenticatedMap[username] = true;
} else{
authenticatedMap[username] = false;
}
return;
}
@http:ResourceConfig {
methods:["GET"],
path:"/",
cors: {
allowOrigins: ["*"]
}
}
resource function sayHello(http:Caller caller, http:Request req, string name, string message) {
string resultBuffer = "";
map<string> requestVariableMap = req. getQueryParams();
var logoutFlag = requestVariableMap["logout"] ?: "false";
boolean flg = boolean.convert(logoutFlag);
string uname = requestVariableMap["username"] ?: "";
string hostname = "localhost";
if (caller.localAddress.host != "") {
hostname= caller.localAddress.host;
}
string buffer = "http://" + hostname + ":9093";
if (flg) {
authenticatedMap[uname] = false;
sessionMap[uname] = "";
io:println(uname + " logged out.");
http:Response res = new;
res.setPayload(untaint buffer);
res.setContentType("text/html; charset=utf-8");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
return;
}
// string functionToCall = functionMap[uname] ?: "";
// http:Request request = new;
// request.setHeader("Content-Type", "application/json");
// request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_call", "params":[{"from": "0x88c9a72c84636bd5f39fe63cf4440214be31c061", "to":"0xbd7bc5b627cce81bf916b9f621ad79b96a4d7df1", "data": functionToCall}, "latest"]});
// string finalResult = "";
// boolean errorFlag = false;
// var httpResponse = ethereumClient -> post("/", request);
// if (httpResponse is http:Response) {
// int statusCode = httpResponse.statusCode;
// var jsonResponse = httpResponse.getJsonPayload();
// if (jsonResponse is json) {
// if (jsonResponse["error"] == null) {
// string inputString = jsonResponse.result.toString();
// finalResult = convertHexStringToString(inputString);
// } else {
// error err = error("(wso2/ethereum)EthereumError",
// { message: "Error occurred while accessing the JSON payload of the response" });
// finalResult = jsonResponse["error"].toString();
// errorFlag = true;
// }
// } else {
// error err = error("(wso2/ethereum)EthereumError",
// { message: "Error occurred while accessing the JSON payload of the response" });
// finalResult = jsonResponse.reason();
// errorFlag = true;
// }
// } else {
// error err = error("(wso2/ethereum)EthereumError", { message: "Error occurred while invoking the Ethererum API" });
// errorFlag = true;
// }
// if (!errorFlag) {
// string hashKey = untaint finalResult;
// io:ReadableCharacterChannel sourceChannel = new (io:openReadableFile("key-db/" + untaint uname + "/" + hashKey), "UTF-8");
// var readableRecordsChannel = new io:ReadableTextRecordChannel(sourceChannel, fs = ",", rs = "\n");
// while (readableRecordsChannel.hasNext()) {
// var result = readableRecordsChannel.getNext();
// if (result is string[]) {
// string randKey = generateRandomKey(16);
// sessionMap[uname] = randKey;
// finalResult = utils:encryptRSAWithPublicKey(result[0], randKey);
// } else {
// //return result; // An IO error occurred when reading the records.
// }
// }
// } else {
// io:println("An error has ocurred.");
// }
// http:Response res = new;
// // A util method that can be used to set string payload.
// res.setPayload(untaint finalResult);
// res.setContentType("text/html; charset=utf-8");
// res.setHeader("Access-Control-Allow-Origin", "*");
// res.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE");
// res.setHeader("Access-Control-Allow-Headers", "Authorization, Lang");
// // Sends the response back to the client.
// var result = caller->respond(res);
// if (result is error) {
// log:printError("Error sending response", err = result);
// }
}
}
@http:WebSocketServiceConfig {
path: "/basic/ws",
subProtocols: ["xml", "json"],
idleTimeoutInSeconds: 120
}
service basic on new http:WebSocketListener(9098) {
string ping = "ping";
byte[] pingData = ping.toByteArray("UTF-8");
resource function onOpen(http:WebSocketCaller caller) {
var err = caller->pushText(chatBuffer);
if (err is error) {
log:printError("Error occurred when sending text", err = err);
}
}
resource function onText(http:WebSocketCaller caller, string text,
boolean finalFrame) {
if (text == "ping") {
var err = caller->ping(self.pingData);
if (err is error) {
log:printError("Error sending ping", err = err);
}
} else if (text == "closeMe") {
_ = caller->close(statusCode = 1001,
reason = "You asked me to close the connection",
timeoutInSecs = 0);
} else {
chatBuffer += untaint text + "\r\n";
var err = caller->pushText(chatBuffer);
if (err is error) {
log:printError("Error occurred when sending text", err = err);
}
io:println("Pinging...");
var err2 = caller->ping(self.pingData);
if (err2 is error) {
log:printError("Error sending ping", err = err);
}
}
}
resource function onBinary(http:WebSocketCaller caller, byte[] b) {
io:println("\nNew binary message received");
io:print("UTF-8 decoded binary message: ");
io:println(b);
var err = caller->pushBinary(b);
if (err is error) {
log:printError("Error occurred when sending binary", err = err);
}
}
resource function onPing(http:WebSocketCaller caller, byte[] data) {
var err = caller->pong(data);
if (err is error) {
log:printError("Error occurred when closing the connection",
err = err);
}
}
resource function onPong(http:WebSocketCaller caller, byte[] data) {
io:println("Pong received");
}
resource function onIdleTimeout(http:WebSocketCaller caller) {
io:println("\nReached idle timeout");
io:println("Closing connection " + caller.id);
var err = caller->close(statusCode = 1001, reason =
"Connection timeout");
if (err is error) {
log:printError("Error occured when closing the connection",
err = err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
log:printError("Error occurred ", err = err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode,
string reason) {
io:println(string `Client left with {{statusCode}} because
{{reason}}`);
}
}
public function convertHexStringToString(string inputString) returns (string) {
int len = inputString.length();
int counter = 0;
string buffer = "";
while (counter < len) {
string s3 = inputString.substring(counter, counter+2);
if (s3.equalsIgnoreCase("0x")) {
counter += 2;
continue;
}
var res1 = utils:hexToChar(s3);
if (res1.length() != 0) {
buffer += res1;
}
counter += 2;
}
return buffer;
}
public function generateRandomKey(int keyLen) returns (string){
string buffer = "";
int counter = 0;
while (counter < keyLen) {
buffer += utils:decToChar(65 + math:randomInRange(0, 26));
counter += 1;
}
return buffer;
}
public function readHashFromBloackchain(string didmid) returns (string) {
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc":"2.0", "id":"2000", "method":"eth_getTransactionByHash", "params":[untaint didmid]});
string finalResult = "";
string pkHash = "";
boolean errorFlag = false;
var httpResponse = ethereumClient -> post("/", request);
if (httpResponse is http:Response) {
int statusCode = httpResponse.statusCode;
var jsonResponse = httpResponse.getJsonPayload();
if (jsonResponse is json) {
if (jsonResponse["error"] == null) {
finalResult = jsonResponse.result.toString();
pkHash = jsonResponse.result["input"].toString();
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse["error"].toString();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError",
{ message: "Error occurred while accessing the JSON payload of the response" });
finalResult = jsonResponse.reason();
errorFlag = true;
}
} else {
error err = error("(wso2/ethereum)EthereumError", { message: "Error occurred while invoking the Ethererum API" });
errorFlag = true;
}
return finalResult;
}