|
| 1 | +using System; |
| 2 | +using System.ComponentModel.DataAnnotations; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 8 | + |
| 9 | +namespace SourceGit.ViewModels |
| 10 | +{ |
| 11 | + public partial class SSHKeyGenerator : ObservableValidator |
| 12 | + { |
| 13 | + [Required(ErrorMessage = "Name is required.")] |
| 14 | + [RegularExpression(@"^[a-zA-Z0-9_\-]+$", ErrorMessage = "Name can only contain letters, numbers, underscores, and hyphens.")] |
| 15 | + public string Name |
| 16 | + { |
| 17 | + get => _name; |
| 18 | + set => SetProperty(ref _name, value, true); |
| 19 | + } |
| 20 | + |
| 21 | + [Required(ErrorMessage = "Email is required.")] |
| 22 | + [EmailAddress(ErrorMessage = "Invalid email address.")] |
| 23 | + public string Email |
| 24 | + { |
| 25 | + get => _email; |
| 26 | + set => SetProperty(ref _email, value, true); |
| 27 | + } |
| 28 | + |
| 29 | + public bool UsePassphrase |
| 30 | + { |
| 31 | + get => _usePassphrase; |
| 32 | + set |
| 33 | + { |
| 34 | + if (SetProperty(ref _usePassphrase, value)) |
| 35 | + { |
| 36 | + ValidateProperty(_passphrase, nameof(Passphrase)); |
| 37 | + ValidateProperty(_confirmedPassphrase, nameof(ConfirmedPassphrase)); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [CustomValidation(typeof(SSHKeyGenerator), nameof(ValidatePassphrase))] |
| 43 | + public string Passphrase |
| 44 | + { |
| 45 | + get => _passphrase; |
| 46 | + set |
| 47 | + { |
| 48 | + if (SetProperty(ref _passphrase, value, true)) |
| 49 | + ValidateProperty(_confirmedPassphrase, nameof(ConfirmedPassphrase)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [CustomValidation(typeof(SSHKeyGenerator), nameof(ValidateConfirmedPassphrase))] |
| 54 | + public string ConfirmedPassphrase |
| 55 | + { |
| 56 | + get => _confirmedPassphrase; |
| 57 | + set => SetProperty(ref _confirmedPassphrase, value, true); |
| 58 | + } |
| 59 | + |
| 60 | + public string ErrorMessage |
| 61 | + { |
| 62 | + get => _errorMessage; |
| 63 | + set => SetProperty(ref _errorMessage, value); |
| 64 | + } |
| 65 | + |
| 66 | + public SSHKeyGenerator(string baseDir) |
| 67 | + { |
| 68 | + _baseDir = baseDir; |
| 69 | + } |
| 70 | + |
| 71 | + public static ValidationResult ValidatePassphrase(string password, ValidationContext context) |
| 72 | + { |
| 73 | + var instance = (SSHKeyGenerator)context.ObjectInstance; |
| 74 | + if (!instance.UsePassphrase) |
| 75 | + return ValidationResult.Success; |
| 76 | + |
| 77 | + if (string.IsNullOrEmpty(password)) |
| 78 | + return new ValidationResult("Passphrase is required!"); |
| 79 | + |
| 80 | + if (!REG_PSWD_FORMAT().IsMatch(password)) |
| 81 | + return new ValidationResult("Passphrase can only contain letters, numbers, and special characters: _ + - = @ # $ % ! &"); |
| 82 | + |
| 83 | + return ValidationResult.Success; |
| 84 | + } |
| 85 | + |
| 86 | + public static ValidationResult ValidateConfirmedPassphrase(string confirmedPassword, ValidationContext context) |
| 87 | + { |
| 88 | + var instance = (SSHKeyGenerator)context.ObjectInstance; |
| 89 | + if (!instance.UsePassphrase) |
| 90 | + return ValidationResult.Success; |
| 91 | + |
| 92 | + if (confirmedPassword != instance.Passphrase) |
| 93 | + return new ValidationResult("Passphrase and confirmation do not match!"); |
| 94 | + |
| 95 | + return ValidationResult.Success; |
| 96 | + } |
| 97 | + |
| 98 | + public bool Run() |
| 99 | + { |
| 100 | + ErrorMessage = string.Empty; |
| 101 | + |
| 102 | + ValidateAllProperties(); |
| 103 | + if (HasErrors) |
| 104 | + return false; |
| 105 | + |
| 106 | + var passphrase = _usePassphrase ? _passphrase : string.Empty; |
| 107 | + var keyFile = Path.Combine(_baseDir, _name); |
| 108 | + var start = new ProcessStartInfo(); |
| 109 | + start.FileName = "ssh-keygen"; |
| 110 | + start.Arguments = $"-q -t ed25519 -N {passphrase.Quoted()} -C {_email.Quoted()} -f {keyFile.Quoted()}"; |
| 111 | + start.UseShellExecute = false; |
| 112 | + start.CreateNoWindow = true; |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + var proc = Process.Start(start); |
| 117 | + proc.WaitForExit(); |
| 118 | + proc.Close(); |
| 119 | + } |
| 120 | + catch (Exception e) |
| 121 | + { |
| 122 | + ErrorMessage = $"Failed to generate SSH key: {e.Message}"; |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + [GeneratedRegex(@"^[0-9a-zA-Z_\-\@\#\$\%\!\&\+\=]+$")] |
| 130 | + private static partial Regex REG_PSWD_FORMAT(); |
| 131 | + |
| 132 | + private string _baseDir; |
| 133 | + private string _name; |
| 134 | + private string _email; |
| 135 | + private bool _usePassphrase; |
| 136 | + private string _passphrase; |
| 137 | + private string _confirmedPassphrase; |
| 138 | + private string _errorMessage; |
| 139 | + } |
| 140 | +} |
0 commit comments