aboutsummaryrefslogtreecommitdiff
path: root/back-end
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-02 22:45:59 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-02 22:45:59 -0500
commite4b5b937a05a9869249619a3b17a7269648d93bd (patch)
treec55c9ae5650d3f1fc6d8104bcfed3860e9fe6bfa /back-end
parent2bae750d6a4945e2916f1162d81717a954fd9fac (diff)
fix: #4 added server discovery, close #6, and fix history view
Diffstat (limited to 'back-end')
-rw-r--r--back-end/plugins/nvault/src/Endpoints/Endpoint.cs4
-rw-r--r--back-end/plugins/nvault/src/Endpoints/SettingSyncEndpoint.cs30
-rw-r--r--back-end/plugins/nvault/src/Endpoints/WellKnownEndpoint.cs130
-rw-r--r--back-end/plugins/nvault/src/Model/NostrEventEntry.cs4
-rw-r--r--back-end/plugins/nvault/src/NVault.csproj6
-rw-r--r--back-end/plugins/nvault/src/NVault.example.json12
-rw-r--r--back-end/plugins/nvault/src/NostrEntry.cs1
-rw-r--r--back-end/plugins/nvault/src/NostrOpProvider.cs4
8 files changed, 181 insertions, 10 deletions
diff --git a/back-end/plugins/nvault/src/Endpoints/Endpoint.cs b/back-end/plugins/nvault/src/Endpoints/Endpoint.cs
index 5e8bf09..68aea62 100644
--- a/back-end/plugins/nvault/src/Endpoints/Endpoint.cs
+++ b/back-end/plugins/nvault/src/Endpoints/Endpoint.cs
@@ -26,7 +26,6 @@ using Microsoft.EntityFrameworkCore;
using FluentValidation;
using NVault.VaultExtensions;
-
using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Plugins;
@@ -40,12 +39,11 @@ using VNLib.Plugins.Extensions.Data.Extensions;
using NVault.Plugins.Vault.Model;
-
namespace NVault.Plugins.Vault.Endpoints
{
[ConfigurationName("endpoint")]
- internal class Endpoint : ProtectedWebEndpoint
+ internal sealed class Endpoint : ProtectedWebEndpoint
{
const string EventLogTemplate = "Method {m}, UserID {uid}, Type {tp} Payload {p}";
diff --git a/back-end/plugins/nvault/src/Endpoints/SettingSyncEndpoint.cs b/back-end/plugins/nvault/src/Endpoints/SettingSyncEndpoint.cs
new file mode 100644
index 0000000..05322a5
--- /dev/null
+++ b/back-end/plugins/nvault/src/Endpoints/SettingSyncEndpoint.cs
@@ -0,0 +1,30 @@
+// Copyright (C) 2024 Vaughn Nugent
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+using VNLib.Plugins;
+using VNLib.Plugins.Essentials.Endpoints;
+using VNLib.Plugins.Extensions.Loading;
+
+namespace NVault.Plugins.Vault.Endpoints
+{
+ [ConfigurationName("sync")]
+ internal sealed class SettingSyncEndpoint : ProtectedWebEndpoint
+ {
+ public SettingSyncEndpoint(PluginBase plugin, IConfigScope config)
+ {
+
+ }
+ }
+}
diff --git a/back-end/plugins/nvault/src/Endpoints/WellKnownEndpoint.cs b/back-end/plugins/nvault/src/Endpoints/WellKnownEndpoint.cs
new file mode 100644
index 0000000..d47a6d4
--- /dev/null
+++ b/back-end/plugins/nvault/src/Endpoints/WellKnownEndpoint.cs
@@ -0,0 +1,130 @@
+// Copyright (C) 2024 Vaughn Nugent
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+using System;
+using System.Net;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+using VNLib.Net.Http;
+using VNLib.Utils.IO;
+using VNLib.Utils.Extensions;
+using VNLib.Plugins;
+using VNLib.Plugins.Essentials;
+using VNLib.Plugins.Essentials.Endpoints;
+using VNLib.Plugins.Essentials.Extensions;
+using VNLib.Plugins.Extensions.Loading;
+
+namespace NVault.Plugins.Vault.Endpoints
+{
+
+ [ConfigurationName("discovery")]
+ internal sealed class WellKnownEndpoint : ResourceEndpointBase, IDisposable
+ {
+ private readonly VnMemoryStream _discoveryJsonData;
+ private readonly TimeSpan _cacheDuration;
+
+ protected override ProtectionSettings EndpointProtectionSettings { get; } = new()
+ {
+ //We are going to enable caching ourselves and sessions are not required for discovery
+ EnableCaching = true,
+ DisableSessionsRequired = true,
+ };
+
+ public WellKnownEndpoint(PluginBase plugin, IConfigScope config)
+ {
+ string path = config.GetRequiredProperty("path", p => p.GetString()!);
+ InitPathAndLog(path, plugin.Log);
+
+ //Users may set the cache duration
+ _cacheDuration = config.GetRequiredProperty("cache_duration_sec", p => p.GetTimeSpan(TimeParseType.Seconds));
+
+ //We cant get the account's path programatically, so the user will have to configure it manually
+ string accountsPath = config.GetRequiredProperty("accounts_path", p => p.GetString()!);
+
+ IConfigScope vaultEp = plugin.GetConfigForType<Endpoint>();
+ string nvaultPath = vaultEp.GetRequiredProperty("path", p => p.GetString()!);
+
+ IConfigScope syncConfig = plugin.GetConfigForType<SettingSyncEndpoint>();
+ string syncPath = syncConfig.GetRequiredProperty("path", p => p.GetString()!);
+
+ //Build the discovery result to serialize to json
+ NvaultDiscoveryResult res = new()
+ {
+ //Map endpoints
+ Endpoints = [
+ GetEndpoint("accounts", accountsPath),
+ GetEndpoint("nostr", nvaultPath),
+ GetEndpoint("sync", syncPath)
+ ]
+ };
+
+ _discoveryJsonData = new VnMemoryStream();
+ JsonSerializer.Serialize(_discoveryJsonData, res);
+ _discoveryJsonData.Seek(0, System.IO.SeekOrigin.Begin);
+
+ //Set as readonly to make zero alloc copies
+ VnMemoryStream.CreateReadonly(_discoveryJsonData);
+ }
+
+ protected override VfReturnType Get(HttpEntity entity)
+ {
+ //Allow caching
+ entity.Server.SetCache(CacheType.Public, _cacheDuration);
+
+ //Return a copy of the discovery json object
+ return VirtualClose(
+ entity,
+ HttpStatusCode.OK,
+ ContentType.Json,
+ _discoveryJsonData.GetReadonlyShallowCopy() //Shallow copy to avoid alloc and copy
+ );
+ }
+
+ void IDisposable.Dispose()
+ {
+ _discoveryJsonData.Dispose();
+ }
+
+ private static NvaultEndpoint GetEndpoint(string name, string path)
+ {
+ return new()
+ {
+ Name = name,
+ Path = path
+ };
+ }
+
+ sealed class NvaultDiscoveryResult
+ {
+ [JsonPropertyName("endpoints")]
+ public NvaultEndpoint[]? Endpoints { get; set; }
+ }
+
+ sealed class NvaultEndpoint
+ {
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+
+ [JsonPropertyName("path")]
+ public string? Path { get; set; }
+ }
+
+ public void Dispose()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/back-end/plugins/nvault/src/Model/NostrEventEntry.cs b/back-end/plugins/nvault/src/Model/NostrEventEntry.cs
index b34fd9d..d330580 100644
--- a/back-end/plugins/nvault/src/Model/NostrEventEntry.cs
+++ b/back-end/plugins/nvault/src/Model/NostrEventEntry.cs
@@ -39,12 +39,12 @@ namespace NVault.Plugins.Vault.Model
[MaxLength(64)]
public string? UserId { get; set; }
- [MaxLength(8000)]
+ [MaxLength]
public string? EventData { get; set; }
public static NostrEventEntry FromEvent(string userId, NostrEvent @event) => new()
{
- EventData= JsonSerializer.Serialize(@event),
+ EventData = JsonSerializer.Serialize(@event),
UserId = userId,
};
diff --git a/back-end/plugins/nvault/src/NVault.csproj b/back-end/plugins/nvault/src/NVault.csproj
index 343fd46..2a51610 100644
--- a/back-end/plugins/nvault/src/NVault.csproj
+++ b/back-end/plugins/nvault/src/NVault.csproj
@@ -21,9 +21,9 @@
<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="VNLib.Plugins.Extensions.Data" Version="0.1.0-ci0049" />
- <PackageReference Include="VNLib.Plugins.Extensions.Validation" Version="0.1.0-ci0049" />
- <PackageReference Include="VNLib.Plugins.Extensions.Loading.Sql" Version="0.1.0-ci0049" />
+ <PackageReference Include="VNLib.Plugins.Extensions.Data" Version="0.1.0-ci0050" />
+ <PackageReference Include="VNLib.Plugins.Extensions.Validation" Version="0.1.0-ci0050" />
+ <PackageReference Include="VNLib.Plugins.Extensions.Loading.Sql" Version="0.1.0-ci0050" />
</ItemGroup>
<ItemGroup>
diff --git a/back-end/plugins/nvault/src/NVault.example.json b/back-end/plugins/nvault/src/NVault.example.json
index a3aa2d2..904eba5 100644
--- a/back-end/plugins/nvault/src/NVault.example.json
+++ b/back-end/plugins/nvault/src/NVault.example.json
@@ -1,6 +1,13 @@
{
"debug": false,
+ //well-known discovery path
+ "discovery": {
+ "path": "/.well-known/nvault",
+ "cache_duration_sec": 36000,
+ "accounts_path": "/accounts"
+ },
+
"endpoint": {
"path": "/nostr",
@@ -28,5 +35,10 @@
"lib_type": "managed | native",
"lib_path": "path to dll"
}
+ },
+
+ //Sync endpoint
+ "sync": {
+ "path": "/sync"
}
} \ No newline at end of file
diff --git a/back-end/plugins/nvault/src/NostrEntry.cs b/back-end/plugins/nvault/src/NostrEntry.cs
index 2e57390..1f579d2 100644
--- a/back-end/plugins/nvault/src/NostrEntry.cs
+++ b/back-end/plugins/nvault/src/NostrEntry.cs
@@ -34,6 +34,7 @@ namespace NVault.Plugins.Vault
{
//Load the endpoint
this.Route<Endpoint>();
+ this.Route<WellKnownEndpoint>();
//Create the database
_ = this.ObserveWork(() => this.EnsureDbCreatedAsync<NostrContext>(this), 1800);
diff --git a/back-end/plugins/nvault/src/NostrOpProvider.cs b/back-end/plugins/nvault/src/NostrOpProvider.cs
index cc7342a..253ddb9 100644
--- a/back-end/plugins/nvault/src/NostrOpProvider.cs
+++ b/back-end/plugins/nvault/src/NostrOpProvider.cs
@@ -79,7 +79,7 @@ namespace NVault.Plugins.Vault
newKey.Value = Convert.ToHexString(pubKey).ToLower();
//Zero the buffers
- MemoryUtil.InitializeBlock(buffHandle.Span);
+ MemoryUtil.InitializeBlock(ref buffHandle.GetReference(), buffHandle.GetIntLength());
if (privateKey == null)
{
@@ -126,7 +126,7 @@ namespace NVault.Plugins.Vault
finally
{
//Always zero the private key buffer
- MemoryUtil.InitializeBlock(privKeyBuffer.AsSpan());
+ MemoryUtil.InitializeBlock(privKeyBuffer);
}
}