Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ResendVerificationCodes.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
15
17{
22 {
27 : base("Account/ResendVerificationCodes",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(Create).Namespace + ".JSON.ResendVerificationCodes.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(Create).Namespace + ".XML.ResendVerificationCodes.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
46 await this.CheckBlocks(Request);
47
48 AccountUser User = AssertUserAuthenticated(Request, false);
49
50 string EMail = (string)Parameters["PEMail"]?.AssociatedObjectValue;
51 string PhoneNr = (string)Parameters["PPhoneNr"]?.AssociatedObjectValue;
52 string Language = (string)Parameters["PLanguage"].AssociatedObjectValue;
53
54 if (!string.IsNullOrEmpty(PhoneNr) &&
55 !Create.InternationalNumberFormat.IsMatch(PhoneNr))
56 {
57 await Response.SendResponse(new BadRequestException("Phone numbers must be provided using the international number format, starting with +, then the country code, then the number."));
58 return;
59 }
60
62
63 if (Account.Enabled)
64 {
65 LoginAuditor.Fail("User tried to resend codes for an account that is already enabled.", User.UserName, Request.RemoteEndPoint, "HTTPS");
66 await Response.SendResponse(new BadRequestException("Account already enabled. No verification code available to resend."));
67 return;
68 }
69
70 if (!string.IsNullOrEmpty(PhoneNr) && PhoneNr != Account.PhoneNr)
71 {
72 LoginAuditor.Fail("User tried to change phone number when attempting to resend codes.", User.UserName, Request.RemoteEndPoint, "HTTPS");
73 await Response.SendResponse(new ForbiddenException(Request, "Not permitted to change phone number."));
74 return;
75 }
76
77 if (!string.IsNullOrEmpty(EMail) && EMail != Account.EMail)
78 {
79 LoginAuditor.Fail("User tried to change e-mail address when attempting to resend codes.", User.UserName, Request.RemoteEndPoint, "HTTPS");
80 await Response.SendResponse(new ForbiddenException(Request, "Not permitted to change e-mail address."));
81 return;
82 }
83
84 if (!string.IsNullOrEmpty(PhoneNr) && Account.PhoneNrVerified.HasValue)
85 {
86 LoginAuditor.Fail("User tried to resend phone number verification code when phone number is already verified.", User.UserName, Request.RemoteEndPoint, "HTTPS");
87 await Response.SendResponse(new BadRequestException("Phone number has already been verified. No verification code available to resend."));
88 return;
89 }
90
91 if (!string.IsNullOrEmpty(EMail) && Account.EMailVerified.HasValue)
92 {
93 LoginAuditor.Fail("User tried to resend e-mail address verification code when e-mail address is already verified.", User.UserName, Request.RemoteEndPoint, "HTTPS");
94 await Response.SendResponse(new BadRequestException("E-Mail has already been verified. No verification code available to resend."));
95 return;
96 }
97
98 string OnboardingDomainName = await LegalComponent.GetOnboardingNeuronDomainName();
99
100 if (!string.IsNullOrEmpty(EMail))
101 {
103 new Uri("https://" + OnboardingDomainName + "/ID/SendVerificationMessage.ws"),
104 new Dictionary<string, object>()
105 {
106 { "EMail", EMail },
107 { "Resend", true },
108 { "Language", Language }
109 },
110 new KeyValuePair<string, string>("Accept", JsonCodec.DefaultContentType));
111
113 {
114 await Response.SendResponse(ContentResponse.Error);
115 return;
116 }
117
118 if (!(ContentResponse.Decoded is Dictionary<string, object> EMailResult))
119 {
120 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification mail. Unexpected result"));
121 return;
122 }
123
124 if (!EMailResult.TryGetValue("Status", out object Obj) || !(Obj is bool Status) || !Status)
125 {
126 if (EMailResult.TryGetValue("Message", out Obj) && Obj is string Message)
127 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification mail. Error reported: " + Message));
128 else
129 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification mail. Check e-mail address provided."));
130
131 return;
132 }
133 }
134
136 {
138 new Uri("https://" + OnboardingDomainName + "/ID/SendVerificationMessage.ws"),
139 new Dictionary<string, object>()
140 {
141 { "Nr", PhoneNr },
142 { "Resend", true },
143 { "Language", Language }
144 },
145 new KeyValuePair<string, string>("Accept", JsonCodec.DefaultContentType));
146
148 {
149 await Response.SendResponse(ContentResponse.Error);
150 return;
151 }
152
153 if (!(ContentResponse.Decoded is Dictionary<string, object> SmsResult))
154 {
155 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification mail. Unexpected result"));
156 return;
157 }
158
159 if (!SmsResult.TryGetValue("Status", out object Obj) || !(Obj is bool Status2) || !Status2)
160 {
161 if (SmsResult.TryGetValue("Message", out Obj) && Obj is string Message)
162 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification SMS. Error reported: " + Message));
163 else
164 await Response.SendResponse(new ServiceUnavailableException("Unable to send verification SMS. Check phone number provided."));
165
166 return;
167 }
168 }
169
170 await Response.Return(new NamedDictionary<string, object>("AckResponse", AgentNamespace));
171 }
172 }
173}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > PostAsync(Uri Uri, object Data, params KeyValuePair< string, string >[] Headers)
Posts to a resource, using a Uniform Resource Identifier (or Locator).
const string DefaultContentType
application/json
Definition: JsonCodec.cs:20
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
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
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...
Task Return(Exception ex)
Returns an error to the client.
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Represents a case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
Class that monitors login events, and help applications determine malicious intent....
Definition: LoginAuditor.cs:26
static void Fail(string Message, string UserName, string RemoteEndPoint, string Protocol)
Handles a failed login attempt.
Contains information about a broker account.
Definition: Account.cs:41
DateTime? PhoneNrVerified
When Phone Number was verified.
Definition: Account.cs:344
CaseInsensitiveString EMail
E-mail address associated with account.
Definition: Account.cs:176
bool Enabled
If account is enabled
Definition: Account.cs:354
DateTime? EMailVerified
When e-Mail was verified.
Definition: Account.cs:335
CaseInsensitiveString PhoneNr
Phone number associated with account.
Definition: Account.cs:185
static readonly Regex InternationalNumberFormat
International phone number format.
Definition: Create.cs:47
Resends verification codes for an account being onboarded on the server.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
ResendVerificationCodes()
Resends verification codes for an account being onboarded on the server.
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
async Task CheckBlocks(HttpRequest Request)
Checks if the client is blocked.