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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
13
15{
20 {
21 private static readonly HttpAuthenticationScheme[] authenticationSchemes = new HttpAuthenticationScheme[]
22 {
24 };
25
29 public AddNote()
30 : base("/AddNote")
31 {
32 }
33
34 public override bool HandlesSubPaths => true;
35 public override bool UserSessions => true;
36 public bool AllowsPOST => true;
37
38 public override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request) => authenticationSchemes;
39
40 public async Task POST(HttpRequest Request, HttpResponse Response)
41 {
42 IUser User = Request.User
43 ?? throw new ForbiddenException("Unauthorized access prohibited: No user authenticated");
44
45 if (string.IsNullOrEmpty(Request.SubPath))
46 throw new BadRequestException("Missing Token ID.");
47
48 string TokenId = Request.SubPath.Substring(1);
49 int i = TokenId.IndexOf('@');
50 if (i < 0 || !Guid.TryParse(TokenId.Substring(0, i), out _))
51 throw new BadRequestException("Invalid Token ID.");
52
53 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
54 ?? throw new NotFoundException("Token '" + TokenId + "' not found on this neuron.");
55
56 if (!Request.HasData)
57 throw new BadRequestException("Missing content.");
58
59 object Obj = await Request.DecodeDataAsync();
60 if (Obj is string TextNote)
61 {
62 if (!User.HasPrivilege("NeuroFeatures.Notes.Add.Text"))
63 throw new ForbiddenException("Missing privileges to add token text notes.");
64
66 {
67 ArchiveOptional = Token.ArchiveOptional,
68 ArchiveRequired = Token.ArchiveRequired,
69 Expires = Token.Expires,
70 Note = TextNote,
71 Source = User.UserName,
72 Timestamp = DateTime.UtcNow,
73 TokenId = Token.TokenId
74 };
75
76 await Database.Insert(Event);
77 await StateMachineProcessor.EventGenerated(Token, Event);
78 }
79 else if (Obj is XmlDocument XmlNote)
80 {
81 if (!User.HasPrivilege("NeuroFeatures.Notes.Add.XML"))
82 throw new ForbiddenException("Missing privileges to add token XML notes.");
83
84 if (XmlNote.DocumentElement is null)
85 throw new BadRequestException("Empty XML not permitted.");
86
87 if (string.IsNullOrEmpty(XmlNote.DocumentElement.NamespaceURI))
88 throw new BadRequestException("XML namespace requried.");
89
90 AssertXmlSafe(XmlNote);
91
92 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(XmlNote);
93 if (!string.IsNullOrEmpty(P.Item1))
94 throw new BadRequestException(P.Item1);
95
97 {
98 ArchiveOptional = Token.ArchiveOptional,
99 ArchiveRequired = Token.ArchiveRequired,
100 Expires = Token.Expires,
101 Note = XmlNote.DocumentElement.OuterXml,
102 LocalName = XmlNote.DocumentElement.LocalName,
103 Namespace = XmlNote.DocumentElement.NamespaceURI,
104 Source = User.UserName,
105 Timestamp = DateTime.UtcNow,
106 TokenId = Token.TokenId
107 };
108
109 await Database.Insert(Event);
110 await StateMachineProcessor.EventGenerated(Token, Event);
111 }
112 else
113 throw new UnsupportedMediaTypeException("External notes on tokens must be plain text or XML.");
114 }
115
116 private static void AssertXmlSafe(XmlNode Xml)
117 {
118 if (Xml is XmlProcessingInstruction)
119 throw new BadRequestException("XML Processing instructions not allowed.");
120
121 if (Xml.HasChildNodes)
122 {
123 foreach (XmlNode N in Xml.ChildNodes)
124 AssertXmlSafe(N);
125 }
126 }
127 }
128}
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:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:146
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
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:19
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
Corresponds to a user in the system.
Definition: User.cs:21
string UserName
User Name
Definition: User.cs:53
bool HasPrivilege(string Privilege)
If the user has a given privilege.
Definition: User.cs:129
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:382
Duration? ArchiveRequired
Duration after which token expires, the token is required to be archived.
Definition: Token.cs:372
DateTime Expires
Expiry date of token.
Definition: Token.cs:362
bool AllowsPOST
If the POST method is allowed.
Definition: AddNote.cs:36
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: AddNote.cs:40
AddNote()
Adds an external note to a token.
Definition: AddNote.cs:29
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
Service Module hosting the XMPP broker and its components.
POST Interface for HTTP resources.
Basic interface for a user.
Definition: IUser.cs:7