Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AuthorizeAccessToContract.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
16
18{
23 {
28 : base("Legal/AuthorizeAccessToContract",
29 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
30 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
31 {
32 }
33
34 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(AuthorizeAccessToContract).Namespace + ".JSON.AuthorizeAccessToContract.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(AuthorizeAccessToContract).Namespace + ".XML.AuthorizeAccessToContract.req");
36
45 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
46 {
48
49 CaseInsensitiveString ContractId = (string)Parameters["PContractId"].AssociatedObjectValue;
51 throw new BadRequestException("No Contract ID specified.");
52
53 XmppAddress ContractIdAddress = new XmppAddress(ContractId);
54 if (!ContractIdAddress.IsBareJID)
55 throw new BadRequestException("Invalid Contract ID.");
56
57 if (!(XmppServerModule.Legal is null) && !XmppServerModule.Legal.IsComponentDomain(ContractIdAddress.Domain, true))
58 throw new BadRequestException("Not a local Contract ID.");
59
60 CaseInsensitiveString RemoteId = (string)Parameters["PRemoteId"].AssociatedObjectValue;
61 bool Authorized = (bool)Parameters["PAuthorized"].AssociatedObjectValue;
62
64 throw new BadRequestException("No Remote ID specified.");
65
66 XmppAddress RemoteAddress = new XmppAddress(RemoteId);
67 if (!RemoteAddress.IsBareJID)
68 throw new BadRequestException("Invalid Remote ID.");
69
70 if (!(XmppServerModule.Legal is null))
71 {
72 using (Semaphore Semaphore = await Semaphores.BeginRead("iotsc:" + ContractId.LowerCase))
73 {
74 Contract Contract = await XmppServerModule.Legal.GetContract(ContractId)
75 ?? throw new NotFoundException("Contract not found.");
76
77 XmppAddress From = new XmppAddress(User.UserName + "@" + Gateway.Domain);
78
79 if (!await Contract.CanRead(From, XmppServerModule.Server, XmppServerModule.Legal))
80 throw new ForbiddenException("You do not have access rights to contract.");
81
82 LegalComponent.ClientInformation ClientInfo = await XmppServerModule.Legal.GetNetworkIdentity(RemoteId, true, true, Contract.Version);
83 CaseInsensitiveString RemoteJid = ClientInfo.Jid;
84 XmppServerModule.Legal.ContractAuthorization(RemoteJid, From.BareJid, ContractId, Authorized);
85 }
86 }
87
88 await Response.Return(new NamedDictionary<string, object>("AckResponse", AgentNamespace));
89 }
90 }
91}
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
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...
Represents an HTTP request.
Definition: HttpRequest.cs:18
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...
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 BareJid
Bare JID
Definition: XmppAddress.cs:45
Represents a case-insensitive string.
string LowerCase
Lower-case representation of the case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:16
static async Task< Semaphore > BeginRead(string Key)
Waits until the semaphore identified by Key is ready for reading. Each call to BeginRead must be fol...
Definition: Semaphores.cs:53
Class managing a script expression.
Definition: Expression.cs:39
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
Service Module hosting the XMPP broker and its components.