Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RequestApiKey.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
7using Waher.Events;
12
14{
16 {
17 public RequestApiKey()
18 : base("/RequestApiKey")
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 Product;
46 string Number;
47 string Where;
48 string TimeLimit;
49 string RecaptchaResponse;
50
51 if (!Form.ContainsKey("Name") || string.IsNullOrEmpty(Name = Form["Name"]))
52 {
53 await Response.SendResponse(new BadRequestException("Name missing."));
54 return;
55 }
56
57 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
58 {
59 await Response.SendResponse(new BadRequestException("E-mail missing."));
60 return;
61 }
62
63 if (!Form.ContainsKey("LinkedIn") || string.IsNullOrEmpty(LinkedIn = Form["LinkedIn"]))
64 {
65 await Response.SendResponse(new BadRequestException("LinkedIn link missing."));
66 return;
67 }
68
69 if (!Form.ContainsKey("Organization") || string.IsNullOrEmpty(Organization = Form["Organization"]))
70 {
71 await Response.SendResponse(new BadRequestException("Organization missing."));
72 return;
73 }
74
75 if (!Form.ContainsKey("Product") || string.IsNullOrEmpty(Product = Form["Product"]))
76 {
77 await Response.SendResponse(new BadRequestException("Product missing."));
78 return;
79 }
80
81 if (!Form.ContainsKey("Number") || string.IsNullOrEmpty(Number = Form["Number"]))
82 {
83 await Response.SendResponse(new BadRequestException("Number of requested accounts missing."));
84 return;
85 }
86
87 if (!Form.ContainsKey("Where") || string.IsNullOrEmpty(Where = Form["Where"]))
88 {
89 await Response.SendResponse(new BadRequestException("Where reference missing."));
90 return;
91 }
92
93 if (!Form.ContainsKey("TimeLimit") || string.IsNullOrEmpty(TimeLimit = Form["TimeLimit"]))
94 TimeLimit = string.Empty;
95
96 if (!Form.ContainsKey("g-recaptcha-response") || string.IsNullOrEmpty(RecaptchaResponse = Form["g-recaptcha-response"]))
97 {
98 await Response.SendResponse(new BadRequestException());
99 return;
100 }
101
102 if (await Feedback.SiteVerify(RecaptchaResponse, Request.RemoteEndPoint))
103 {
104 KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(Request.RemoteEndPoint,
105 new KeyValuePair<string, object>("Name", Name),
106 new KeyValuePair<string, object>("EMail", EMail.Value),
107 new KeyValuePair<string, object>("LinkedIn", LinkedIn),
108 new KeyValuePair<string, object>("Organization", Organization),
109 new KeyValuePair<string, object>("Product", Product),
110 new KeyValuePair<string, object>("Number", Number),
111 new KeyValuePair<string, object>("Where", Where),
112 new KeyValuePair<string, object>("TimeLimit", TimeLimit),
113 new KeyValuePair<string, object>("RemoteEndPoint", Request.RemoteEndPoint));
114
115 Dictionary<string, object> Feedback = new Dictionary<string, object>();
116 foreach (KeyValuePair<string, object> P in Tags)
117 Feedback.Add(P.Key, P.Value);
118
119 Request.Session["Feedback"] = Feedback;
120
121 StringBuilder Markdown = new StringBuilder();
122 DateTime Now = DateTime.Now;
123
124 Markdown.AppendLine("API key requested.");
125 Markdown.AppendLine();
126
127 Markdown.AppendLine("| API Key request ||");
128 Markdown.AppendLine("|:------|:-------|");
129
130 Markdown.Append("| Name: | ");
131 Markdown.Append(MarkdownDocument.Encode(Name));
132 Markdown.AppendLine(" |");
133
134 Markdown.Append("| e-Mail: | <a href=\"mailto:");
135 Markdown.Append(EMail.Replace("\"", "&quot;"));
136 Markdown.Append("\" target=\"_blank\">");
137 Markdown.Append(MarkdownDocument.Encode(EMail));
138 Markdown.AppendLine("</a> |");
139
140 Markdown.Append("| LinkedIn: | <a href=\"");
141 Markdown.Append(LinkedIn.Replace("\"", "&quot;"));
142 Markdown.Append("\" target=\"_blank\">");
143 Markdown.Append(MarkdownDocument.Encode(LinkedIn));
144 Markdown.AppendLine("</a> |");
145
146 Markdown.Append("| Organization: | ");
147 Markdown.Append(MarkdownDocument.Encode(Organization));
148 Markdown.AppendLine(" |");
149
150 Markdown.Append("| Product: | ");
151 Markdown.Append(MarkdownDocument.Encode(Product));
152 Markdown.AppendLine(" |");
153
154 Markdown.Append("| Number: | ");
155 Markdown.Append(MarkdownDocument.Encode(Number));
156 Markdown.AppendLine(" |");
157
158 Markdown.Append("| Where: | ");
159 Markdown.Append(MarkdownDocument.Encode(Where));
160 Markdown.AppendLine(" |");
161
162 Markdown.Append("| Time Limit: | ");
163 Markdown.Append(MarkdownDocument.Encode(TimeLimit));
164 Markdown.AppendLine(" |");
165
167
168 Markdown.Append("| Date | ");
169 Markdown.Append(MarkdownDocument.Encode(Now.ToShortDateString()));
170 Markdown.AppendLine(" |");
171 Markdown.Append("| Time | ");
172 Markdown.Append(MarkdownDocument.Encode(Now.ToLongTimeString()));
173 Markdown.AppendLine(" |");
174
175 await LoginAuditor.AppendWhoIsInfo(Markdown, Request.RemoteEndPoint);
176
178 await IoTGateway.Gateway.SendNotification(Markdown.ToString());
179 else
180 Log.Notice(Markdown.ToString(), Tags);
181
182 await Response.SendResponse(new SeeOtherException("/ApiKeyRequested.md")); // PRG pattern.
183 }
184 else
185 await Response.SendResponse(new ForbiddenException(Request, "Request not verified. Bot?"));
186 }
187 }
188}
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 ApiKeyRequested
If a notification should be sent when an API key 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.