Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateAccount.cs
2using System.Threading.Tasks;
3using Waher.Content;
8
10{
12 {
13 public CreateAccount()
14 : base("/CreateAccount")
15 {
16 }
17
18 public override bool HandlesSubPaths => false;
19 public override bool UserSessions => true;
20 public bool AllowsPOST => true;
21
22 public async Task POST(HttpRequest Request, HttpResponse Response)
23 {
24 Gateway.AssertUserAuthenticated(Request, "Admin.Broker.Accounts");
25
26 if (!Request.HasData)
27 {
28 await Response.SendResponse(new BadRequestException());
29 return;
30 }
31
32 ContentResponse Content = await Request.DecodeDataAsync();
33 if (Content.HasError || !(Content.Decoded is Dictionary<string, string> Form))
34 {
35 await Response.SendResponse(new BadRequestException());
36 return;
37 }
38
39 CaseInsensitiveString UserName;
40 string Password;
43
44 if (!Form.ContainsKey("UserName") || CaseInsensitiveString.IsNullOrEmpty(UserName = Form["UserName"]))
45 {
46 await Response.SendResponse(new BadRequestException());
47 return;
48 }
49
50 if (!Form.ContainsKey("Password") || string.IsNullOrEmpty(Password = Form["Password"]))
51 {
52 await Response.SendResponse(new BadRequestException());
53 return;
54 }
55
56 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
57 {
58 await Response.SendResponse(new BadRequestException());
59 return;
60 }
61
62 if (!Form.ContainsKey("PhoneNr") || CaseInsensitiveString.IsNullOrEmpty(PhoneNr = Form["PhoneNr"]))
63 PhoneNr = string.Empty;
64
65 DataStorage.PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new DataStorage.PersistenceLayer();
66 KeyValuePair<IAccount, string[]> P = await PersistenceLayer.CreateAccount(string.Empty, UserName, Password, EMail, PhoneNr, Request.RemoteEndPoint);
67 IAccount Account = P.Key;
68
69 if (Account is null)
70 {
71 await Response.SendResponse(new ConflictException(GetAlterntivesHeaders(P.Value)));
72 return;
73 }
74
75 await Response.SendResponse(new SeeOtherException("/Account.md?UserName=" + Account.UserName)); // PRG pattern.
76 }
77
78 internal static KeyValuePair<string, string>[] GetAlterntivesHeaders(string[] Alternatives)
79 {
80 int i, c = Alternatives?.Length ?? 0;
81 KeyValuePair<string, string>[] Suggestions = new KeyValuePair<string, string>[c];
82
83 for (i = 0; i < c; i++)
84 Suggestions[i] = new KeyValuePair<string, string>("X-AlternativeName" + (i + 1).ToString(), Alternatives[i]);
85
86 return Suggestions;
87 }
88
89 }
90}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The request could not be completed due to a conflict with the current state of the resource....
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
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.
static readonly CaseInsensitiveString Empty
Empty case-insensitive string
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Task< KeyValuePair< Networking.XMPP.Server.IAccount, string[]> > CreateAccount(string ApiKey, CaseInsensitiveString UserName, string Password, CaseInsensitiveString EMail, CaseInsensitiveString PhoneNr, string RemoteEndPoint)
Creates an account.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
bool AllowsPOST
If the POST method is allowed.
POST Interface for HTTP resources.
CaseInsensitiveString UserName
User Name
Definition: IAccount.cs:24
Interface for XMPP user accounts.
Definition: IAccount.cs:9
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.