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;
3using System.Threading.Tasks;
4using Waher.Content;
9
11{
13 {
14 public ValidateLegalId()
15 : base("/ValidateLegalId")
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.Legal.Validate.ID");
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, object> RequestObj) ||
35 !RequestObj.TryGetValue("LegalId", out object Obj) || !(Obj is string LegalId) ||
36 !RequestObj.TryGetValue("Purpose", out Obj) || !(Obj is string Purpose) ||
37 !RequestObj.TryGetValue("Password", out Obj) || !(Obj is string Password) ||
38 !RequestObj.TryGetValue("TabID", out Obj) || !(Obj is string TabID))
39 {
40 await Response.SendResponse(new BadRequestException());
41 return;
42 }
43
44 if (LegalId.IndexOf('@') < 0)
45 {
46 await Response.SendResponse(new BadRequestException("Invalid legal identity."));
47 return;
48 }
49
50 if (Gateway.ContractsClient is null)
51 {
52 await Response.SendResponse(new ServiceUnavailableException("Broker not connected to a legal service."));
53 return;
54 }
55
56 Dictionary<string, object> ResponseObj;
57
58 try
59 {
61 ResponseObj = await this.Encode(Identity);
62 }
63 catch (Exception)
64 {
65 string PetitionId = Guid.NewGuid().ToString();
66
67 Purpose = Purpose.Trim();
68 if (string.IsNullOrEmpty(Purpose))
69 Purpose = "Validating Legal Identity through web interface at " + Gateway.Domain;
70
71 if (!await Gateway.PetitionLegalIdentity(LegalId, PetitionId, Purpose, Password, async (_, e) =>
72 {
73 await ClientEvents.PushEvent(new string[] { TabID }, "PetitionResponseReceived", JSON.Encode(new Dictionary<string, object>()
74 {
75 { "PetitionId", e.PetitionId },
76 { "PetitionResponse", e.Response },
77 { "Identity", await this.Encode(e.RequestedIdentity) },
78 { "Declined", e.RequestedIdentity is null && !(e.Message is null) },
79 { "Timeout", e.Message is null }
80 }, false), true);
81 }, TimeSpan.FromMinutes(5)))
82 {
83 await Response.SendResponse(new ForbiddenException(Request, "Unable to sign petition. Either password is incorrect, or no legal identity assigned to broker."));
84 return;
85 }
86
87 ResponseObj = new Dictionary<string, object>()
88 {
89 { "Id", LegalId },
90 { "Petition", true },
91 { "PetitionId", PetitionId },
92 { "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." }
93 };
94 }
95
96 await Response.Return(ResponseObj);
97 }
98
99 private async Task<Dictionary<string, object>> Encode(LegalIdentity Identity)
100 {
101 if (Identity is null)
102 return null;
103
105 IdentityStatus Status = e.Status;
106 Dictionary<string, object> Properties = new Dictionary<string, object>();
107 Dictionary<string, object> ResponseObj = new Dictionary<string, object>()
108 {
109 { "Id", Identity.Id },
110 { "IdUri", Identity.IdUriString },
111 { "Petition", false },
112 { "ClientKeyName", Identity.ClientKeyName },
113 { "ClientPubKey", Identity.HasClientPublicKey ? Convert.ToBase64String(Identity.ClientPubKey) : null },
114 { "ClientSignature", Identity.HasClientSignature ? Convert.ToBase64String(Identity.ClientSignature) : null },
115 { "Created", Identity.Created },
116 { "From", Identity.From },
117 { "To", Identity.To },
118 { "HasClientPublicKey", Identity.HasClientPublicKey },
119 { "HasClientSignature", Identity.HasClientSignature },
120 { "Provider", Identity.Provider },
121 { "ServerSignature", Convert.ToBase64String(Identity.ServerSignature) },
122 { "State", Identity.State.ToString() },
123 { "Updated", Identity.Updated },
124 { "ValidationStatus", Status.ToString() },
125 { "ValidationStatusTags", e.Tags },
126 { "Properties", Properties }
127 };
128
129 foreach (Property P in Identity.Properties)
130 Properties[P.Name] = P.Value;
131
132 if (!(Identity.Attachments is null))
133 {
134 List<Dictionary<string, object>> Attachments = new List<Dictionary<string, object>>();
135
136 foreach (Attachment Attachment in Identity.Attachments)
137 {
138 Attachments.Add(new Dictionary<string, object>()
139 {
140 { "Id", Attachment.Id },
141 { "ContentType", Attachment.ContentType },
142 { "FileName", Attachment.FileName },
143 { "Url", Attachment.Url },
144 { "Signature", Convert.ToBase64String(Attachment.Signature) },
145 { "Timestamp", Attachment.Timestamp },
146 });
147 }
148
149 ResponseObj["Attachments"] = Attachments.ToArray();
150 }
151
152 return ResponseObj;
153 }
154
155 }
156}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
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:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
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
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
Definition: Gateway.cs:5299
static Task< bool > PetitionLegalIdentity(string LegalId, string PetitionId, string Purpose, EventHandlerAsync< LegalIdentityPetitionResponseEventArgs > Callback, TimeSpan Timeout, HttpRequest Request)
Petitions information about a legal identity from its owner.
Definition: Gateway.cs:5458
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: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...
Task Return(Exception ex)
Returns an error to the client.
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:10
byte[] Signature
Binary signature of the attachment, generated by an approved legal identity of the account-holder....
Definition: Attachment.cs:76
Task< LegalIdentity > GetLegalIdentityAsync(string LegalIdentityId)
Gets legal identity registered with the account.
Task< IdentityValidationEventArgs > ValidateAsync(LegalIdentity Identity)
Validates a legal identity.
POST Interface for HTTP resources.
Definition: ImplTypes.g.cs:58
IdentityStatus
Validation Status of legal identity