Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateAccount.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
7
9{
11 {
12 public CreateAccount()
13 : base("/CreateAccount")
14 {
15 }
16
17 public override bool HandlesSubPaths => false;
18 public override bool UserSessions => true;
19 public bool AllowsPOST => true;
20
21 public async Task POST(HttpRequest Request, HttpResponse Response)
22 {
23 Gateway.AssertUserAuthenticated(Request, "Admin.Broker.Accounts");
24
25 if (!Request.HasData)
26 throw new BadRequestException();
27
28 object Obj = await Request.DecodeDataAsync();
29 if (!(Obj is Dictionary<string, string> Form))
30 throw new BadRequestException();
31
32 CaseInsensitiveString UserName;
33 string Password;
36
37 if (!Form.ContainsKey("UserName") || CaseInsensitiveString.IsNullOrEmpty(UserName = Form["UserName"]))
38 throw new BadRequestException();
39
40 if (!Form.ContainsKey("Password") || string.IsNullOrEmpty(Password = Form["Password"]))
41 throw new BadRequestException();
42
43 if (!Form.ContainsKey("EMail") || CaseInsensitiveString.IsNullOrEmpty(EMail = Form["EMail"]))
44 throw new BadRequestException();
45
46 if (!Form.ContainsKey("PhoneNr") || CaseInsensitiveString.IsNullOrEmpty(PhoneNr = Form["PhoneNr"]))
47 PhoneNr = string.Empty;
48
49 DataStorage.PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new DataStorage.PersistenceLayer();
50 KeyValuePair<IAccount, string[]> P = await PersistenceLayer.CreateAccount(string.Empty, UserName, Password, EMail, PhoneNr, Request.RemoteEndPoint);
51 IAccount Account = P.Key ?? throw new ConflictException(GetAlterntivesHeaders(P.Value));
52
53 throw new SeeOtherException("/Account.md?UserName=" + Account.UserName); // PRG pattern.
54 }
55
56 internal static KeyValuePair<string, string>[] GetAlterntivesHeaders(string[] Alternatives)
57 {
58 int i, c = Alternatives?.Length ?? 0;
59 KeyValuePair<string, string>[] Suggestions = new KeyValuePair<string, string>[c];
60
61 for (i = 0; i < c; i++)
62 Suggestions[i] = new KeyValuePair<string, string>("X-AlternativeName" + (i + 1).ToString(), Alternatives[i]);
63
64 return Suggestions;
65 }
66
67 }
68}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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:3041
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:18
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:195
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
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...
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.
async 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