diff options
author | Wouter Tinus <win.acme.simple@gmail.com> | 2020-05-31 15:08:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-31 15:08:36 +0200 |
commit | c417987f3562acb24efefd5fc157cc1c299ed1e3 (patch) | |
tree | bb1a6e652d88badef095ae0d6090be1e4bdc1667 /src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs | |
parent | 96818c529910c8d1bb2856e8f50b14d9c195ab70 (diff) | |
parent | eddd9b13e9ea422c108f5e0cc88f9daa8e26a1a8 (diff) | |
download | letsencrypt-win-simple-c417987f3562acb24efefd5fc157cc1c299ed1e3.zip letsencrypt-win-simple-c417987f3562acb24efefd5fc157cc1c299ed1e3.tar.gz letsencrypt-win-simple-c417987f3562acb24efefd5fc157cc1c299ed1e3.tar.bz2 |
Merge pull request #1556 from win-acme/2.1.8v2.1.8.838
2.1.8
Diffstat (limited to 'src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs')
-rw-r--r-- | src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs b/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs new file mode 100644 index 0000000..53c3ede --- /dev/null +++ b/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFileOptionsFactory.cs @@ -0,0 +1,91 @@ +using PKISharp.WACS.Extensions; +using PKISharp.WACS.Plugins.Base.Factories; +using PKISharp.WACS.Services; +using PKISharp.WACS.Services.Serialization; +using System; +using System.Threading.Tasks; + +namespace PKISharp.WACS.Plugins.StorePlugins +{ + internal class PfxFileOptionsFactory : StorePluginOptionsFactory<PfxFile, PfxFileOptions> + { + private readonly ILogService _log; + private readonly IArgumentsService _arguments; + private readonly ISettingsService _settings; + + public PfxFileOptionsFactory(ILogService log, ISettingsService settings, IArgumentsService arguments) + { + _log = log; + _arguments = arguments; + _settings = settings; + } + + public override async Task<PfxFileOptions?> Aquire(IInputService input, RunLevel runLevel) + { + var args = _arguments.GetArguments<PfxFileArguments>(); + + // Get path from command line, default setting or user input + var path = args?.PfxFilePath; + if (string.IsNullOrWhiteSpace(path)) + { + path = _settings.Store.PfxFile?.DefaultPassword; + } + while (string.IsNullOrWhiteSpace(path) || !path.ValidPath(_log)) + { + path = await input.RequestString("Path to folder to store the .pfx file"); + } + + // Get password from command line, default setting or user input + var password = args?.PfxPassword; + if (string.IsNullOrWhiteSpace(password)) + { + password = _settings.Store.PfxFile?.DefaultPassword; + } + if (string.IsNullOrEmpty(password)) + { + password = await input.ReadPassword("Password to use for the .pfx files or <ENTER> for none"); + } + return Create(path, password); + } + + public override async Task<PfxFileOptions?> Default() + { + var args = _arguments.GetArguments<PfxFileArguments>(); + var path = _settings.Store.PfxFile?.DefaultPath; + if (string.IsNullOrWhiteSpace(path)) + { + path = _arguments.TryGetRequiredArgument(nameof(args.PfxFilePath), args?.PfxFilePath); + } + + var password = _settings.Store.PfxFile?.DefaultPassword; + if (!string.IsNullOrWhiteSpace(args?.PfxPassword)) + { + password = args.PfxPassword; + } + + if (path != null && path.ValidPath(_log)) + { + return Create(path, password); + } + else + { + throw new Exception("Invalid path specified"); + } + } + + private PfxFileOptions Create(string path, string? password) + { + var ret = new PfxFileOptions(); + if (!string.IsNullOrWhiteSpace(password) && + !string.Equals(password, _settings.Store.PfxFile?.DefaultPassword)) + { + ret.PfxPassword = new ProtectedString(password); + } + if (!string.Equals(path, _settings.Store.PfxFile?.DefaultPath, StringComparison.CurrentCultureIgnoreCase)) + { + ret.Path = path; + } + return ret; + } + } +} |