Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PetitionId.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
13using Waher.Script;
15using Waher.Security;
20
22{
27 {
31 public PetitionId()
32 : base("Legal/PetitionId",
33 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
34 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
35 {
36 }
37
38 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(PetitionId).Namespace + ".JSON.PetitionId.req");
39 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(PetitionId).Namespace + ".XML.PetitionId.req");
40
49 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
50 {
52
53 string KeyId = (string)Parameters["PKeyId"].AssociatedObjectValue;
54 if (string.IsNullOrEmpty(KeyId))
55 throw new BadRequestException("Key ID cannot be empty.");
56
57 CaseInsensitiveString LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
59 throw new BadRequestException("No Legal ID specified.");
60
61 XmppAddress LegalIdAddress = new XmppAddress(LegalId);
62 if (!LegalIdAddress.IsBareJID)
63 throw new BadRequestException("Invalid Legal ID.");
64
65 if (!(XmppServerModule.Legal is null) && !XmppServerModule.Legal.IsComponentDomain(LegalIdAddress.Domain, true))
66 throw new BadRequestException("Not a local Legal ID.");
67
68 CaseInsensitiveString RemoteId = (string)Parameters["PRemoteId"].AssociatedObjectValue;
70 throw new BadRequestException("No Remote ID specified.");
71
72 XmppAddress RemoteAddress = new XmppAddress(RemoteId);
73 if (!RemoteAddress.IsBareJID)
74 throw new BadRequestException("Invalid Remote ID.");
75
76 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
77 new FilterFieldEqualTo("Account", User.UserName),
78 new FilterFieldEqualTo("Id", KeyId)))
79 ?? throw new NotFoundException("Key not found.");
80
81 string PetitionId = (string)Parameters["PPetitionId"].AssociatedObjectValue;
82 string Purpose = (string)Parameters["PPurpose"].AssociatedObjectValue;
83 string KeySignature = (string)Parameters["PKeySignature"].AssociatedObjectValue;
84 string RequestSignature = (string)Parameters["PRequestSignature"].AssociatedObjectValue;
85
86 StringBuilder sb = new StringBuilder();
87
88 sb.Append(User.UserName);
89 sb.Append(':');
90 sb.Append(Request.Header.Host.Value);
91 sb.Append(':');
92 sb.Append(AgentKey.LocalName);
93 sb.Append(':');
94 sb.Append(AgentKey.Namespace);
95 sb.Append(':');
96 sb.Append(KeyId);
97
98 //string s1 = sb.ToString();
99
100 sb.Append(':');
101 sb.Append(KeySignature);
102
103 //string s2 = sb.ToString();
104
105 sb.Append(':');
106 sb.Append(PetitionId);
107 sb.Append(':');
108 sb.Append(Purpose);
109 sb.Append(':');
110 sb.Append(LegalId);
111 sb.Append(':');
112 sb.Append(RemoteId);
113
114 string s3 = sb.ToString();
115
116 string s = Convert.ToBase64String(
118 Encoding.UTF8.GetBytes(User.Account.Password),
119 Encoding.UTF8.GetBytes(s3)));
120
121 if (s != RequestSignature)
122 {
123 string Msg = "Request Signature invalid.";
124 throw new ForbiddenException(Msg);
125 }
126
127 LegalIdentity Identity = await LegalComponent.GetLocalLegalIdentity(LegalId)
128 ?? throw new NotFoundException("Legal identity not found.");
129
130 if (User.UserName != Identity.Account)
131 throw new ForbiddenException("Not your identity.");
132
133 if (!(XmppServerModule.Legal is null))
134 {
135 LegalComponent.ClientInformation ClientInfo = await XmppServerModule.Legal.GetNetworkIdentity(RemoteId, true, false, Identity.Version);
136 CaseInsensitiveString RemoteJid = ClientInfo.Jid;
137 CaseInsensitiveString From = User.UserName + "@" + Gateway.Domain;
138 XmppServerModule.Legal.IdentityAuthorization(RemoteJid, From, LegalId, true);
139 Dictionary<string, string> AttachmentUrls = XmppServerModule.Legal.GetAttachmentUrls(Identity);
140
141 StringBuilder Msg = new StringBuilder();
142
143 Msg.Append("<petitionIdentityMsg id=\"");
144 Msg.Append(XML.Encode(RemoteId));
145 Msg.Append("\" pid=\"");
146 Msg.Append(XML.Encode(PetitionId));
147 Msg.Append("\" from=\"");
148 Msg.Append(XML.Encode(From));
149 Msg.Append("\" purpose=\"");
150 Msg.Append(XML.Encode(Purpose));
151 Msg.Append("\" clientEp=\"");
152 Msg.Append(XML.Encode(Request.RemoteEndPoint));
153 Msg.Append("\" xmlns=\"");
154 Msg.Append(LegalComponent.NamespaceLegalIdentity(Identity.Version));
155 Msg.Append("\">");
156 Identity.Serialize(Msg, false, true, true, true, true, true, true, AttachmentUrls, XmppServerModule.Legal);
157 Msg.Append("</petitionIdentityMsg>");
158
159 await XmppServerModule.Server.SendMessage(string.Empty, string.Empty,
160 XmppServerModule.Legal.MainDomain.Address, RemoteJid, string.Empty,
161 Msg.ToString());
162 }
163
164 await Response.Return(new NamedDictionary<string, object>("AckResponse", AgentNamespace));
165 }
166 }
167}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
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:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:195
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,...
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:86
bool IsComponentDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the component domain, or optionally, an alternative component domain.
Definition: Component.cs:123
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:3412
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:19
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.
Class managing a script expression.
Definition: Expression.cs:39
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] ComputeHMACSHA256Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:585
string Password
Password of account
Definition: Account.cs:109
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.