Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RequestWhiteList.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
8using Waher.Events;
12using Waher.Security;
13
15{
20 {
21 internal static string whiteListKey = null;
22 private readonly XmppServer xmppServer;
23
29 : base("/RequestWhiteList")
30 {
31 this.xmppServer = XmppServer;
32 }
33
37 public bool AllowsGET => true;
38
42 public override bool HandlesSubPaths => false;
43
47 public override bool UserSessions => false;
48
55 public async Task GET(HttpRequest Request, HttpResponse Response)
56 {
57 if (!Request.Header.TryGetQueryParameter("Sender", out string Sender) ||
58 !Request.Header.TryGetQueryParameter("Receiver", out string Receiver) ||
59 !Request.Header.TryGetQueryParameter("Expires", out string Expires) ||
60 !Request.Header.TryGetQueryParameter("MAC", out string MAC) ||
61 !Expires.StartsWith("P") ||
62 !long.TryParse(Expires.Substring(1), out long Ticks))
63 {
64 Log.Warning("White-list request link not valid.", this.ResourceName, Request.RemoteEndPoint,
65 new KeyValuePair<string, object>("URL", Request.Header.GetURL(true)));
66
67 throw new BadRequestException("Invalid request.");
68 }
69
70 StringBuilder sb = new StringBuilder();
71 sb.Append(Sender);
72 sb.Append(" | ");
73 sb.Append(Receiver);
74 sb.Append(" | ");
75 sb.Append(Ticks);
76
77 if (whiteListKey is null)
78 {
79 string Key = await RuntimeSettings.GetAsync("WhiteList.Key", string.Empty);
80 if (string.IsNullOrEmpty(Key))
81 {
82 Key = Convert.ToBase64String(XmppServer.GetRandomNumbers(32));
83 RuntimeSettings.Set("WhiteList.Key", Key);
84 }
85
86 whiteListKey = Key;
87 }
88
89 if (string.Compare(MAC, Hashes.ComputeHMACSHA256HashString(System.Convert.FromBase64String(RequestWhiteList.whiteListKey),
90 Encoding.UTF8.GetBytes(sb.ToString())), true) != 0)
91 {
92 Log.Warning("White-list request link MAC not valid.", Receiver, Sender,
93 new KeyValuePair<string, object>("Sender", Sender),
94 new KeyValuePair<string, object>("Receiver", Receiver),
95 new KeyValuePair<string, object>("Expires", Ticks),
96 new KeyValuePair<string, object>("MAC", MAC));
97
98 throw new BadRequestException("Invalid MAC in request.");
99 }
100
101 StringBuilder Markdown = new StringBuilder();
102
103 Markdown.AppendLine("Title: Requesting White-Listing");
104 Markdown.AppendLine("Description: Page allows mail-users to request white-listing.");
105 Markdown.AppendLine("Author: Peter Waher");
106 Markdown.AppendLine("Copyright: Copyright.md");
107 Markdown.AppendLine("Master: Master.md");
108 Markdown.AppendLine();
109 Markdown.AppendLine("=========");
110 Markdown.AppendLine();
111 Markdown.AppendLine("Approval request");
112 Markdown.AppendLine("=========================");
113 Markdown.AppendLine();
114
115 if (Ticks < DateTime.Now.Ticks)
116 {
117 Log.Notice("White-list request link old.", Receiver, Sender,
118 new KeyValuePair<string, object>("Sender", Sender),
119 new KeyValuePair<string, object>("Receiver", Receiver),
120 new KeyValuePair<string, object>("Expires", Expires),
121 new KeyValuePair<string, object>("MAC", MAC));
122
123 Markdown.AppendLine("The request link is no longer valid.");
124 }
125 else
126 {
127 await this.xmppServer.Presence("subscribe", string.Empty, new XmppAddress(Receiver), new XmppAddress(Sender), string.Empty, string.Empty, this.xmppServer);
128
129 StringBuilder Markdown2 = new StringBuilder();
130
131 Markdown2.AppendLine("Request sent");
132 Markdown2.AppendLine("==============");
133 Markdown2.AppendLine();
134
135 Markdown2.Append("A request has been sent to **");
136 Markdown2.Append(MarkdownDocument.Encode(Receiver));
137 Markdown2.Append("** to approve messages from **");
138 Markdown2.Append(MarkdownDocument.Encode(Sender));
139 Markdown2.AppendLine("**. You will be notified when an response to the request is provided.");
140
141 await this.xmppServer.SendMailMessage(Receiver, Sender, "White-listing request sent", Markdown2.ToString());
142
143 Log.Notice("White-listing request mail sent.", Sender, Receiver);
144
145 Markdown.Append("A request has been sent to `");
146 Markdown.Append(Receiver);
147 Markdown.Append("` to approve that e - mail messages from `");
148 Markdown.Append(Sender);
149 Markdown.AppendLine("` be forwarded. You will be notified by e-mail when a response is available.");
150 }
151
152 MarkdownSettings Settings = new MarkdownSettings();
153 if (Types.TryGetModuleParameter("Root", out object Obj) && Obj is string Root)
154 Settings.RootFolder = Root;
155
156 MarkdownDocument Doc = await MarkdownDocument.CreateAsync(Markdown.ToString(), Settings,
157 Path.Combine(Settings.RootFolder, "RequestWhiteList.md"), string.Empty,
158 Request.Header.GetURL(false, false));
159 string Html = await Doc.GenerateHTML();
160
161 Response.ContentType = HtmlCodec.DefaultContentType;
162 await Response.Write(Html);
163 }
164 }
165}
166
167
HTML encoder/decoder.
Definition: HtmlCodec.cs:13
const string DefaultContentType
Default Content-Type for HTML: text/html
Definition: HtmlCodec.cs:24
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
async Task< string > GenerateHTML()
Generates HTML from the markdown text.
static Task< MarkdownDocument > CreateAsync(string MarkdownText, params Type[] TransparentExceptionTypes)
Contains a markdown document. This markdown document class supports original markdown,...
Contains settings that the Markdown parser uses to customize its behavior.
string RootFolder
File system root folder. If file references are absolute, and this property is provided,...
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Definition: Log.cs:566
static void Notice(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a notice event.
Definition: Log.cs:450
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
bool TryGetQueryParameter(string QueryParameter, out string Value)
Tries to get the value of an individual query parameter, if available.
string GetURL()
Gets an absolute URL for the request.
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:195
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Web resource for requesting white-list authentication of a client.
override bool UserSessions
If user sessions are required
RequestWhiteList(XmppServer XmppServer)
Web resource for requesting white-list authentication of a client.
override bool HandlesSubPaths
If subpaths are handled
async Task GET(HttpRequest Request, HttpResponse Response)
HTTP GET method is called.
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
static byte[] GetRandomNumbers(int NrBytes)
Generates a set of random numbers.
Definition: XmppServer.cs:672
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Static class managing persistent settings.
static bool Set(string Key, string Value)
Sets a string-valued setting.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string ComputeHMACSHA256HashString(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:574
GET Interface for HTTP resources.