Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ValidateLegalId.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
8
10{
12 {
13 public ValidateLegalId()
14 : base("/ValidateLegalId")
15 {
16 }
17
18 public override bool HandlesSubPaths => false;
19 public override bool UserSessions => true;
20 public bool AllowsPOST => true;
21
22 public async Task POST(HttpRequest Request, HttpResponse Response)
23 {
24 Gateway.AssertUserAuthenticated(Request, "Admin.Legal.Validate.ID");
25
26 if (!Request.HasData)
27 throw new BadRequestException();
28
29 object Obj = await Request.DecodeDataAsync();
30 if (!(Obj is Dictionary<string, object> RequestObj) ||
31 !RequestObj.TryGetValue("LegalId", out Obj) || !(Obj is string LegalId) ||
32 !RequestObj.TryGetValue("Purpose", out Obj) || !(Obj is string Purpose) ||
33 !RequestObj.TryGetValue("Password", out Obj) || !(Obj is string Password) ||
34 !RequestObj.TryGetValue("TabID", out Obj) || !(Obj is string TabID))
35 {
36 throw new BadRequestException();
37 }
38
39 if (LegalId.IndexOf('@') < 0)
40 throw new BadRequestException("Invalid legal identity.");
41
42 if (Gateway.ContractsClient is null)
43 throw new ServiceUnavailableException("Broker not connected to a legal service.");
44
45 Dictionary<string, object> ResponseObj;
46
47 try
48 {
50 ResponseObj = await this.Encode(Identity);
51 }
52 catch (Exception)
53 {
54 string PetitionId = Guid.NewGuid().ToString();
55
56 Purpose = Purpose.Trim();
57 if (string.IsNullOrEmpty(Purpose))
58 Purpose = "Validating Legal Identity through web interface at " + Gateway.Domain;
59
60 if (!await Gateway.PetitionLegalIdentity(LegalId, PetitionId, Purpose, Password, async (_, e) =>
61 {
62 await ClientEvents.PushEvent(new string[] { TabID }, "PetitionResponseReceived", JSON.Encode(new Dictionary<string, object>()
63 {
64 { "PetitionId", e.PetitionId },
65 { "PetitionResponse", e.Response },
66 { "Identity", await this.Encode(e.RequestedIdentity) },
67 { "Declined", e.RequestedIdentity is null && !(e.Message is null) },
68 { "Timeout", e.Message is null }
69 }, false), true);
70 }, TimeSpan.FromMinutes(5)))
71 {
72 throw new ForbiddenException("Unable to sign petition. Either password is incorrect, or no legal identity assigned to broker.");
73 }
74
75 ResponseObj = new Dictionary<string, object>()
76 {
77 { "Id", LegalId },
78 { "Petition", true },
79 { "PetitionId", PetitionId },
80 { "Message", "The information has been petitioned from the owner. If the owner accepts to share the information, it will be displayed here when it arrives." }
81 };
82 }
83
84 await Response.Return(ResponseObj);
85 }
86
87 private async Task<Dictionary<string, object>> Encode(LegalIdentity Identity)
88 {
89 if (Identity is null)
90 return null;
91
92 IdentityStatus Status = await Gateway.ContractsClient.ValidateAsync(Identity);
93 Dictionary<string, object> Properties = new Dictionary<string, object>();
94 Dictionary<string, object> ResponseObj = new Dictionary<string, object>()
95 {
96 { "Id", Identity.Id },
97 { "IdUri", Identity.IdUriString },
98 { "Petition", false },
99 { "ClientKeyName", Identity.ClientKeyName },
100 { "ClientPubKey", Identity.HasClientPublicKey ? Convert.ToBase64String(Identity.ClientPubKey) : null },
101 { "ClientSignature", Identity.HasClientSignature ? Convert.ToBase64String(Identity.ClientSignature) : null },
102 { "Created", Identity.Created },
103 { "From", Identity.From },
104 { "To", Identity.To },
105 { "HasClientPublicKey", Identity.HasClientPublicKey },
106 { "HasClientSignature", Identity.HasClientSignature },
107 { "Provider", Identity.Provider },
108 { "ServerSignature", Convert.ToBase64String(Identity.ServerSignature) },
109 { "State", Identity.State.ToString() },
110 { "Updated", Identity.Updated },
111 { "ValidationStatus", Status.ToString() },
112 { "Properties", Properties }
113 };
114
115 foreach (Property P in Identity.Properties)
116 Properties[P.Name] = P.Value;
117
118 if (!(Identity.Attachments is null))
119 {
120 List<Dictionary<string, object>> Attachments = new List<Dictionary<string, object>>();
121
122 foreach (Attachment Attachment in Identity.Attachments)
123 {
124 Attachments.Add(new Dictionary<string, object>()
125 {
126 { "Id", Attachment.Id },
127 { "ContentType", Attachment.ContentType },
128 { "FileName", Attachment.FileName },
129 { "Url", Attachment.Url },
130 { "Signature", Convert.ToBase64String(Attachment.Signature) },
131 { "Timestamp", Attachment.Timestamp },
132 });
133 }
134
135 ResponseObj["Attachments"] = Attachments.ToArray();
136 }
137
138 return ResponseObj;
139 }
140
141 }
142}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
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
static Task< bool > PetitionLegalIdentity(string LegalId, string PetitionId, string Purpose, EventHandlerAsync< LegalIdentityPetitionResponseEventArgs > Callback, TimeSpan Timeout)
Petitions information about a legal identity from its owner.
Definition: Gateway.cs:4536
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
Definition: Gateway.cs:4375
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
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
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Contains a reference to an attachment assigned to a legal object.
Definition: Attachment.cs:9
byte[] Signature
Binary signature of the attachment, generated by an approved legal identity of the account-holder....
Definition: Attachment.cs:75
Task< LegalIdentity > GetLegalIdentityAsync(string LegalIdentityId)
Gets legal identity registered with the account.
Task< IdentityStatus > ValidateAsync(LegalIdentity Identity)
Validates a legal identity.
POST Interface for HTTP resources.
IdentityStatus
Validation Status of legal identity