Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetToken.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
12using Waher.Script;
17
19{
24 {
28 public GetToken()
29 : base("Tokens/GetToken",
30 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
31 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
32 {
33 }
34
35 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetToken).Namespace + ".JSON.GetToken.req");
36 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetToken).Namespace + ".XML.GetToken.req");
37
46 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
47 {
49 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
50 string BareJid = User.UserName + "@" + Gateway.Domain;
51
52 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, false)
53 ?? throw new NotFoundException("Token not found.");
54
55 await AssertAuthorization(Request, Token, new XmppAddress(BareJid));
56
57 StringBuilder Xml = new StringBuilder();
58 await Token.Serialize(Xml, true, true);
59
60 XmlDocument Doc = XML.ParseXml(Xml.ToString(), true);
61
62 await Response.Return(new NamedDictionary<string, object>("TokenResult", AgentNamespace)
63 {
64 { "Token", Doc }
65 });
66 }
67
68 internal static async Task AssertAuthorization(HttpRequest Request, Token Token, XmppAddress From)
69 {
70 switch (Token.Visibility)
71 {
72 case ContractVisibility.Public:
73 case ContractVisibility.PublicSearchable:
74 return;
75
76 case ContractVisibility.DomainAndParts:
77 if (XmppServerModule.Server.IsServerDomain(From.Domain, true))
78 return;
79 break;
80 }
81
82 if (Token.OwnerJid == From.BareJid)
83 return;
84
86 return;
87
88 KeyValuePair<Contract, IqResultEventArgs> P = await XmppServerModule.Legal.GetContract(Token.OwnershipContract);
89 Contract Contract = P.Key;
90
91 if (Contract is null)
92 {
93 if (string.IsNullOrEmpty(P.Value?.ErrorText))
94 throw new ServiceUnavailableException("Ownership contract of token not accessible at this time.");
95 else
96 throw new ServiceUnavailableException("Ownership contract of token not accessible at this time: " + P.Value.ErrorText);
97 }
98
99 if (!await Contract.CanRead(From, XmppServerModule.Server, XmppServerModule.Legal))
100 throw new ForbiddenException(Request, "You do not have access to this token.");
101 }
102 }
103}
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 XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
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
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...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Domain
Domain
Definition: XmppAddress.cs:97
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
bool IsServerDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the server domain, or optionally, an alternative domain.
Definition: XmppServer.cs:898
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
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
async Task Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeServerSignature)
Serializes the Token, in normalized form.
Definition: Token.cs:652
CaseInsensitiveString OwnershipContract
ID of contract that details the claims of the current owner
Definition: Token.cs:316
ContractVisibility Visibility
Visibility of token
Definition: Token.cs:173
CaseInsensitiveString OwnerJid
JID of Current owner of token
Definition: Token.cs:216
bool IsExternalPart(CaseInsensitiveString BareJid)
Checks if a JID is an external part in the creation of the contract.
Definition: Token.cs:1321
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
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetToken.cs:46
Service Module hosting the XMPP broker and its components.