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;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
10
12{
14 {
15 public InstallPackage()
16 : base("/InstallPackage")
17 {
18 }
19
20 public override bool HandlesSubPaths => false;
21 public override bool UserSessions => true;
22 public bool AllowsPOST => true;
23
24 public async Task POST(HttpRequest Request, HttpResponse Response)
25 {
26 Gateway.AssertUserAuthenticated(Request, "Admin.Software.Upload");
27
28 if (!Request.HasData || !(await Request.DecodeDataAsync() is Dictionary<string, object> Obj) ||
29 !Obj.TryGetValue("package", out object Value) || !(Value is string FileName) ||
30 !Obj.TryGetValue("key", out Value) || !(Value is string KeyStr))
31 {
32 throw new BadRequestException();
33 }
34
35 if (KeyStr.Length != 76 && KeyStr.Length != 76 + 32)
36 throw new BadRequestException("Invalid key");
37
38 byte[] PublicKey = Convert.FromBase64String(KeyStr.Substring(0, 76));
39 byte[] AesKey = KeyStr.Length == 76 ? null : Hashes.StringToBinary(KeyStr.Substring(76));
40
41 string FullFileName = Path.Combine(XmppServerModule.PackagesFolder, FileName);
42 if (!File.Exists(FullFileName))
43 throw new NotFoundException("Package file not found.");
44
45 Package Package = await Provisioning.ProvisioningComponent.GetPackage(FileName);
46 if (Package is null)
47 throw new NotFoundException("Package not found.");
48
50 throw new BadRequestException("Reserved package.");
51
52 using (FileStream fs = File.OpenRead(FullFileName))
53 {
54 if (!XmppServerModule.ValidatePackage(fs, PublicKey, Package.Signature))
55 throw new BadRequestException("Invalid public key");
56 }
57
58 Package.Installed = Package.Published;
59 Package.PublicKey = PublicKey;
60 Package.AesKey = AesKey;
61 Package.ContentOnly = XmppServerModule.IsContentPackage(Package);
62
63 await Database.Update(Package);
64
65 await XmppServerModule.Instance.UpdateSoftware(Package, true);
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...
Represents an HTTP request.
Definition: HttpRequest.cs:18
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 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:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
Identity of the IoT Broker package.
Definition: BrokerPackage.cs:7
const string FileName
IoTBroker.package
Contains information about a software package.
Definition: Package.cs:18
byte[] Signature
Cryptographic signature of package, as calculated by the issuer of the package.
Definition: Package.cs:46
CaseInsensitiveString FileName
Filename of package.
Definition: Package.cs:40
DateTime Published
When package was published.
Definition: Package.cs:76
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.
POST Interface for HTTP resources.