Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AddNote.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
14
16{
21 {
22 private static readonly HttpAuthenticationScheme[] authenticationSchemes = new HttpAuthenticationScheme[]
23 {
25 };
26
30 public AddNote()
31 : base("/AddNote")
32 {
33 }
34
38 public override bool HandlesSubPaths => true;
39
43 public override bool UserSessions => true;
44
48 public bool AllowsPOST => true;
49
58 {
59 return authenticationSchemes;
60 }
61
68 public async Task POST(HttpRequest Request, HttpResponse Response)
69 {
70 IUser User = Request.User;
71 if (User is null)
72 {
73 await Response.SendResponse(new ForbiddenException(Request, "Unauthorized access prohibited: No user authenticated"));
74 return;
75 }
76
77 if (string.IsNullOrEmpty(Request.SubPath))
78 {
79 await Response.SendResponse(new BadRequestException("Missing Token ID."));
80 return;
81 }
82
83 string TokenId = Request.SubPath[1..];
84 int i = TokenId.IndexOf('@');
85 if (i < 0 || !Guid.TryParse(TokenId[..i], out _))
86 {
87 await Response.SendResponse(new BadRequestException("Invalid Token ID."));
88 return;
89 }
90
91 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true);
92 if (Token is null)
93 {
94 await Response.SendResponse(new NotFoundException("Token '" + TokenId + "' not found on this neuron."));
95 return;
96 }
97
98 if (!Request.HasData)
99 {
100 await Response.SendResponse(new BadRequestException("Missing content."));
101 return;
102 }
103
104 ContentResponse Content = await Request.DecodeDataAsync();
105 if (Content.HasError)
106 {
107 await Response.SendResponse(Content.Error);
108 return;
109 }
110
111 if (Content.Decoded is string TextNote)
112 {
113 if (!User.HasPrivilege("NeuroFeatures.Notes.Add.Text"))
114 {
115 await Response.SendResponse(new ForbiddenException(Request, "Missing privileges to add token text notes."));
116 return;
117 }
118
120 {
121 ArchiveOptional = Token.ArchiveOptional,
122 ArchiveRequired = Token.ArchiveRequired,
123 Expires = Token.Expires,
124 Note = TextNote,
125 Source = User.UserName,
126 Timestamp = DateTime.UtcNow,
127 TokenId = Token.TokenId
128 };
129
130 await Database.Insert(Event);
131 await StateMachineProcessor.EventGenerated(Token, Event);
132 }
133 else if (Content.Decoded is XmlDocument XmlNote)
134 {
135 if (!User.HasPrivilege("NeuroFeatures.Notes.Add.XML"))
136 {
137 await Response.SendResponse(new ForbiddenException(Request, "Missing privileges to add token XML notes."));
138 return;
139 }
140
141 if (XmlNote.DocumentElement is null)
142 {
143 await Response.SendResponse(new BadRequestException("Empty XML not permitted."));
144 return;
145 }
146
147 if (string.IsNullOrEmpty(XmlNote.DocumentElement.NamespaceURI))
148 {
149 await Response.SendResponse(new BadRequestException("XML namespace requried."));
150 return;
151 }
152
153 AssertXmlSafe(XmlNote);
154
155 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(XmlNote, null);
156 if (!string.IsNullOrEmpty(P.Item1))
157 {
158 await Response.SendResponse(new BadRequestException(P.Item1));
159 return;
160 }
161
162 ExternalNoteXml Event = new ExternalNoteXml()
163 {
164 ArchiveOptional = Token.ArchiveOptional,
165 ArchiveRequired = Token.ArchiveRequired,
166 Expires = Token.Expires,
167 Note = XmlNote.DocumentElement.OuterXml,
168 LocalName = XmlNote.DocumentElement.LocalName,
169 Namespace = XmlNote.DocumentElement.NamespaceURI,
170 Source = User.UserName,
171 Timestamp = DateTime.UtcNow,
172 TokenId = Token.TokenId
173 };
174
175 await Database.Insert(Event);
176 await StateMachineProcessor.EventGenerated(Token, Event);
177 }
178 else
179 {
180 await Response.SendResponse(new UnsupportedMediaTypeException("External notes on tokens must be plain text or XML."));
181 return;
182 }
183 }
184
185 private static void AssertXmlSafe(XmlNode Xml)
186 {
187 if (Xml is XmlProcessingInstruction)
188 new BadRequestException("XML Processing instructions not allowed.");
189
190 if (Xml.HasChildNodes)
191 {
192 foreach (XmlNode N in Xml.ChildNodes)
193 AssertXmlSafe(N);
194 }
195 }
196 }
197}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
mTLS authentication mechanism, where identity is taken from a valid client certificate.
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...
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Represents an HTTP request.
Definition: HttpRequest.cs:22
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
IUser User
Authenticated user, if available, or null if not available.
Definition: HttpRequest.cs:203
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
The server is refusing to service the request because the entity of the request is in a format not su...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
Corresponds to a user in the system.
Definition: User.cs:24
string UserName
User Name
Definition: User.cs:60
bool HasPrivilege(string Privilege)
If the user has a given privilege.
Definition: User.cs:187
Maintains the collection of all users in the system.
Definition: Users.cs:24
static IUserSource Source
User source.
Definition: Users.cs:37
A text note logged on the token from an external source.
An xml note logged on the token from an external source.
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
Duration? ArchiveOptional
Duration after which token expires, and the required archiving time, the token can optionally be arch...
Definition: Token.cs:422
Duration? ArchiveRequired
Duration after which token expires, the token is required to be archived.
Definition: Token.cs:412
DateTime Expires
Expiry date of token.
Definition: Token.cs:402
bool AllowsPOST
If the POST method is allowed.
Definition: AddNote.cs:48
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: AddNote.cs:68
override bool UserSessions
If the resource uses user sessions.
Definition: AddNote.cs:43
AddNote()
Adds an external note to a token.
Definition: AddNote.cs:30
override bool HandlesSubPaths
If the resource handles sub-paths.
Definition: AddNote.cs:38
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
Definition: AddNote.cs:57
Service Module hosting the XMPP broker and its components.
POST Interface for HTTP resources.
Basic interface for a user.
Definition: IUser.cs:7
Definition: ImplTypes.g.cs:58