-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam_authenticate.bal
More file actions
392 lines (345 loc) · 16.2 KB
/
iam_authenticate.bal
File metadata and controls
392 lines (345 loc) · 16.2 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
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
import ballerina/http;
import ballerina/io;
import ballerina/log;
import ballerina/math;
import ballerina/stringutils;
import ballerina/time;
import ballerinax/java.jdbc;
import wso2/utils;
listener http:Listener IAM_AUTHENTICATOR = new (9090);
map<string> sessionMap = {};
map<boolean> authenticatedMap = {};
map<string> userMap = {"test1": "123", "test2": "456"};
string chatBuffer = "";
string pk = "";
string randomKey = "";
string ethereumAccount = "0x59Ce579B482E85B60d62676fFBbd1f7846F8393e";
string ethereumAccountPass = "1234";
http:Client ethereumClient = new ("http://127.0.0.1:8505");
jdbc:Client ssiDB = new ({
url: "jdbc:mysql://localhost/ssidb",
username: "root",
password: "",
dbOptions: {useSSL: false}
});
type DoctorCredential record {
string doctorType;
string name;
};
public function generateRandomKey(int keyLen) returns (string) {
string buffer = "";
int counter = 0;
while (counter < keyLen) {
int | error randVal = math:randomInRange(0, 26);
int n = 0;
if (randVal is int) {
n = randVal;
}
buffer += utils:decToChar(65 + n);
counter += 1;
}
io:println("generated Random key: " + buffer);
return buffer;
}
public function getVerifiableCredentials(string didmid) returns @tainted string {
io:println("verifying credentials");
time:Time currentTime = time:currentTime();
string | error timeStr = time:format(currentTime, "dd-MM-yyyy");
string customTimeString = "";
if (timeStr is string) {
customTimeString = timeStr;
}
string doctorType = "";
string firstname = "";
var selectRet = ssiDB->select(<@untainted>"select doctorType, firstname from ssidb.govid where (did LIKE '" + <@untainted>didmid + "');", DoctorCredential);
if (selectRet is table<DoctorCredential>) {
if (selectRet.hasNext()) {
var ret = selectRet.getNext();
if (ret is DoctorCredential) {
doctorType = ret.doctorType;
firstname = ret.name;
}
}
}
string finalResult = sendTransactionAndgetHash(doctorType);
string doctorCredential = "|||" + didmid + "," + finalResult + ",http://127.0.0.1:9090/api,DoctorCredential" + "||| " + "{" +
"// set the context, which establishes the special terms we will be using" +
"// such as 'issuer' and 'alumniOf'." +
"\"@context\": [" +
" \"https://www.w3.org/2018/credentials/v1\"," +
" \"https://www.w3.org/2018/credentials/examples/v1\"" +
"]," +
"// specify the identifier for the credential" +
"\"id\": \"http://localhost:9090/credentials/1\"," +
"// the credential types, which declare what data to expect in the credential" +
"\"type\": [\"VerifiableCredential\", \"DoctorCredential\"]," +
"// the entity that issued the credential" +
"\"issuer\": \"http://ip6-localhost:9090/api\"," +
"// when the credential was issued" +
"\"issuanceDate\": \"" + customTimeString + "\"," +
"// claims about the subjects of the credential" +
"\"credentialSubject\": {" +
"// identifier for the only subject of the credential" +
"\"id\": \"did:ethr:" + didmid + "\"," +
"// assertion about the only subject of the credential" +
"\"doctorType\": {" +
" \"id\": \"did:ethr:" + finalResult + "\"," +
"\"name\": [{" +
" \"value\": \"doctorType\"," +
"\"lang\": \"en\"" +
"}, {" +
" \"value\": \"" + doctorType + "\"," +
"\"lang\": \"en\"" +
"}]" +
"}" +
"}," +
"// digital proof that makes the credential tamper-evident" +
"// see the NOTE at end of this section for more detail" +
"\"proof\": {" +
"// the cryptographic signature suite that was used to generate the signature" +
"\"type\": \"RsaSignature2018\"," +
"// the date the signature was created" +
"\"created\": \"2017-06-18T21:19:10Z\"," +
"// purpose of this proof" +
"\"proofPurpose\": \"assertionMethod\"," +
"// the identifier of the public key that can verify the signature" +
"\"verificationMethod\": \"https://example.edu/issuers/keys/1\"," +
"// the digital signature value" +
"\"jws\": \"eyJhbGciOiJSUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..TCYt5X" +
"sITJX1CxPCT8yAV-TVkIEq_PbChOMqsLfRoPsnsgw5WEuts01mq-pQy7UJiN5mgRxD-WUc" +
"X16dUEMGlv50aqzpqh4Qktb3rk-BuQy72IFLOqV0G_zS245-kronKb78cPN25DGlcTwLtj" +
"PAYuNzVBAh4vGHSrQyHUdBBPM\"" +
"}" +
"}";
return doctorCredential;
}
public function sendTransactionAndgetHash(string data) returns (string) {
http:Request request2 = new;
io:println("try to connect with eth acc");
request2.setHeader("Content-Type", "application/json");
request2.setJsonPayload({"jsonrpc": "2.0", "id": "2000", "method": "personal_unlockAccount", "params": [ethereumAccount, ethereumAccountPass, null]});
string finalResult2 = "";
boolean errorFlag2 = false;
var httpResponse2 = ethereumClient->post("/", request2);
io:println("eth request sends to 8081");
if (httpResponse2 is http:Response) {
int statusCode = httpResponse2.statusCode;
string | error s = httpResponse2.getTextPayload();
var jsonResponse = httpResponse2.getJsonPayload();
if (jsonResponse is map<json>) {
if (jsonResponse["error"] == null) {
finalResult2 = jsonResponse.result.toString();
} else {
error err = error("EthereumError",
message = "Error occurred while accessing the JSON payload of the response");
finalResult2 = jsonResponse["error"].toString();
errorFlag2 = true;
log:printError("get hash err1: ", err = err);
}
} else {
error err = error("EthereumError", message = "Error occurred while accessing the JSON payload of the response");
finalResult2 = "Error occurred while accessing the JSON payload of the response";
errorFlag2 = true;
log:printError("get hash err2: ", err = err);
}
} else {
error err = error("EthereumError", message = "Error occurred while invoking the Ethererum API");
errorFlag2 = true;
log:printError("get hash err2: ", err = err);
}
io:println("no problem get acc 8081");
string hexEncodedString = "0x" + utils:hashSHA256(data);
//Next we will write the blockchain record
http:Request request = new;
request.setHeader("Content-Type", "application/json");
request.setJsonPayload({"jsonrpc": "2.0", "id": "2000", "method": "eth_sendTransaction", "params": [{"from": ethereumAccount, "to": "0x6814412628addef8989ee696a67b0fad5d62735e", "data": hexEncodedString}]});
io:println("eth write blockchain record 8081");
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 map<json>) {
if (jsonResponse["error"] == null) {
finalResult = jsonResponse.result.toString();
} else {
error err = error("EthereumError", message = "Error occurred while accessing the JSON payload of the response");
finalResult = jsonResponse["error"].toString();
errorFlag = true;
log:printError("get hash err4: ", err = err);
}
} else {
error err = error("EthereumError", message = "Error occurred while accessing the JSON payload of the response");
finalResult = "Error occurred while accessing the JSON payload of the response";
errorFlag = true;
log:printError("get hash err5: ", err = err);
}
} else {
error err = error("EthereumError", message = "Error occurred while invoking the Ethererum API");
errorFlag = true;
log:printError("get hash err6: ", err = err);
}
io:println("all good???");
return finalResult;
}
service uiServiceIAM_AUTHENTICATOR on IAM_AUTHENTICATOR {
@http:ResourceConfig {
methods: ["GET"],
path: "/",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function displayRegisterPage(http:Caller caller, http:Request req) {
string buffer = readFile("register.html");
http:Response res = new;
if (caller.localAddress.host != "") {
buffer = stringutils:replace(buffer, "127.0.0.1", caller.localAddress.host);
}
res.setPayload(<@untainted>buffer);
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");
var result = caller->respond(res);
if (result is error) {
log:printError("Error sending response", err = result);
}
}
@http:ResourceConfig {
methods: ["POST"],
path: "/api",
cors: {
allowOrigins: ["*"],
allowHeaders: ["Authorization, Lang"]
}
}
resource function api(http:Caller caller, http:Request req) returns error? {
var requestVariableMap = check req.getFormParams();
if (requestVariableMap["command"] == "cmd1") {
io:println("inside cmd1");
if (requestVariableMap.hasKey("secureToken") && (!stringutils:equalsIgnoreCase(randomKey, requestVariableMap["secureToken"] ?: ""))) {
io:println("incorrect sec token");
var result = caller->respond("incorrect-token");
if (result is error) {
log:printError("Error sending response", err = result);
}
} else {
if (requestVariableMap.hasKey("did")) {
pk = <@untainted>requestVariableMap["did"] ?: "";
io:println("pk :" + pk);
var result = caller->respond("done");
if (result is error) {
log:printError("Error sending response", err = result);
}
}
}
} else if (requestVariableMap["command"] == "requestvc") {
io:println("insdie requestvc");
string didxx = requestVariableMap["did"] ?: "didx";
io:println("did api reqvc: " + didxx);
var did = requestVariableMap["did"] ?: "";
did = stringutils:replace(did, "%2C", ",");
did = utils:binaryStringToString(did);
int index2 = (did.indexOf("\"id\": \"did:ethr:") ?: 0) + 16;
string didmid = did.substring(index2, index2 + 64);
io:println("didmid api reqvc: " + didmid);
index2 = (did.indexOf("-----BEGIN PUBLIC KEY-----") ?: 0) + 26;
int index3 = did.indexOf("-----END PUBLIC KEY-----") ?: 0;
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": [<@untainted>didmid]});
string finalResult = "";
string pkHash = "";
boolean errorFlag = false;
io:println("is going to 8081??");
var httpResponse = ethereumClient->post("/", request);
if (httpResponse is http:Response) {
int statusCode = httpResponse.statusCode;
var jsonResponse = httpResponse.getJsonPayload();
if (jsonResponse is map<json>) {
if (jsonResponse["error"] == null) {
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;
log:printError("requesvc err1", err = err);
}
} else {
error err = error("(wso2/ethereum)EthereumError", message = "Error occurred while accessing the JSON payload of the response");
finalResult = "Error occurred while accessing the JSON payload of the response";
errorFlag = true;
log:printError("requesvc err2", err = err);
}
} else {
error err = error("(wso2/ethereum)EthereumError", message = "Error occurred while invoking the Ethererum API");
errorFlag = true;
log:printError("requesvc err3", err = err);
}
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);
io:println("success key validadtion");
} else {
finalResult = "Failure in Key Verification";
io:println("key validation failed");
}
http:Response res = new;
// A util method that can be used to set string payload.
io:println("final result key validation: " + finalResult);
res.setPayload(<@untainted>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);
}
io:println("send key response successfully");
} else if (requestVariableMap["command"] == "encresponse") {
var did = requestVariableMap["did"] ?: "";
string didxx = requestVariableMap["did"] ?: "didx";
io:println("encresponse did: " + didxx);
var encryptedval = requestVariableMap["encryptedval"] ?: "";
did = stringutils:replace(did, "%2C", ",");
did = utils:binaryStringToString(did);
int index2 = (did.indexOf("\"id\": \"did:ethr:") ?: 0) + 16;
string didmid = did.substring(index2, index2 + 64);
index2 = (did.indexOf("-----BEGIN PUBLIC KEY-----") ?: 0) + 26;
io:println("index2" + index2.toString());
int index3 = did.indexOf("-----END PUBLIC KEY-----") ?: 0;
var publicKey = did.substring(index2, index3);
var didmidOrg = didmid;
didmid = "0x" + didmid;
string randKey = sessionMap[didmid] ?: "";
if (encryptedval === randKey) {
io:println(encryptedval.toString() + randKey.toString() + didmidOrg.toString());
var verifiableCredentialsList = getVerifiableCredentials(didmidOrg);
http:Response res = new;
// A util method that can be used to set string payload.
res.setPayload(<@untainted>verifiableCredentialsList);
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);
io:println("enc response sends....");
if (result is error) {
log:printError("Error sending response", err = result);
}
} else {
io:println("Challenge response authentication failed.");
}
}
}
}