Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PetitionPeerReview.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
14using Waher.Script;
16using Waher.Security;
21
23{
28 {
33 : base("Legal/PetitionPeerReview",
34 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
35 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
36 {
37 }
38
39 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(PetitionPeerReview).Namespace + ".JSON.PetitionPeerReview.req");
40 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(PetitionPeerReview).Namespace + ".XML.PetitionPeerReview.req");
41
50 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
51 {
53
54 string KeyId = (string)Parameters["PKeyId"].AssociatedObjectValue;
55 if (string.IsNullOrEmpty(KeyId))
56 throw new BadRequestException("Key ID cannot be empty.");
57
58 CaseInsensitiveString LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
60 throw new BadRequestException("No Legal ID specified.");
61
62 XmppAddress LegalIdAddress = new XmppAddress(LegalId);
63 if (!LegalIdAddress.IsBareJID)
64 throw new BadRequestException("Invalid Legal ID.");
65
66 if (!(XmppServerModule.Legal is null) && !XmppServerModule.Legal.IsComponentDomain(LegalIdAddress.Domain, true))
67 throw new BadRequestException("Not a local Legal ID.");
68
69 CaseInsensitiveString RemoteId = (string)Parameters["PRemoteId"].AssociatedObjectValue;
71 throw new BadRequestException("No Remote ID specified.");
72
73 XmppAddress RemoteAddress = new XmppAddress(RemoteId);
74 if (!RemoteAddress.IsBareJID)
75 throw new BadRequestException("Invalid Remote ID.");
76
77 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
78 new FilterFieldEqualTo("Account", User.UserName),
79 new FilterFieldEqualTo("Id", KeyId)))
80 ?? throw new NotFoundException("Key not found.");
81
82 string PetitionId = (string)Parameters["PPetitionId"].AssociatedObjectValue;
83 string Purpose = (string)Parameters["PPurpose"].AssociatedObjectValue;
84 string KeySignature = (string)Parameters["PKeySignature"].AssociatedObjectValue;
85 string RequestSignature = (string)Parameters["PRequestSignature"].AssociatedObjectValue;
86
87 StringBuilder sb = new StringBuilder();
88
89 sb.Append(User.UserName);
90 sb.Append(':');
91 sb.Append(Request.Header.Host.Value);
92 sb.Append(':');
93 sb.Append(AgentKey.LocalName);
94 sb.Append(':');
95 sb.Append(AgentKey.Namespace);
96 sb.Append(':');
97 sb.Append(KeyId);
98
99 //string s1 = sb.ToString();
100
101 sb.Append(':');
102 sb.Append(KeySignature);
103
104 //string s2 = sb.ToString();
105
106 sb.Append(':');
107 sb.Append(PetitionId);
108 sb.Append(':');
109 sb.Append(Purpose);
110 sb.Append(':');
111 sb.Append(LegalId);
112 sb.Append(':');
113 sb.Append(RemoteId);
114
115 string s3 = sb.ToString();
116
117 string s = Convert.ToBase64String(
119 Encoding.UTF8.GetBytes(User.Account.Password),
120 Encoding.UTF8.GetBytes(s3)));
121
122 if (s != RequestSignature)
123 {
124 string Msg = "Request Signature invalid.";
125 throw new ForbiddenException(Request, Msg);
126 }
127
128 LegalIdentity Identity = await LegalComponent.GetLocalLegalIdentity(LegalId)
129 ?? throw new NotFoundException("Legal identity not found.");
130
131 if (User.UserName != Identity.Account)
132 throw new ForbiddenException(Request, "Not your identity.");
133
134 if (Identity.State != IoTBroker.Legal.Identity.IdentityState.Created)
135 throw new ForbiddenException(Request, "Identity not in a state where peer review is possible.");
136
137 if (!(XmppServerModule.Legal is null))
138 {
139 Dictionary<string, string> AttachmentUrls = XmppServerModule.Legal.GetAttachmentUrls(Identity);
140 StringBuilder Xml = new StringBuilder();
141 Identity.Serialize(Xml, true, true, true, true, true, true, true, AttachmentUrls, XmppServerModule.Legal);
142 byte[] Content = Encoding.UTF8.GetBytes(Xml.ToString());
143 string ContentBase64 = Convert.ToBase64String(Content);
144
145 LegalComponent.ClientInformation ClientInfo = await XmppServerModule.Legal.GetNetworkIdentity(RemoteId, true, false, Identity.Version);
146 CaseInsensitiveString RemoteJid = ClientInfo.Jid;
147 CaseInsensitiveString From = User.UserName + "@" + Gateway.Domain;
148 XmppServerModule.Legal.IdentityAuthorization(RemoteJid, From, LegalId, true);
149
150 StringBuilder Msg = new StringBuilder();
151
152 Msg.Append("<petitionSignatureMsg id=\"");
153 Msg.Append(XML.Encode(RemoteId));
154 Msg.Append("\" pid=\"");
155 Msg.Append(XML.Encode(PetitionId));
156 Msg.Append("\" from=\"");
157 Msg.Append(XML.Encode(From));
158 Msg.Append("\" purpose=\"");
159 Msg.Append(XML.Encode(Purpose));
160 Msg.Append("\" clientEp=\"");
161 Msg.Append(XML.Encode(Request.RemoteEndPoint));
162 Msg.Append("\" xmlns=\"");
163 Msg.Append(LegalComponent.NamespaceLegalIdentity(Identity.Version));
164 Msg.Append("\"><content>");
165 Msg.Append(ContentBase64);
166 Msg.Append("</content>");
167 Msg.Append("</petitionSignatureMsg>");
168
169 await XmppServerModule.Server.SendMessage(string.Empty, string.Empty,
170 XmppServerModule.Legal.MainDomain.Address, RemoteJid, string.Empty,
171 Msg.ToString());
172 }
173
174 await Response.Return(new NamedDictionary<string, object>("AckResponse", AgentNamespace));
175 }
176 }
177}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
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 class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
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...
HttpFieldHost Host
Host HTTP Field header. (RFC 2616, §14.23)
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
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
The server has not found anything matching the Request-URI. No indication is given of whether the con...
XmppAddress MainDomain
Main/principal domain address
Definition: Component.cs:87
bool IsComponentDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the component domain, or optionally, an alternative component domain.
Definition: Component.cs:124
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
bool IsBareJID
If the address is a Bare JID.
Definition: XmppAddress.cs:159
CaseInsensitiveString Domain
Domain
Definition: XmppAddress.cs:97
CaseInsensitiveString Address
XMPP Address
Definition: XmppAddress.cs:37
Task< bool > SendMessage(string Type, string Id, string From, string To, string Language, string ContentXml)
Sends a Message stanza to a recipient.
Definition: XmppServer.cs:3862
Represents a case-insensitive string.
static readonly CaseInsensitiveString Empty
Empty case-insensitive string
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeHMACSHA256Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:735
string Password
Password of account
Definition: Account.cs:151
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Contains an encrypted key for an agent.
Definition: AgentKey.cs:13
Service Module hosting the XMPP broker and its components.