Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Feedback.cs
1using System;
2using System.Collections.Generic;
3using System.Net.Http;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
10
12{
14 {
15 public Feedback()
16 : base("/Feedback")
17 {
18 }
19
20 public override bool HandlesSubPaths => false;
21 public override bool UserSessions => true;
22 public bool AllowsPOST => true;
23
24 public async Task POST(HttpRequest Request, HttpResponse Response)
25 {
26 if (!Request.HasData)
27 throw new BadRequestException();
28
29 object Obj = await Request.DecodeDataAsync();
30 if (!(Obj is Dictionary<string, string> Form))
31 throw new BadRequestException();
32
33 string Name;
34 string EMail;
35 string Reason;
36 string Text;
37 string RecaptchaResponse;
38 bool ResponseDesired;
39
40 if (!Form.ContainsKey("Name") || string.IsNullOrEmpty(Name = Form["Name"]?.Trim()))
41 throw new BadRequestException();
42
43 if (!Form.ContainsKey("EMail") || string.IsNullOrEmpty(EMail = Form["EMail"]?.Trim()))
44 throw new BadRequestException();
45
46 if (!Form.ContainsKey("Reason") || string.IsNullOrEmpty(Reason = Form["Reason"]?.Trim()))
47 throw new BadRequestException();
48
49 if (!Form.ContainsKey("Text") || string.IsNullOrEmpty(Text = Form["Text"]?.Trim()))
50 throw new BadRequestException();
51
52 if (!Form.ContainsKey("g-recaptcha-response") || string.IsNullOrEmpty(RecaptchaResponse = Form["g-recaptcha-response"]))
53 throw new BadRequestException();
54
55 if (!Form.ContainsKey("ResponseDesired"))
56 ResponseDesired = false;
57 else if (!CommonTypes.TryParse(Form["ResponseDesired"], out ResponseDesired))
58 throw new BadRequestException();
59
60 if (await SiteVerify(RecaptchaResponse, Request.RemoteEndPoint))
61 {
62 KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(Request.RemoteEndPoint,
63 new KeyValuePair<string, object>("Name", Name),
64 new KeyValuePair<string, object>("EMail", EMail),
65 new KeyValuePair<string, object>("Reason", Reason),
66 new KeyValuePair<string, object>("Text", Text),
67 new KeyValuePair<string, object>("ResponseDesired", ResponseDesired),
68 new KeyValuePair<string, object>("RemoteEndPoint", Request.RemoteEndPoint));
69
70 Dictionary<string, object> Feedback = new Dictionary<string, object>();
71 foreach (KeyValuePair<string, object> P in Tags)
72 Feedback.Add(P.Key, P.Value);
73
74 Request.Session["Feedback"] = Feedback;
75
76 StringBuilder Markdown = new StringBuilder();
77 DateTime Now = DateTime.Now;
78
79 Markdown.AppendLine("Feedback received.");
80 Markdown.AppendLine();
81
82 Markdown.AppendLine("| Feedback ||");
83 Markdown.AppendLine("|:------|:-------|");
84
85 Markdown.Append("| Name: | ");
86 Markdown.Append(MarkdownDocument.Encode(Name));
87 Markdown.AppendLine(" |");
88
89 Markdown.Append("| e-Mail: | <a href=\"mailto:");
90 Markdown.Append(EMail.Replace("\"", "&quot;"));
91 Markdown.Append("\" target=\"_blank\">");
92 Markdown.Append(MarkdownDocument.Encode(EMail));
93 Markdown.AppendLine("</a> |");
94
95 Markdown.Append("| Reason: | ");
96 Markdown.Append(MarkdownDocument.Encode(Reason));
97 Markdown.AppendLine(" |");
98
99 Markdown.Append("| Response desired: | ");
100 if (ResponseDesired)
101 Markdown.Append(":white_check_mark:");
102 else
103 Markdown.Append(":negative_squared_cross_mark:");
104 Markdown.AppendLine(" |");
105
107
108 Markdown.Append("| Date | ");
109 Markdown.Append(MarkdownDocument.Encode(Now.ToShortDateString()));
110 Markdown.AppendLine(" |");
111 Markdown.Append("| Time | ");
112 Markdown.Append(MarkdownDocument.Encode(Now.ToLongTimeString()));
113 Markdown.AppendLine(" |");
114
115 Markdown.AppendLine();
116 Markdown.AppendLine();
117 Markdown.AppendLine("```");
118 Markdown.AppendLine(Text.Replace("```", " ` ` ` "));
119 Markdown.AppendLine("```");
120
121 await LoginAuditor.AppendWhoIsInfo(Markdown, Request.RemoteEndPoint);
122
123 await IoTGateway.Gateway.SendNotification(Markdown.ToString());
124
125 throw new SeeOtherException("/FeedbackSent.md"); // PRG pattern.
126 }
127 else
128 throw new ForbiddenException("Request not verified. Bot?");
129 }
130
137 internal static async Task<bool> SiteVerify(string RecaptchaResponse, string RemoteEndPoint)
138 {
139 using (HttpClient WebClient = new HttpClient())
140 {
141 Dictionary<string, string> VerificationForm = new Dictionary<string, string>()
142 {
143 { "secret", "6LcHNh0UAAAAAI1DwveBT21M4IkSRc5i-QciPUvg" }, // TODO: Make configurable.
144 { "response", RecaptchaResponse },
145 { "remoteip", RemoteEndPoint }
146 };
147
148 HttpContent Content = new FormUrlEncodedContent(VerificationForm);
149 HttpResponseMessage ResponseMsg = await WebClient.PostAsync("https://www.google.com/recaptcha/api/siteverify", Content);
150 if (!ResponseMsg.IsSuccessStatusCode)
151 throw new ServiceUnavailableException();
152
153 string Json = await ResponseMsg.Content.ReadAsStringAsync();
154 return (JSON.Parse(Json) is Dictionary<string, object> Parsed && Parsed.ContainsKey("success") && Parsed["success"] is bool Success && Success);
155 }
156 }
157
158 }
159}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static object Parse(string Json)
Parses a JSON string.
Definition: JSON.cs:43
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 ...
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:18
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:195
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
Variables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:164
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
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...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
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.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: Feedback.cs:24
bool AllowsPOST
If the POST method is allowed.
Definition: Feedback.cs:22
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.