Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Vault.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using Waher.Content;
13
15{
20 {
21 public Vault()
22 : base("/Vault")
23 {
24 }
25
29 public bool AllowsGET => true;
30
34 public override bool HandlesSubPaths => true;
35
39 public override bool UserSessions => false;
40
47 public async Task GET(HttpRequest Request, HttpResponse Response)
48 {
49 string[] Parts = Request.SubPath.Split('/', StringSplitOptions.None);
50 if (Parts.Length != 3 || !string.IsNullOrEmpty(Parts[0]))
51 {
52 await Response.SendResponse(new BadRequestException("Invalid Vault URL."));
53 return;
54 }
55
56 string ReferenceId = Parts[1];
57 using Semaphore Semaphore = await Semaphores.BeginWrite("vaultreference:" + ReferenceId);
58 byte[] Signature;
59
60 try
61 {
62 Signature = Base64Url.Decode(Parts[2]);
63 }
64 catch (Exception)
65 {
66 await Response.SendResponse(new BadRequestException("Invalid Signature."));
67 return;
68 }
69
71
72 try
73 {
74 Ref = await Database.TryLoadObject<VaultItemReference>(ReferenceId);
75 }
76 catch (Exception)
77 {
78 await Response.SendResponse(new BadRequestException("Invalid Vault Reference."));
79 return;
80 }
81
82 if (Ref is null)
83 {
84 await Response.SendResponse(new NotFoundException("Vault Reference not found."));
85 return;
86 }
87
88 if (DateTime.UtcNow > Ref.Expires)
89 {
90 await Database.Delete(Ref);
91 await Response.SendResponse(new NotFoundException("Vault Reference expired."));
92 return;
93 }
94
95 VaultItem Item = await Database.FindFirstIgnoreRest<VaultItem>(
96 new FilterFieldEqualTo("VaultId", Ref.VaultId));
97
98 if (Item is null)
99 {
100 await Database.Delete(Ref);
101 await Response.SendResponse(new NotFoundException("Vault Item not found."));
102 return;
103 }
104
105 AgentKey AgentKey = await Database.FindFirstDeleteRest<AgentKey>(new FilterAnd(
106 new FilterFieldEqualTo("Account", Item.Account),
107 new FilterFieldEqualTo("Id", Ref.KeyId)));
108
109 if (AgentKey is null)
110 {
111 await Database.Delete(Ref);
112 await Response.SendResponse(new NotFoundException("Vault Reference signature key not found."));
113 return;
114 }
115
116 EllipticCurveEndpoint KeyEndpoint = ApplyId.GetEndpoint(Request, AgentKey, Ref.Seed);
117 string PreSign = "/Vault/" + ReferenceId + '/';
118
119 if (!KeyEndpoint.Verify(Encoding.UTF8.GetBytes(PreSign), Signature))
120 {
121 await Response.SendResponse(new ForbiddenException("Invalid Signature."));
122 return;
123 }
124
125 if (Ref.UseCount > 0)
126 {
127 Ref.UseCount--;
128
129 if (Ref.UseCount == 0)
130 await Database.Delete(Ref);
131 else
132 await Database.Update(Ref);
133 }
134
135 await Response.Return(GetFromVault.EncodeVaultItem(Item, Ref.Masked));
136 }
137 }
138}
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
Definition: Base64Url.cs:11
static byte[] Decode(string Base64Url)
Converts a Base64URL-encoded string to its binary representation.
Definition: Base64Url.cs:17
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
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
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...
Task Return(Exception ex)
Returns an error to the client.
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...
Abstract base class for Elliptic Curve endpoints.
bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature.
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 Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:1291
static Task< object > TryLoadObject(string CollectionName, object ObjectId)
Tries to load an object given its Object ID ObjectId and its collection name CollectionName .
Definition: Database.cs:1838
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:17
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:91
Contains an encrypted key for an agent.
Definition: AgentKey.cs:13
Gets information from the Encrypted Vault.
Definition: GetFromVault.cs:21
Contains information about an item in the Vault.
Definition: VaultItem.cs:16
bool Masked
If referenced vault item should be returned masked (true) or unmasked (false).
Access to secured vault storage via signed URLs.
Definition: Vault.cs:20
bool AllowsGET
If the GET method is allowed.
Definition: Vault.cs:29
override bool HandlesSubPaths
If the resource handles sub-paths.
Definition: Vault.cs:34
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
Definition: Vault.cs:47
override bool UserSessions
If the resource uses user sessions.
Definition: Vault.cs:39
GET Interface for HTTP resources.