Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PackageUrlValidator.cs
1using System;
3using System.Threading.Tasks;
5using Waher.Events;
8
10{
12 {
13 public PackageUrlValidator()
14 {
15 }
16
20 public override string DisplayName => "Package URL";
21
22 public override string[] GetChallenges(HttpRequest Request)
23 {
24 return Array.Empty<string>();
25 }
26
27 public override async Task<IUser> IsAuthenticated(HttpRequest Request)
28 {
29 string FileName = Request.SubPath;
30 if (FileName.StartsWith('/'))
31 FileName = FileName[1..];
32
33 if (!Request.Header.TryGetQueryParameter("s", out string SignatureStr) ||
34 !Request.Header.TryGetQueryParameter("p", out string PublishedStr) ||
35 !XML.TryParse(PublishedStr, out DateTime Published))
36 {
37 Log.Error("Attempting to access package without valid signature or published date.",
38 FileName, Request.RemoteEndPoint);
39 return null;
40 }
41
42 Package Package = await ProvisioningComponent.GetPackage(FileName);
43
44 if (Package is null)
45 {
46 Log.Error("Attempting to access package that does not exist.",
47 FileName, Request.RemoteEndPoint);
48 return null;
49 }
50
51 string Expected = XML.Encode(Package.Published);
52 string Requested = XML.Encode(Published);
53
54 if (Expected != Requested)
55 {
56 Log.Error("Attempting to access package with invalid publishing timestamp.",
57 FileName, Request.RemoteEndPoint,
58 new KeyValuePair<string, object>("Expected", Expected),
59 new KeyValuePair<string, object>("Requested", Requested));
60
61 return null;
62 }
63
64 Expected = Package.SignatureBase64Url;
65
66 if (string.Compare(Expected, SignatureStr, true) != 0)
67 {
68 Log.Error("Attempting to access package with invalid signature.",
69 FileName, Request.RemoteEndPoint,
70 new KeyValuePair<string, object>("Expected", Expected),
71 new KeyValuePair<string, object>("Requested", SignatureStr));
72
73 return null;
74 }
75
76 Log.Informational("Access to package authorized.", FileName, Request.RemoteEndPoint);
77
78 return new PackageUser(Package);
79 }
80
81 private class PackageUser : IUser
82 {
83 private readonly Package package;
84
85 public PackageUser(Package Package)
86 {
87 this.package = Package;
88 }
89
90 public string UserName => this.package.FileName;
91 public string FederatedUserName => this.package.FileName;
92 public string FriendlyName => this.package.FileName;
93 public string PasswordHash => throw new InvalidOperationException();
94 public string PasswordHashType => throw new InvalidOperationException();
95
96 public bool HasPrivilege(string Privilege)
97 {
98 return false;
99 }
100 }
101 }
102}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:692
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:344
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
bool TryGetQueryParameter(string QueryParameter, out string Value)
Tries to get the value of an individual query parameter, if available.
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:243
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
Contains information about a software package.
Definition: Package.cs:21
string SignatureBase64Url
Base64-encoded version of Signature.
Definition: Package.cs:67
CaseInsensitiveString FileName
Filename of package.
Definition: Package.cs:43
DateTime Published
When package was published.
Definition: Package.cs:79
override string DisplayName
Display name for authentication scheme.
override async Task< IUser > IsAuthenticated(HttpRequest Request)
Checks if the request is authorized.
override string[] GetChallenges(HttpRequest Request)
Gets available challenges for the authenticating client to respond to.
Basic interface for a user.
Definition: IUser.cs:7