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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
10using Waher.Script;
15
17{
22 {
26 public GetToken()
27 : base("Tokens/GetToken",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetToken).Namespace + ".JSON.GetToken.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetToken).Namespace + ".XML.GetToken.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
48 string BareJid = User.UserName + "@" + Gateway.Domain;
49
50 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, false)
51 ?? throw new NotFoundException("Token not found.");
52
53 await AssertAuthorization(Token, new XmppAddress(BareJid));
54
55 StringBuilder Xml = new StringBuilder();
56 Token.Serialize(Xml, true, true);
57
58 XmlDocument Doc = new XmlDocument()
59 {
60 PreserveWhitespace = true
61 };
62 Doc.LoadXml(Xml.ToString());
63
64 await Response.Return(new NamedDictionary<string, object>("TokenResult", AgentNamespace)
65 {
66 { "Token", Doc }
67 });
68 }
69
70 internal static async Task AssertAuthorization(Token Token, XmppAddress From)
71 {
72 switch (Token.Visibility)
73 {
74 case ContractVisibility.Public:
75 case ContractVisibility.PublicSearchable:
76 return;
77
78 case ContractVisibility.DomainAndParts:
79 if (XmppServerModule.Server.IsServerDomain(From.Domain, true))
80 return;
81 break;
82 }
83
84 if (Token.OwnerJid == From.BareJid)
85 return;
86
88 return;
89
91 ?? throw new ServiceUnavailableException("Ownership contract of token not accessible at this time.");
92
93 if (!await Contract.CanRead(From, XmppServerModule.Server, XmppServerModule.Legal))
94 throw new ForbiddenException("You do not have access to this token.");
95 }
96 }
97}
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 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...
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:861
Class managing a script expression.
Definition: Expression.cs:39
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeServerSignature)
Serializes the Token, in normalized form.
Definition: Token.cs:609
CaseInsensitiveString OwnershipContract
ID of contract that details the claims of the current owner
Definition: Token.cs:276
ContractVisibility Visibility
Visibility of token
Definition: Token.cs:167
CaseInsensitiveString OwnerJid
JID of Current owner of token
Definition: Token.cs:203
bool IsExternalPart(CaseInsensitiveString BareJid)
Checks if a JID is an external part in the creation of the contract.
Definition: Token.cs:1233
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:44
Service Module hosting the XMPP broker and its components.