Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RequestAccount.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
7using Waher.Events;
12
14{
16 {
17 public RequestAccount()
18 : base("/RequestAccount")
19 {
20 }
21
22 public override bool HandlesSubPaths => false;
23 public override bool UserSessions => true;
24 public bool AllowsPOST => true;
25
26 public async Task POST(HttpRequest Request, HttpResponse Response)
27 {
28 if (!Request.HasData)
29 {
30 await Response.SendResponse(new BadRequestException());
31 return;
32 }
33
34 ContentResponse Content = await Request.DecodeDataAsync();
35 if (Content.HasError || !(Content.Decoded is Dictionary<string, string> Form))
36 {
37 await Response.SendResponse(new BadRequestException());
38 return;
39 }
40
41 string Name;
43 string LinkedIn;
44 string Organization;
45 string Where;
46 string TimeLimit;
47 string Text;
48 string RecaptchaResponse;
49
50 if (!Form.ContainsKey("Name") || string.IsNullOrEmpty(Name = Form["Name"]))
51 {
52 await Response.SendResponse(new BadRequestException("Name missing."));
53 return;
54 }
55
56 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
57 {
58 await Response.SendResponse(new BadRequestException("E-mail missing."));
59 return;
60 }
61
62 if (!Form.ContainsKey("LinkedIn") || string.IsNullOrEmpty(LinkedIn = Form["LinkedIn"]))
63 {
64 await Response.SendResponse(new BadRequestException("LinkedIn link missing."));
65 return;
66 }
67
68 if (!Form.ContainsKey("Organization") || string.IsNullOrEmpty(Organization = Form["Organization"]))
69 {
70 await Response.SendResponse(new BadRequestException("Organization missing."));
71 return;
72 }
73
74 if (!Form.ContainsKey("Where") || string.IsNullOrEmpty(Where = Form["Where"]))
75 {
76 await Response.SendResponse(new BadRequestException("Where reference missing."));
77 return;
78 }
79
80 if (!Form.ContainsKey("TimeLimit") || string.IsNullOrEmpty(TimeLimit = Form["TimeLimit"]))
81 TimeLimit = string.Empty;
82
83 if (!Form.ContainsKey("Text") || string.IsNullOrEmpty(Text = Form["Text"]))
84 {
85 await Response.SendResponse(new BadRequestException("Reason missing."));
86 return;
87 }
88
89 if (!Form.ContainsKey("g-recaptcha-response") || string.IsNullOrEmpty(RecaptchaResponse = Form["g-recaptcha-response"]))
90 {
91 await Response.SendResponse(new BadRequestException());
92 return;
93 }
94
95 if (await Feedback.SiteVerify(RecaptchaResponse, Request.RemoteEndPoint))
96 {
97 KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(Request.RemoteEndPoint,
98 new KeyValuePair<string, object>("Name", Name),
99 new KeyValuePair<string, object>("EMail", EMail.Value),
100 new KeyValuePair<string, object>("LinkedIn", LinkedIn),
101 new KeyValuePair<string, object>("Organization", Organization),
102 new KeyValuePair<string, object>("Where", Where),
103 new KeyValuePair<string, object>("TimeLimit", TimeLimit),
104 new KeyValuePair<string, object>("Text", Text),
105 new KeyValuePair<string, object>("RemoteEndPoint", Request.RemoteEndPoint));
106
107 Dictionary<string, object> Feedback = new Dictionary<string, object>();
108 foreach (KeyValuePair<string, object> P in Tags)
109 Feedback.Add(P.Key, P.Value);
110
111 Request.Session["Feedback"] = Feedback;
112
113 StringBuilder Markdown = new StringBuilder();
114 DateTime Now = DateTime.Now;
115
116 Markdown.AppendLine("Account requested.");
117 Markdown.AppendLine();
118
119 Markdown.AppendLine("| Account request ||");
120 Markdown.AppendLine("|:------|:-------|");
121
122 Markdown.Append("| Name: | ");
123 Markdown.Append(MarkdownDocument.Encode(Name));
124 Markdown.AppendLine(" |");
125
126 Markdown.Append("| e-Mail: | <a href=\"mailto:");
127 Markdown.Append(EMail.Replace("\"", "&quot;"));
128 Markdown.Append("\" target=\"_blank\">");
129 Markdown.Append(MarkdownDocument.Encode(EMail));
130 Markdown.AppendLine("</a> |");
131
132 Markdown.Append("| LinkedIn: | <a href=\"");
133 Markdown.Append(LinkedIn.Replace("\"", "&quot;"));
134 Markdown.Append("\" target=\"_blank\">");
135 Markdown.Append(MarkdownDocument.Encode(LinkedIn));
136 Markdown.AppendLine("</a> |");
137
138 Markdown.Append("| Organization: | ");
139 Markdown.Append(MarkdownDocument.Encode(Organization));
140 Markdown.AppendLine(" |");
141
142 Markdown.Append("| Where: | ");
143 Markdown.Append(MarkdownDocument.Encode(Where));
144 Markdown.AppendLine(" |");
145
146 Markdown.Append("| Time Limit: | ");
147 Markdown.Append(MarkdownDocument.Encode(TimeLimit));
148 Markdown.AppendLine(" |");
149
151
152 Markdown.Append("| Date | ");
153 Markdown.Append(MarkdownDocument.Encode(Now.ToShortDateString()));
154 Markdown.AppendLine(" |");
155 Markdown.Append("| Time | ");
156 Markdown.Append(MarkdownDocument.Encode(Now.ToLongTimeString()));
157 Markdown.AppendLine(" |");
158
159 Markdown.AppendLine();
160 Markdown.AppendLine();
161 Markdown.AppendLine("```");
162 Markdown.AppendLine(Text.Replace("```", " ` ` ` "));
163 Markdown.AppendLine("```");
164
165 await LoginAuditor.AppendWhoIsInfo(Markdown, Request.RemoteEndPoint);
166
168 await IoTGateway.Gateway.SendNotification(Markdown.ToString());
169 else
170 Log.Notice(Markdown.ToString(), Tags);
171
172 await Response.SendResponse(new SeeOtherException("/AccountRequested.md")); // PRG pattern.
173 }
174 else
175 await Response.SendResponse(new ForbiddenException(Request, "Request not verified. Bot?"));
176 }
177 }
178}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
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 ...
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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:460
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
Represents an HTTP request.
Definition: HttpRequest.cs:22
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:243
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
SessionVariables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:212
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
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...
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The response to the request can be found under a different URI and SHOULD be retrieved using a GET me...
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
CaseInsensitiveString Replace(CaseInsensitiveString oldValue, CaseInsensitiveString newValue)
Returns a new string in which all occurrences of a specified string in the current instance are repla...
Class that monitors login events, and help applications determine malicious intent....
Definition: LoginAuditor.cs:26
static async Task< string > AppendWhoIsInfo(StringBuilder Markdown, string RemoteEndPoint)
Appends WHOIS information to a Markdown document.
static async Task< KeyValuePair< string, object >[]> Annotate(string RemoteEndPoint, params KeyValuePair< string, object >[] Tags)
Annotates a remote endpoint.
Provides the user with options to control notifications from the Broker.
static BrokerNotificationConfiguration Instance
Current instance of configuration.
bool AccountRequested
If a notification should be sent when an account has been requested.
static async Task< bool > SiteVerify(string RecaptchaResponse, string RemoteEndPoint)
Allows web pages and web services to verify that Google reCaptcha responses are valid.
Definition: Feedback.cs:166
bool AllowsPOST
If the POST method is allowed.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
static async Task AppendRemoteEndPointToTable(StringBuilder Markdown, string RemoteEndPoint)
Appends annotated information about a remote endpoint to a Markdown table.
POST Interface for HTTP resources.