-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
760 lines (643 loc) · 34.2 KB
/
Program.cs
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
namespace FHIRClient;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Security.Claims;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using IdentityModel.Client;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Jose;
using System.Collections.Immutable;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.X509;
using Sharprompt;
using System.Dynamic;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using System.Runtime.Versioning;
using Org.BouncyCastle.Utilities.IO.Pem;
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// configuration data - FHIR repository
FhirRepositoryConfig fhirRepositoryConfig = null!;
var baseFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\CoZoSHIFT";
if (!Directory.Exists(baseFilePath))
{
Directory.CreateDirectory(baseFilePath);
}
var correlationId = Guid.NewGuid().ToString();
var fhirRepositoryConfigFile = baseFilePath + @"\fhir-repository-config.json";
// serialize JSON directly to a file
if (!File.Exists(fhirRepositoryConfigFile))
{
var fhirRepositoryConfigDefault = new FhirRepositoryConfig()
{
Name = "Name of your FHIR Repository",
FhirEndpoint = "https://fhirserver.org/fhir-server/api/v4",
OauthTokenEndpoint = "https://fhirserver.org/oauth2/token",
OauthClientId = "",
OauthScopes = "system/Organization.cruds system/Practitioner.cruds system/PractitionerRole.cruds system/Patient.cruds system/Condition.cruds",
JwkSetUrl = "https://api-acpt.ehealth.fgov.be/etee/v1/pubKeys/cacerts/jwks?identifier=<riziv-hospital>&type=NIHII-HOSPITAL&use=sig",
JwkSetKeyId = "",
KeystoreFile = baseFilePath + @"NIHII-HOSPITAL=xxxxxx 20220415-104753.acc-p12",
KeystoreFilePassword = "",
MaxResultsPerPage = 200
};
fhirRepositoryConfig = fhirRepositoryConfigDefault;
// store the repo configuration a file
using (StreamWriter file = File.CreateText(fhirRepositoryConfigFile))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, fhirRepositoryConfig);
}
}
else
{
var jsonRepositoryConfig = File.ReadAllText(fhirRepositoryConfigFile);
if (jsonRepositoryConfig != null)
{
fhirRepositoryConfig = System.Text.Json.JsonSerializer.Deserialize<FhirRepositoryConfig>(jsonRepositoryConfig)!;
}
}
if ((fhirRepositoryConfig.OauthClientId == "") || (fhirRepositoryConfig.OauthClientId == null))
{
Console.WriteLine("Incomplete repository configuration - please update file " + fhirRepositoryConfigFile);
Console.WriteLine("Press any key to stop...");
Console.ReadKey();
System.Environment.Exit(-1);
}
if ((fhirRepositoryConfig.JwkSetKeyId == "") || (fhirRepositoryConfig.JwkSetKeyId == null))
{
Console.WriteLine("Incomplete repository configuration - please update file " + fhirRepositoryConfigFile);
Console.WriteLine("Press any key to stop...");
Console.ReadKey();
System.Environment.Exit(-1);
}
Console.WriteLine(new string('-', 80));
Console.WriteLine("Download meest recente versie van FHIRClient via : " + "https://cozoshift.s3.eu-central-1.amazonaws.com/FHIRClient.exe");
Console.WriteLine("Repository configuratie bestand: " + fhirRepositoryConfigFile);
Console.WriteLine("Gedetailleerde log bestanden worden opgeslagen in bestand " + baseFilePath + @"\logs\" + correlationId + ".txt");
Console.WriteLine("FHIR endpoint: " + fhirRepositoryConfig.FhirEndpoint);
Console.WriteLine("OAuth token endpoint: " + fhirRepositoryConfig.OauthTokenEndpoint);
Console.WriteLine(new string('-', 80));
Console.WriteLine("\r\n");
// collect end user properties
// configuration data - end user properties -- https://github.com/shibayan/Sharprompt
bool userSelected = false;
string subjectRoleCode = "";
string userIdType = "";
string userIdentifierValue = "";
string userIdentifierSystem = "";
string subjectName = "";
bool organizationSelected = false;
string subjectOrganization = "";
string subjectOrganizationIdentifierValue = "";
string subjectOrganizationIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/nihdi";
bool patientSelected = false;
string patientIdentifier = "";
string patientIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/ssin";
if (fhirRepositoryConfig.UserInputDefaults != null)
{
if ((fhirRepositoryConfig.UserInputDefaults.Users != null) && (fhirRepositoryConfig.UserInputDefaults.Users.Length > 0))
{
var user = Prompt.Select("Selecteer gebruiker", fhirRepositoryConfig.UserInputDefaults.Users);
subjectRoleCode = user.Role;
if ((user.RIZIV != null) && (user.RIZIV != ""))
{
userIdType = "RIZIVNR";
userIdentifierValue = user.RIZIV;
userIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/nihdi";
}
else
{
userIdType = "INSZ";
userIdentifierValue = user.INSZ;
userIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/ssin";
}
subjectName = user.LastName + " " + user.GivenName;
userSelected = true;
}
if (fhirRepositoryConfig.UserInputDefaults.Organization != null)
{
subjectOrganization = fhirRepositoryConfig.UserInputDefaults.Organization.Name;
subjectOrganizationIdentifierValue = fhirRepositoryConfig.UserInputDefaults.Organization.RIZIV;
organizationSelected = true;
}
if ((fhirRepositoryConfig.UserInputDefaults.Patients != null) && (fhirRepositoryConfig.UserInputDefaults.Patients.Length > 0))
{
var patient = Prompt.Select("Selecteer patient", fhirRepositoryConfig.UserInputDefaults.Patients);
if ((patient.INSZ != null) && (patient.INSZ != ""))
{
patientIdentifier = patient.INSZ;
patientIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/ssin";
patientSelected = true;
}
else
{
if ((patient.Identifier != null) && (patient.Identifier != "") && (patient.IdentifierSystem != null) && (patient.IdentifierSystem != ""))
{
patientIdentifier = patient.Identifier;
patientIdentifierSystem = patient.IdentifierSystem;
patientSelected = true;
}
}
}
}
if (!userSelected)
{
subjectRoleCode = Prompt.Select("Type gebruiker", new[] { "persphysician", "persnurse", "perspharmacist", "persdentist" });
userIdType = Prompt.Select("Gebruiker identificatienummer type", new[] { "INSZ", "RIZIVNR" });
if (userIdType == "INSZ")
{
userIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/ssin";
userIdentifierValue = Prompt.Input<string>("Gebruiker INSZ", defaultValue: "", validators: new[] { Validators.Required(), Validators.MinLength(11) });
}
else
{
userIdentifierValue = Prompt.Input<string>("Gebruiker RIZIV nummer ", defaultValue: "10828465123", validators: new[] { Validators.Required(), Validators.MinLength(11) });
userIdentifierSystem = "https://www.ehealth.fgov.be/standards/fhir/core/NamingSystem/nihdi";
}
subjectName = Prompt.Input<string>("Naam gebruiker: ", defaultValue: "Naam van de zorgverlener");
}
if (!organizationSelected)
{
subjectOrganization = Prompt.Input<string>("Instelling: ", defaultValue: "Amaron Ziekenhuis");
subjectOrganizationIdentifierValue = Prompt.Input<string>("RIZIV nummer instelling: ", defaultValue: "71045471", validators: new[] { Validators.Required(), Validators.MinLength(8), Validators.MaxLength(8) });
}
var subjectRoleSystem = "https://www.ehealth.fgov.be/standards/fhir/core/CodeSystem/cd-hcparty";
var subjectRoleDisplay = "";
switch (subjectRoleCode)
{
case "persphysician":
subjectRoleDisplay = "arts";
break;
case "persnurse":
subjectRoleDisplay = "verpleegkundige";
break;
case "persmidwife":
subjectRoleDisplay = "vroedvrouw";
break;
case "persclinicalbiologist":
subjectRoleDisplay = "klinisch bioloog";
break;
case "perspharmacist":
subjectRoleDisplay = "apotheker";
break;
case "persdentist":
subjectRoleDisplay = "tandarts";
break;
default:
subjectRoleDisplay = "onbekend";
break;
}
var purposeOfUseSystem = "http://terminology.hl7.org/CodeSystem/v3-ActReason";
var purposeOfUseCode = "PATADMIN";
var purposeOfUseDisplay = "Patient administration";
// configuration data - Patient search query
//var patientSearchQueryString = Prompt.Input<string>("Patient search query: ", defaultValue: "identifier=urn:oid:1.2.840.114350.1.13.0.1.7.5.737384.14|203713");
if (!patientSelected)
{
if (fhirRepositoryConfig.FhirEndpoint == "https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/")
{
patientIdentifier = Prompt.Input<string>("EPIC Patient identifier: ", defaultValue: "203713");
patientIdentifierSystem = "urn:oid:1.2.840.114350.1.13.0.1.7.5.737384.14";
}
else
{
patientIdentifier = Prompt.Input<string>("Patient INSZ: ", defaultValue: "01482105560");
}
}
var typeFHIRQuery = Prompt.Select("Type FHIR query", new[] { "Allergien", "Problemen/Antecedenten", "Custom" });
var fhirResourceName = "";
var defaultQueryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
switch (typeFHIRQuery)
{
case "Allergien":
fhirResourceName = "AllergyIntolerance";
//defaultQueryString.Add("patient:Patient.organization:Organization.identifier", subjectOrganizationIdentifierSystem + "|" + subjectOrganizationIdentifierValue);
defaultQueryString.Add("patient:Patient.identifier", patientIdentifierSystem + "|" + patientIdentifier);
defaultQueryString.Add("_include:iterate", "PractitionerRole:practitioner");
defaultQueryString.Add("_include:iterate", "PractitionerRole:organization");
defaultQueryString.Add("_include:iterate", "Patient:organization");
defaultQueryString.Add("_include", "AllergyIntolerance:recorder");
defaultQueryString.Add("_include", "AllergyIntolerance:patient");
break;
case "Problemen/Antecedenten":
fhirResourceName = "Condition";
//defaultQueryString.Add("patient:Patient.organization:Organization.identifier", subjectOrganizationIdentifierSystem + "|" + subjectOrganizationIdentifierValue);
defaultQueryString.Add("patient:Patient.identifier", patientIdentifierSystem + "|" + patientIdentifier);
defaultQueryString.Add("_include:iterate", "PractitionerRole:practitioner");
defaultQueryString.Add("_include:iterate", "PractitionerRole:organization");
defaultQueryString.Add("_include:iterate", "Patient:organization");
defaultQueryString.Add("_include", "Condition:asserter");
defaultQueryString.Add("_include", "Condition:patient");
break;
case "Custom":
fhirResourceName = Prompt.Input<string>("Resource: ", defaultValue: "");
break;
}
// Add specific query parameters for FHIRStation debugging
if ((fhirRepositoryConfig.FhirEndpoint.EndsWith("/fhirstation-rest/api/fhir")) ||
(fhirRepositoryConfig.FhirEndpoint.EndsWith("/fhirstation-rest/api/fhir/")))
{
defaultQueryString.Add("_AMARON_tracing", "true");
defaultQueryString.Add("_AMARON_tracing_payload", "true");
}
var defaultSearchQueryString = System.Web.HttpUtility.UrlDecode(defaultQueryString.ToString());
if (fhirResourceName != "")
{
Console.WriteLine("Path = /" + fhirResourceName);
}
var searchQueryString = Prompt.Input<string>("Search query string: ", defaultValue: defaultSearchQueryString);
WriteToLog(baseFilePath, correlationId, "Search query string: " + searchQueryString);
WriteToLog(baseFilePath, correlationId, "");
// END configuration data
var searchQueryNameValueCollection = System.Web.HttpUtility.ParseQueryString(searchQueryString);
var queryParameters = new List<string>();
for (int i = 0; i < searchQueryNameValueCollection.Count; i++)
{
var parameterName = searchQueryNameValueCollection.GetKey(i);
if (searchQueryNameValueCollection.GetValues(i) != null)
{
string[] parameterValues = searchQueryNameValueCollection.GetValues(i)!;
foreach (string paramValue in parameterValues)
{
queryParameters.Add(parameterName + "=" + paramValue);
}
}
}
var tokenClient = new HttpClient(new LoggingHandler(new HttpClientHandler(), baseFilePath, correlationId));
var now = DateTimeOffset.UtcNow;
var issuedAtDateTime = now.AddSeconds(-10);
// Belangrijk voor EPIC - expiration date moet minder dan 5 minuten zijn !
// https://fhir.epic.com/Resources/jwt_auth_troubleshoot_eof
var expires = now.AddMinutes(2);
long lIat = (long)Convert.ToDouble(issuedAtDateTime.ToUnixTimeSeconds().ToString());
long lExp = (long)Convert.ToDouble(expires.ToUnixTimeSeconds().ToString());
var subjectRole = new
{
system = subjectRoleSystem,
code = subjectRoleCode,
display = subjectRoleDisplay
};
object[] subjectRoles = new object[1];
subjectRoles[0] = subjectRole;
var purposeOfUse = new
{
system = purposeOfUseSystem,
code = purposeOfUseCode,
display = purposeOfUseDisplay
};
object[] purposeOfUses = new object[1];
purposeOfUses[0] = purposeOfUse;
var iheIUA = new
{
person_id = patientIdentifierSystem + "|" + patientIdentifier,
national_provider_identifier = userIdentifierSystem + "|" + userIdentifierValue,
subject_name = subjectName,
subject_organization = subjectOrganization,
subject_organization_id = subjectOrganizationIdentifierSystem + "|" + subjectOrganizationIdentifierValue,
subject_role = subjectRoles,
purpose_of_use = purposeOfUses,
};
var extensions = new { ihe_iua = iheIUA };
var claimsPayload = new Dictionary<string, object>()
{
{"iss", fhirRepositoryConfig.OauthClientId},
{"aud", fhirRepositoryConfig.OauthTokenEndpoint},
{"sub", fhirRepositoryConfig.OauthClientId},
{"iat", lIat},
{"exp", lExp},
{"jti", Guid.NewGuid().ToString()},
{"extensions", extensions}
};
var clientAssertion = CreateAuthenticationJWT(claimsPayload, fhirRepositoryConfig);
TokenResponse? tokenResponse = null;
if (clientAssertion != null)
{
WriteToLog(baseFilePath, correlationId, "Generated client_assertion (JWT): " + clientAssertion);
var cli = new ClientAssertion();
cli.Value = clientAssertion;
cli.Type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
tokenResponse = await tokenClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
ClientCredentialStyle = ClientCredentialStyle.PostBody,
Address = fhirRepositoryConfig.OauthTokenEndpoint,
ClientId = fhirRepositoryConfig.OauthClientId,
ClientAssertion = cli,
Scope = fhirRepositoryConfig.OauthScopes,
});
}
using (var handler = new HttpClientEventHandler())
{
var settings = new FhirClientSettings
{
PreferredFormat = ResourceFormat.Json,
Timeout = 30000,
};
using (FhirClient client = new FhirClient(fhirRepositoryConfig.FhirEndpoint, settings: settings, messageHandler: handler))
{
// use strict serializer (https://docs.fire.ly/projects/Firely-NET-SDK/en/stable/client/setup.html#fhirclient-communication-options)
client.WithStrictSerializer();
handler.OnBeforeRequest += async (sender, e) =>
{
if (tokenResponse != null)
{
e.RawRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse.AccessToken);
}
// Generate a Provenance FHIR resource - to send in a header
var provenance = new Provenance();
provenance.Id = Guid.NewGuid().ToString();
var resReference = new ResourceReference();
resReference.Display = "Search operation executed by an end user";
provenance.Target.Add(resReference);
provenance.Occurred = new FhirDateTime(DateTime.Now);
provenance.Recorded = new FhirDateTime(DateTime.Now).ToDateTimeOffset(TimeSpan.Zero);
provenance.Policy = new List<string> { "https://www.cozo.be/ehealthic" };
var reasonCode = new CodeableConcept().Add(purposeOfUseSystem, purposeOfUseCode, purposeOfUseDisplay);
reasonCode.Text = purposeOfUseDisplay;
provenance.Reason.Add(reasonCode);
var agentComponent = new Provenance.AgentComponent();
agentComponent.Type = new CodeableConcept().Add("http://terminology.hl7.org/CodeSystem/provenance-participant-type", "enterer", "");
var roleCode = new CodeableConcept().Add(subjectRoleSystem, subjectRoleCode, subjectRoleDisplay);
roleCode.Text = subjectRoleDisplay;
agentComponent.Role.Add(roleCode);
var resReferenceWho = new ResourceReference();
resReferenceWho.Type = "Practitioner";
resReferenceWho.Identifier = new Identifier();
resReferenceWho.Identifier.System = userIdentifierSystem;
resReferenceWho.Identifier.Value = userIdentifierValue;
resReferenceWho.Display = subjectName;
agentComponent.Who = resReferenceWho;
var resReferenceOnBehalfOf = new ResourceReference();
resReferenceOnBehalfOf.Type = "Organization";
resReferenceOnBehalfOf.Identifier = new Identifier();
resReferenceOnBehalfOf.Identifier.System = subjectOrganizationIdentifierSystem;
resReferenceOnBehalfOf.Identifier.Value = subjectOrganizationIdentifierValue;
resReferenceOnBehalfOf.Display = subjectOrganization;
agentComponent.OnBehalfOf = resReferenceOnBehalfOf;
provenance.Agent.Add(agentComponent);
var FhirJsonSerializer = new FhirJsonSerializer();
var jsProvenance = await FhirJsonSerializer.SerializeToStringAsync(provenance);
WriteToLog(baseFilePath, correlationId, "Generated Provenance resource: " + jsProvenance);
e.RawRequest.Headers.Add("X-Provenance", jsProvenance);
// LOG http request
WriteToLog(baseFilePath, correlationId, "");
WriteToLog(baseFilePath, correlationId, new string('-', 80));
WriteToLog(baseFilePath, correlationId, "HTTP Request:");
WriteToLog(baseFilePath, correlationId, e.RawRequest.ToString());
if (e.RawRequest.Content != null)
{
WriteToLog(baseFilePath, correlationId, await e.RawRequest.Content.ReadAsStringAsync());
}
WriteToLog(baseFilePath, correlationId, "");
};
handler.OnAfterResponse += async (sender, e) =>
{
// LOG http response
WriteToLog(baseFilePath, correlationId, "Received HTTP response from FHIR endpoint " + fhirRepositoryConfig.FhirEndpoint);
WriteToLog(baseFilePath, correlationId, e.RawResponse.ToString());
if (e.RawResponse.Content != null)
{
string httpResponseBody = await e.RawResponse.Content.ReadAsStringAsync();
//WriteToLog(baseFilePath, correlationId, e.RawResponse.Content.ReadAsStringAsync());
WriteToLog(baseFilePath, correlationId, httpResponseBody);
}
WriteToLog(baseFilePath, correlationId, new string('-', 80));
WriteToLog(baseFilePath, correlationId, "");
};
try
{
var results = await client.SearchUsingPostAsync(fhirResourceName, queryParameters.ToArray(), pageSize: fhirRepositoryConfig.MaxResultsPerPage);
if (results != null)
{
WriteToLog(baseFilePath, correlationId, "Formatted JSON response:");
WriteToLog(baseFilePath, correlationId, "");
WriteToLog(baseFilePath, correlationId, JToken.Parse(results.ToJson()).ToString(Formatting.Indented));
await FetchConditionsPractitionerRole(client, results);
while (results != null)
{
results = await client.ContinueAsync(results);
if (results != null)
{
WriteToLog(baseFilePath, correlationId, "Next page fetched");
WriteToLog(baseFilePath, correlationId, JToken.Parse(results.ToJson()).ToString(Formatting.Indented));
await FetchConditionsPractitionerRole(client, results);
}
}
}
}
catch (Exception foe)
{
WriteToLog(baseFilePath, correlationId, "Unable to execute FHIR query - " + foe);
}
}
}
Console.WriteLine("\r\n");
Console.WriteLine("Gedetailleerde logging is te vinden in bestand " + baseFilePath + @"\logs\" + correlationId + ".txt");
Console.WriteLine("Press any key to stop...");
Console.ReadKey();
}
private static async Task<bool> FetchConditionsPractitionerRole(FhirClient client, Hl7.Fhir.Model.Bundle resultBundle)
{
var practitionerRoleFetched = false;
var conditionResourcePresent = false;
foreach (var e in resultBundle.Entry)
{
if (e.Resource != null)
{
if (e.Resource.TypeName == "Condition")
{
var condition_entry = (Condition)e.Resource;
if ((condition_entry != null) &&
(condition_entry.Recorder != null) &&
(condition_entry.Recorder.ReferenceElement != null))
{
conditionResourcePresent = true;
try
{
if ((condition_entry.Meta != null) && (condition_entry.Meta.Source != null) && (condition_entry.Meta.Source != ""))
{
var defaultQueryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
var sourceMetaField = condition_entry.Meta.Source;
if (sourceMetaField.Contains('#'))
{
sourceMetaField = sourceMetaField.Substring(0, sourceMetaField.IndexOf('#'));
}
defaultQueryString.Add("_source", sourceMetaField);
var recorderResourceIdRef = condition_entry.Recorder.ReferenceElement.Value;
var recorderResourceIdRefItems = recorderResourceIdRef.Split("/");
defaultQueryString.Add("_id", recorderResourceIdRefItems[1]);
var searchQueryString = System.Web.HttpUtility.UrlDecode(defaultQueryString.ToString());
if (searchQueryString != null)
{
var searchQueryNameValueCollection = System.Web.HttpUtility.ParseQueryString(searchQueryString);
var queryParameters = new List<string>();
for (int i = 0; i < searchQueryNameValueCollection.Count; i++)
{
var parameterName = searchQueryNameValueCollection.GetKey(i);
if (searchQueryNameValueCollection.GetValues(i) != null)
{
string[] parameterValues = searchQueryNameValueCollection.GetValues(i)!;
foreach (string paramValue in parameterValues)
{
queryParameters.Add(parameterName + "=" + paramValue);
}
}
}
var results = await client.SearchUsingPostAsync("PractitionerRole", queryParameters.ToArray());
if (results != null)
{
foreach (var re in results.Entry)
{
if (re.Resource != null)
{
if (re.Resource.TypeName == "PractitionerRole")
{
practitionerRoleFetched = true;
}
}
}
}
}
}
else
{
var refRecorder = condition_entry.Recorder.Url;
Console.WriteLine("Fetching condition recorder (PractitionerRole) " + refRecorder);
var practitionerRole = await client.ReadAsync<PractitionerRole>(refRecorder);
if (practitionerRole != null)
{
practitionerRoleFetched = true;
}
}
}
catch (Exception foe)
{
Console.WriteLine("Unable to fetch PractitionerRole - " + foe);
}
}
}
}
}
if ((conditionResourcePresent) && (!practitionerRoleFetched))
{
Console.WriteLine("ERROR - Unable to fetch PractitionerRole");
}
return practitionerRoleFetched;
}
public static void WriteToLog(string baseFilePath, string correlationId, object messageToLog)
{
Console.WriteLine(messageToLog);
var logPath = baseFilePath + @"\logs";
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
File.AppendAllText(logPath + @"\" + correlationId + ".txt", messageToLog + Environment.NewLine);
}
public static string? CreateAuthenticationJWT(Dictionary<string, object> claimsPayload, FhirRepositoryConfig fhirRepositoryConfig)
{
RSAParameters rsaParams;
string jwkSetUrl = fhirRepositoryConfig.JwkSetUrl;
string jwkSetKeyId = fhirRepositoryConfig.JwkSetKeyId;
if ((fhirRepositoryConfig.RsaPrivateKeyPEMEncoded != null) && (fhirRepositoryConfig.RsaPrivateKeyPEMEncoded != ""))
{
string privateRsaKey = fhirRepositoryConfig.RsaPrivateKeyPEMEncoded;
using (var tr = new StringReader(privateRsaKey))
{
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(tr);
var keyPair = pemReader.ReadObject() as AsymmetricCipherKeyPair;
if (keyPair == null)
{
throw new Exception("Could not read RSA private key");
}
var privateRsaParams = keyPair.Private as RsaPrivateCrtKeyParameters;
rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);
}
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParams);
var jwtHeaders = new Dictionary<string, object>();
jwtHeaders.Add("typ", "JWT");
jwtHeaders.Add("kid", jwkSetKeyId);
jwtHeaders.Add("jku", jwkSetUrl);
String encodedJWT = Jose.JWT.Encode(claimsPayload, rsa, Jose.JwsAlgorithm.RS256, jwtHeaders);
return encodedJWT;
}
}
else
{
if ((fhirRepositoryConfig.KeystoreFile != null) && (fhirRepositoryConfig.KeystoreFile != ""))
{
string keystoreFilePassword = null!;
if ((fhirRepositoryConfig.KeystoreFilePassword != null) && (fhirRepositoryConfig.KeystoreFilePassword != ""))
{
keystoreFilePassword = fhirRepositoryConfig.KeystoreFilePassword;
}
else
{
keystoreFilePassword = Prompt.Password("Keystore file password: ");
}
Org.BouncyCastle.Pkcs.Pkcs12Store pkcs = new Org.BouncyCastle.Pkcs.Pkcs12StoreBuilder().Build();
using (System.IO.Stream stream = new System.IO.FileStream(fhirRepositoryConfig.KeystoreFile, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read))
{
if (keystoreFilePassword != null)
{
pkcs.Load(stream, keystoreFilePassword.ToCharArray());
}
else
{
pkcs.Load(stream, "".ToCharArray());
}
} // End Using stream
Org.BouncyCastle.Pkcs.AsymmetricKeyEntry keyEntry = null!;
// Belgische ehealth keystores hebben een 'authentication' alias
if (pkcs.ContainsAlias("authentication"))
{
keyEntry = pkcs.GetKey("authentication");
}
else
{
foreach (string alias in pkcs.Aliases)
{
if (pkcs.IsKeyEntry((string)alias))
{
keyEntry = pkcs.GetKey(alias);
}
}
}
if (keyEntry != null)
{
Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey = keyEntry.Key;
var privateRsaParams = privateKey as RsaPrivateCrtKeyParameters;
rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParams);
var jwtHeaders = new Dictionary<string, object>();
jwtHeaders.Add("typ", "JWT");
jwtHeaders.Add("kid", jwkSetKeyId);
jwtHeaders.Add("jku", jwkSetUrl);
String encodedJWT = Jose.JWT.Encode(claimsPayload, rsa, Jose.JwsAlgorithm.RS256, jwtHeaders);
return encodedJWT;
}
}
}
return null;
}
}
}