Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PersonalDataConfiguration.cs
1using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
7using Waher.Events;
14
16{
21 {
22 private static PersonalDataConfiguration instance = null;
23 private static IProcessingActivity[] processingActivities = null;
24 private HttpResource consent = null;
25
26 private string informationHash = string.Empty;
27 private bool consented = false;
28 private DateTime consentedTimestamp = DateTime.MinValue;
29
33 public static PersonalDataConfiguration Instance => instance;
34
38 public static IProcessingActivity[] ProcessingActivities => processingActivities;
39
43 [DefaultValueStringEmpty]
44 public string InformationHash
45 {
46 get => this.informationHash;
47 set => this.informationHash = value;
48 }
49
53 [DefaultValue(false)]
54 public bool Consented
55 {
56 get => this.consented;
57 set => this.consented = value;
58 }
59
63 [DefaultValueDateTimeMinValue]
64 public DateTime ConsentedTimestamp
65 {
66 get => this.consentedTimestamp;
67 set => this.consentedTimestamp = value;
68 }
69
73 public override string Resource => "/Settings/PersonalData.md";
74
78 public override int Priority => 100;
79
85 public override Task<string> Title(Language Language)
86 {
87 return Language.GetStringAsync(typeof(Gateway), 4, "Personal Data");
88 }
89
93 public override Task ConfigureSystem()
94 {
95 return Task.CompletedTask;
96 }
97
102 public override void SetStaticInstance(ISystemConfiguration Configuration)
103 {
104 instance = Configuration as PersonalDataConfiguration;
105 }
106
111 public override async Task InitSetup(HttpServer WebServer)
112 {
113 await base.InitSetup(WebServer);
114
115 this.consent = WebServer.Register("/Settings/Consent", null, this.Consent, true, false, true);
116
117 List<IProcessingActivity> Activities = new List<IProcessingActivity>();
118
119 foreach (Type T in Types.GetTypesImplementingInterface(typeof(IProcessingActivity)))
120 {
121 if (T.IsAbstract || T.IsInterface || T.IsGenericTypeDefinition)
122 continue;
123
124 try
125 {
126 Activities.Add((IProcessingActivity)Types.Instantiate(T));
127 }
128 catch (Exception ex)
129 {
130 Log.Exception(ex, T.FullName);
131 }
132 }
133
134 Activities.Sort((A1, A2) =>
135 {
136 int i = A1.Priority - A2.Priority;
137 if (i != 0)
138 return i;
139
140 return A1.GetType().FullName.CompareTo(A2.GetType().FullName);
141 });
142
143 processingActivities = Activities.ToArray();
144
145 /* Removed since it might affect remote updates.
146
147 StringBuilder sb = new StringBuilder();
148
149 foreach (IProcessingActivity A in processingActivities)
150 {
151 string FileName = Path.Combine(Gateway.RootFolder, "Settings", "PersonalData", A.TransparentInformationMarkdownFileName);
152
153 try
154 {
155 string Markdown = await Files.ReadAllTextAsync(FileName);
156 sb.AppendLine(Markdown);
157 }
158 catch (Exception ex)
159 {
160 Log.Exception(ex, A.TransparentInformationMarkdownFileName);
161 }
162 }
163
164 string Hash = Security.Hashes.ComputeSHA256HashString(Encoding.UTF8.GetBytes(sb.ToString()));
165
166 if (Hash != this.informationHash || (!this.consented && this.Complete))
167 {
168 this.InformationHash = Hash;
169 this.consented = false;
170 this.consentedTimestamp = DateTime.MinValue;
171 this.Complete = false;
172 this.Completed = DateTime.MinValue;
173 this.Updated = DateTime.Now;
174
175 await Database.Update(this);
176 }*/
177 }
178
183 public override Task UnregisterSetup(HttpServer WebServer)
184 {
185 WebServer.Unregister(this.consent);
186
187 return base.UnregisterSetup(WebServer);
188 }
189
193 protected override string ConfigPrivilege => "Admin.Legal.PersonalData";
194
195 private async Task Consent(HttpRequest Request, HttpResponse Response)
196 {
197 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
198
199 if (!Request.HasData)
200 {
201 await Response.SendResponse(new BadRequestException());
202 return;
203 }
204
205 ContentResponse Content = await Request.DecodeDataAsync();
206 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Parameters))
207 {
208 await Response.SendResponse(new BadRequestException());
209 return;
210 }
211
212 if (!Parameters.TryGetValue("consent", out object Obj) || !(Obj is bool Consent))
213 {
214 await Response.SendResponse(new BadRequestException());
215 return;
216 }
217
218 string TabID = Request.Header["X-TabID"];
219 if (string.IsNullOrEmpty(TabID))
220 {
221 await Response.SendResponse(new BadRequestException());
222 return;
223 }
224
225 if (this.consented != Consent)
226 {
227 this.consented = Consent;
228 this.consentedTimestamp = Consent ? DateTime.Now : DateTime.MinValue;
229
230 await Database.Update(this);
231
232 await ClientEvents.PushEvent(new string[] { TabID }, "ShowNext", JSON.Encode(new Dictionary<string, object>()
233 {
234 { "consent", Consent }
235 }, false), true, "User");
236 }
237
238 Response.StatusCode = 200;
239 Response.StatusMessage = "OK";
240 }
241
245 public const string GATEWAY_PII_CONSENT = nameof(GATEWAY_PII_CONSENT);
246
251 public override Task<bool> EnvironmentConfiguration()
252 {
253 if (!this.TryGetEnvironmentVariable(GATEWAY_PII_CONSENT, false, out this.consented) || !this.consented)
254 return Task.FromResult(false);
255
256 this.consentedTimestamp = DateTime.Now;
257 return Task.FromResult(true);
258 }
259
260 }
261}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
Provides information about personal data processing.
const string GATEWAY_PII_CONSENT
Environment variable name for personal data consent configuration.
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
static PersonalDataConfiguration Instance
Current instance of configuration.
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
override async Task InitSetup(HttpServer WebServer)
Initializes the setup object.
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
override Task ConfigureSystem()
Is called during startup to configure the system.
string InformationHash
Digest of transparent information presented to which consent is asked.
DateTime ConsentedTimestamp
When consent to personal data processing has been received.
static IProcessingActivity[] ProcessingActivities
Available processing activities.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
bool Consented
If consent to personal data processing has been received.
Abstract base class for system configurations.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Implements an HTTP server.
Definition: HttpServer.cs:41
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1568
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1743
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1454
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:85
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Interface for system configurations. The gateway will scan all module for system configuration classe...
Interface for Personal Data Processing Activities