using PKISharp.WACS.DomainObjects; using PKISharp.WACS.Plugins.Base.Factories; using PKISharp.WACS.Plugins.Interfaces; using PKISharp.WACS.Services; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace PKISharp.WACS.Plugins.ValidationPlugins { internal abstract class HttpValidationOptionsFactory : ValidationPluginOptionsFactory where TPlugin : IValidationPlugin where TOptions : HttpValidationOptions, new() { protected readonly IArgumentsService _arguments; public HttpValidationOptionsFactory(IArgumentsService arguments) => _arguments = arguments; /// /// Get webroot path manually /// public async Task> BaseAquire(Target target, IInputService input) { var allowEmtpy = AllowEmtpy(target); var path = await _arguments.TryGetArgument(null, input, WebrootHint(allowEmtpy)); while ( (!string.IsNullOrEmpty(path) && !PathIsValid(path)) || (string.IsNullOrEmpty(path) && !allowEmtpy)) { path = await _arguments.TryGetArgument(null, input, WebrootHint(allowEmtpy)); } return new TOptions { Path = path, CopyWebConfig = target.IIS || await input.PromptYesNo("Copy default web.config before validation?", false) }; } /// /// By default we don't allow emtpy paths, but FileSystem /// makes an exception because it can read from IIS /// /// /// public virtual bool AllowEmtpy(Target target) => false; /// /// Check if the webroot makes sense /// /// public virtual bool PathIsValid(string path) => false; /// /// Get webroot automatically /// public HttpValidationOptions BaseDefault(Target target) { string path = null; var allowEmpty = AllowEmtpy(target); var args = _arguments.GetArguments(); if (string.IsNullOrEmpty(path) && !allowEmpty) { path = _arguments.TryGetRequiredArgument(nameof(args.WebRoot), args.WebRoot); } if (!string.IsNullOrEmpty(path) && !PathIsValid(path)) { throw new ArgumentException($"Invalid webroot {path}: {WebrootHint(false)[0]}"); } return new TOptions { Path = path, CopyWebConfig = target.IIS || args.ManualTargetIsIIS }; } /// /// Hint to show to the user what the webroot should look like /// /// public virtual string[] WebrootHint(bool allowEmpty) { var ret = new List { "Path to the root of the site that will handle authentication" }; if (allowEmpty) { ret.Add("Leave empty to automatically read the path from IIS"); } return ret.ToArray(); } } }