Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StoreInVault.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
26 public StoreInVault()
27 : base("Storage/StoreInVault",
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(StoreInVault).Namespace + ".JSON.StoreInVault.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(StoreInVault).Namespace + ".XML.StoreInVault.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47
48 string Type = (string)Parameters["PType"].AssociatedObjectValue;
49 string ClientId = (string)Parameters["PClientId"]?.AssociatedObjectValue;
50 string VaultId = (string)Parameters["PVaultId"]?.AssociatedObjectValue;
51 object[] TagNames = (object[])Parameters["PTagName"].AssociatedObjectValue;
52 object[] TagValues = (object[])Parameters["PTagValue"].AssociatedObjectValue;
53 object[] TagMaskedValues = (object[])Parameters["PTagMaskedValue"].AssociatedObjectValue;
55
56 int i, c = TagNames?.Length ?? 0;
57 if ((TagValues?.Length ?? 0) != c || (TagMaskedValues?.Length ?? 0) != c)
58 throw new BadRequestException("Invalid Tags.");
59
60 for (i = 0; i < c; i++)
61 {
62 if (!(TagNames[i] is string TagName) || string.IsNullOrEmpty(TagName))
63 throw new BadRequestException("Invalid Tag Name.");
64
65 if (!(TagValues[i] is string TagValue))
66 throw new BadRequestException("Invalid Tag Value.");
67
68 if (!(TagMaskedValues[i] is string TagMaskedValue))
69 {
70 if (TagMaskedValues[i] is null)
71 TagMaskedValue = null;
72 else
73 throw new BadRequestException("Invalid Tag Masked Value.");
74 }
75
76 VaultTag Tag = new VaultTag(TagName, TagValue, TagMaskedValue);
77 Tags.Add(Tag);
78 }
79
80 VaultItem Item;
81
82 if (string.IsNullOrEmpty(VaultId))
83 {
84 Item = null;
85
86 while (Item is null)
87 {
88 Item = new VaultItem()
89 {
90 Account = User.UserName,
91 Type = Type,
92 ClientId = ClientId ?? string.Empty,
93 VaultId = Gateway.NextBytes(32),
94 Tags = Tags.ToArray(),
95 Created = DateTime.UtcNow
96 };
97
98 Item.Updated = Item.Created;
99
100 if (!(await Database.FindFirstIgnoreRest<VaultItem>(
101 new FilterFieldEqualTo("VaultId", Item.VaultId)) is null))
102 {
103 Item = null;
104 }
105 }
106
107 await Database.Insert(Item);
108 }
109 else
110 {
111 byte[] VaultIdBin;
112
113 try
114 {
115 VaultIdBin = Convert.FromBase64String(VaultId);
116 }
117 catch(Exception)
118 {
119 throw new BadRequestException("Invalid Vault ID.");
120 }
121
122 Item = await Database.FindFirstIgnoreRest<VaultItem>(
123 new FilterFieldEqualTo("VaultId", VaultIdBin))
124 ?? throw new NotFoundException("Vault item not found.");
125
126 if (Item.Account != User.UserName)
127 throw new ForbiddenException(Request, "Access to vault item denied.");
128
129 if (Item.Type != Type)
130 throw new BadRequestException("Vault item not associated with type.");
131
132 if (Item.ClientId != (ClientId ?? string.Empty))
133 {
134 if (string.IsNullOrEmpty(ClientId))
135 throw new BadRequestException("Vault item associated with a client.");
136 else
137 throw new BadRequestException("Vault item not not associated with client.");
138 }
139
140 Item.Updated = DateTime.UtcNow;
141 Item.Tags = Tags.ToArray();
142
143 await Database.Update(Item);
144 }
145
146 await Response.Return(new NamedDictionary<string, object>("Stored", AgentNamespace)
147 {
148 { "created", Item.Created },
149 { "updated", Item.Updated },
150 { "vaultId", Convert.ToBase64String(Item.VaultId) }
151 });
152 }
153 }
154}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:4335
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: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...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
This filter selects objects that have a named field equal to a given value.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
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
Contains information about a broker account.
Definition: Account.cs:41
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
Stores information in the Encrypted Vault.
Definition: StoreInVault.cs:22
StoreInVault()
Stores information in the Encrypted Vault.
Definition: StoreInVault.cs:26
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: StoreInVault.cs:44
Contains information about an item in the Vault.
Definition: VaultItem.cs:16
string ClientId
Client ID (as defined by the agent).
Definition: VaultItem.cs:62
Contains information about a tag in a vault item.
Definition: VaultTag.cs:7