Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InstallPackage.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Content;
11
13{
15 {
16 public InstallPackage()
17 : base("/InstallPackage")
18 {
19 }
20
21 public override bool HandlesSubPaths => false;
22 public override bool UserSessions => true;
23 public bool AllowsPOST => true;
24
25 public async Task POST(HttpRequest Request, HttpResponse Response)
26 {
27 Gateway.AssertUserAuthenticated(Request, "Admin.Software.Upload");
28
29 if (!Request.HasData)
30 {
31 await Response.SendResponse(new BadRequestException());
32 return;
33 }
34
35 ContentResponse Content = await Request.DecodeDataAsync();
36 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Obj) ||
37 !Obj.TryGetValue("package", out object Value) || !(Value is string FileName) ||
38 !Obj.TryGetValue("key", out Value) || !(Value is string KeyStr))
39 {
40 await Response.SendResponse(new BadRequestException());
41 return;
42 }
43
44 if (KeyStr.Length != 76 && KeyStr.Length != 76 + 32)
45 {
46 await Response.SendResponse(new BadRequestException("Invalid key"));
47 return;
48 }
49
50 byte[] PublicKey = Convert.FromBase64String(KeyStr[..76]);
51 byte[] AesKey = KeyStr.Length == 76 ? null : Hashes.StringToBinary(KeyStr[76..]);
52
53 string FullFileName = Path.Combine(XmppServerModule.PackagesFolder, FileName);
54 if (!File.Exists(FullFileName))
55 {
56 await Response.SendResponse(new NotFoundException("Package file not found."));
57 return;
58 }
59
60 Package Package = await Provisioning.ProvisioningComponent.GetPackage(FileName);
61 if (Package is null)
62 {
63 await Response.SendResponse(new NotFoundException("Package not found."));
64 return;
65 }
66
68 {
69 await Response.SendResponse(new BadRequestException("Reserved package."));
70 return;
71 }
72
73 using (FileStream fs = File.OpenRead(FullFileName))
74 {
75 if (!XmppServerModule.ValidatePackage(fs, PublicKey, Package.Signature))
76 {
77 await Response.SendResponse(new BadRequestException("Invalid public key"));
78 return;
79 }
80 }
81
82 Package.Installed = Package.Published;
83 Package.PublicKey = PublicKey;
84 Package.AesKey = AesKey;
85 Package.ContentOnly = XmppServerModule.IsContentPackage(Package);
86
87 await Database.Update(Package);
88
89 await XmppServerModule.Instance.UpdateSoftware(Package, true);
90 }
91 }
92}
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...
Represents an HTTP request.
Definition: HttpRequest.cs:22
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...
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
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:100
Identity of the IoT Broker package.
Definition: BrokerPackage.cs:7
const string FileName
IoTBroker.package
Contains information about a software package.
Definition: Package.cs:21
byte[] Signature
Cryptographic signature of package, as calculated by the issuer of the package.
Definition: Package.cs:49
CaseInsensitiveString FileName
Filename of package.
Definition: Package.cs:43
DateTime Published
When package was published.
Definition: Package.cs:79
bool AllowsPOST
If the POST method is allowed.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
static bool IsContentPackage(Package Package)
Checks if a Package is a Content-Only package.
POST Interface for HTTP resources.