Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UpdateApiKey.cs
2using System.Threading.Tasks;
3using Waher.Content;
9
11{
13 {
14 public UpdateApiKey()
15 : base("/UpdateApiKey")
16 {
17 }
18
19 public override bool HandlesSubPaths => false;
20 public override bool UserSessions => true;
21 public bool AllowsPOST => true;
22
23 public async Task POST(HttpRequest Request, HttpResponse Response)
24 {
25 Gateway.AssertUserAuthenticated(Request, "Admin.Broker.Keys");
26
27 if (!Request.HasData)
28 {
29 await Response.SendResponse(new BadRequestException());
30 return;
31 }
32
33 ContentResponse Content = await Request.DecodeDataAsync();
34 if (Content.HasError || !(Content.Decoded is Dictionary<string, string> Form))
35 {
36 await Response.SendResponse(new BadRequestException());
37 return;
38 }
39
40 string Key;
41 string Owner;
43
44 if (!Form.ContainsKey("Key") || string.IsNullOrEmpty(Key = Form["Key"]))
45 {
46 await Response.SendResponse(new BadRequestException());
47 return;
48 }
49
50 if (!Form.ContainsKey("Owner") || string.IsNullOrEmpty(Owner = Form["Owner"]))
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("MaxAccounts") || !long.TryParse(Form["MaxAccounts"], out long MaxAccounts) || MaxAccounts < 1)
63 {
64 await Response.SendResponse(new BadRequestException());
65 return;
66 }
67
68 ApiKey ApiKey = await Database.FindFirstDeleteRest<ApiKey>(new FilterFieldEqualTo("Key", Key));
69 if (ApiKey is null)
70 {
71 await Response.SendResponse(new NotFoundException());
72 return;
73 }
74
75 ApiKey.Owner = Owner;
76 ApiKey.EMail = EMail;
77 ApiKey.MaxAccounts = MaxAccounts;
78
79 await Database.Update(ApiKey);
80
81 if (!(Request.Header.Referer is null))
82 await Response.SendResponse(new SeeOtherException(Request.Header.Referer.Value)); // PRG pattern.
83 }
84 }
85}
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...
HttpFieldReferer Referer
Referer HTTP Field header. (RFC 2616, §14.36)
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
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 server has not found anything matching the Request-URI. No indication is given of whether the con...
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 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
This filter selects objects that have a named field equal to a given value.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: UpdateApiKey.cs:23
bool AllowsPOST
If the POST method is allowed.
Definition: UpdateApiKey.cs:21
POST Interface for HTTP resources.