-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add NostrKeyValidAttribute for validating Nostr Key format
This commit adds a new file `NostrKeyValidAttribute.cs` which contains an attribute class for validating the Nostr Key format. The validation logic is implemented in the `IsValid` method of this class. This change also includes modifications to the `User` model to add properties for storing public and private keys, and updates to the `LoginViewModel` to validate the account key using this new attribute. Additionally, there are changes made to localization files, database service, and view models related to user authentication.
- Loading branch information
Showing
11 changed files
with
225 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.Extensions.Localization; | ||
using Nostr.Client.Keys; | ||
using NuSocial.Localization; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace NuSocial.Core.Validation | ||
{ | ||
/// <summary> | ||
/// Attribute class for validating Nostr Key format. | ||
/// </summary> | ||
public sealed class NostrKeyValidAttribute : ValidationAttribute | ||
{ | ||
/// <inheritdoc/> | ||
protected override ValidationResult IsValid(object? value, ValidationContext validationContext) | ||
{ | ||
if (validationContext is null) | ||
{ | ||
throw new ArgumentNullException(nameof(validationContext)); | ||
} | ||
|
||
var instance = validationContext.ObjectInstance; | ||
var loc = (IStringLocalizer<NuSocialResource>?)instance.GetType().GetProperty("L")?.GetValue(instance); | ||
var invalidMessage = loc?["InvalidNostrKey"] ?? "Invalid Key"; | ||
|
||
if (value is string valueString && !string.IsNullOrEmpty(valueString)) | ||
{ | ||
bool isValid = false; | ||
|
||
if (valueString.StartsWith("npub", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
try | ||
{ | ||
_ = NostrPublicKey.FromBech32(valueString); | ||
isValid = true; | ||
} | ||
catch (ArgumentException) { } | ||
} | ||
else if (valueString.StartsWith("nsec", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
try | ||
{ | ||
_ = NostrPrivateKey.FromBech32(valueString); | ||
isValid = true; | ||
} | ||
catch (ArgumentException) { } | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
_ = NostrPublicKey.FromHex(valueString); | ||
isValid = true; | ||
} | ||
catch (ArgumentException) { } | ||
|
||
if (!isValid) | ||
{ | ||
try | ||
{ | ||
_ = NostrPrivateKey.FromHex(valueString); | ||
isValid = true; | ||
} | ||
catch (ArgumentException) { } | ||
} | ||
} | ||
|
||
if (!isValid) | ||
{ | ||
return new(invalidMessage); | ||
} | ||
} | ||
|
||
return ValidationResult.Success!; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,60 @@ | ||
using NuSocial.Core.ViewModel; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using Nostr.Client.Keys; | ||
using NuSocial.Core.Validation; | ||
using NuSocial.Core.ViewModel; | ||
using NuSocial.Messages; | ||
using System.ComponentModel.DataAnnotations; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace NuSocial.ViewModels; | ||
|
||
public partial class LoginViewModel : BaseFormModel, ITransientDependency | ||
{ | ||
private readonly IDatabase _db; | ||
|
||
[Required] | ||
[ObservableProperty] | ||
[NotifyPropertyChangedFor(nameof(IsAccountKeyValid))] | ||
[NostrKeyValid] | ||
private string _accountKey = string.Empty; | ||
|
||
public LoginViewModel(IDialogService dialogService, INavigationService navigationService) : base(dialogService, navigationService) | ||
public LoginViewModel(IDialogService dialogService, INavigationService navigationService, IDatabase db) : base(dialogService, navigationService) | ||
{ | ||
Title = L["Login"]; | ||
_db = db; | ||
} | ||
|
||
public bool IsAccountKeyValid => !string.IsNullOrEmpty(AccountKey); | ||
|
||
[RelayCommand(CanExecute = nameof(IsNotBusy))] | ||
private Task LoginAsync() | ||
{ | ||
return SetBusyAsync(() => { return Task.CompletedTask; }); | ||
return SetBusyAsync(() => | ||
{ | ||
return ValidateAsync(async (isValid) => | ||
{ | ||
if (isValid) | ||
{ | ||
if (AccountKey.StartsWith("npub", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var pubKey = NostrPublicKey.FromBech32(AccountKey); | ||
var user = new User() { PublicKey = pubKey }; | ||
await _db.UpdateUsersAsync(new ObservableCollection<User> { user }); | ||
await Navigation.NavigateTo("//main", user); | ||
} | ||
else | ||
{ | ||
var privKey = NostrPrivateKey.FromBech32(AccountKey); | ||
var pubKeyEc = privKey.Ec.CreateXOnlyPubKey(); | ||
var pubKey = NostrPublicKey.FromEc(pubKeyEc); | ||
var user = new User() { PublicKey = pubKey, PrivateKey = privKey }; | ||
await _db.UpdateUsersAsync(new ObservableCollection<User> { user }); | ||
await Navigation.NavigateTo("//main", user); | ||
} | ||
} | ||
}, async () => await ShowErrorsCommand.ExecuteAsync(null)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
using NuSocial.Core.ViewModel; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using NuSocial.Core.ViewModel; | ||
using NuSocial.Messages; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace NuSocial.ViewModels; | ||
|
||
public partial class MainViewModel : BaseViewModel, ITransientDependency | ||
{ | ||
[ObservableProperty] | ||
private User? _user; | ||
|
||
public MainViewModel(IDialogService dialogService, INavigationService navigationService) : base(dialogService, navigationService) | ||
{ | ||
} | ||
|
||
public override Task OnFirstAppear() | ||
{ | ||
WeakReferenceMessenger.Default.Send<ResetNavMessage>(new()); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task OnParameterSet() | ||
{ | ||
return SetBusyAsync(() => | ||
{ | ||
if (NavigationParameter is User user) | ||
{ | ||
User = user; | ||
} | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters