Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VerifyEMail.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
14
16{
21 {
25 public VerifyEMail()
26 : base("Account/VerifyEMail",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(VerifyEMail).Namespace + ".JSON.VerifyEMail.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(VerifyEMail).Namespace + ".XML.VerifyEMail.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
45 AccountUser User = AssertUserAuthenticated(Request, false);
46 string EMail = (string)Parameters["PEMail"].AssociatedObjectValue;
47 double Code = (double)Parameters["PCode"].AssociatedObjectValue;
48
49 if (User.Account.EMail != EMail)
50 {
51 await Response.SendResponse(new BadRequestException("E-mail mismatch."));
52 return;
53 }
54
55 if (User.Account.EMailVerified.HasValue)
56 {
57 await Response.SendResponse(new BadRequestException("E-mail already verified."));
58 return;
59 }
60
61 string OnboardingDomainName = await LegalComponent.GetOnboardingNeuronDomainName();
63 new Uri("https://" + OnboardingDomainName + "/ID/VerifyNumber.ws"),
64 new Dictionary<string, object>()
65 {
66 { "EMail", EMail },
67 { "Code", (int)Code }
68 },
69 new KeyValuePair<string, string>("Accept", JsonCodec.DefaultContentType));
70
72 {
73 await Response.SendResponse(ContentResponse.Error);
74 return;
75 }
76
77 if (!(ContentResponse.Decoded is Dictionary<string, object> Result))
78 {
79 await Response.SendResponse(new ServiceUnavailableException("Unable to verify mail. Unexpected result"));
80 return;
81 }
82
83 if (!Result.TryGetValue("Status", out object Obj) || !(Obj is bool Status) || !Status)
84 {
85 if (Result.TryGetValue("Message", out Obj) && Obj is string Message)
86 await Response.SendResponse(new BadRequestException("Unable to verify mail. Error reported: " + Message));
87 else
88 await Response.SendResponse(new ServiceUnavailableException("Unable to verify mail."));
89
90 return;
91 }
92
93 User.Account.EMailVerified = DateTime.UtcNow;
94
96 User.Account.PhoneNrVerified.HasValue)
97 {
98 User.Account.Enabled = true;
99 }
100
101 await Database.Update(User.Account);
102
103 await Response.Return(new NamedDictionary<string, object>("EMailVerified", AgentNamespace)
104 {
105 { "eMail", EMail },
106 { "userName", User.UserName }
107 });
108 }
109 }
110}
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...
Represents an HTTP request.
Definition: HttpRequest.cs:22
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 interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
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
DateTime? PhoneNrVerified
When Phone Number was verified.
Definition: Account.cs:344
CaseInsensitiveString EMail
E-mail address associated with account.
Definition: Account.cs:176
DateTime? EMailVerified
When e-Mail was verified.
Definition: Account.cs:335
CaseInsensitiveString PhoneNr
Phone number associated with account.
Definition: Account.cs:185
Verifies an e-mail address corresponding to an account.
Definition: VerifyEMail.cs:21
VerifyEMail()
Verifies an e-mail address corresponding to an account.
Definition: VerifyEMail.cs:25
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: VerifyEMail.cs:43
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