summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Plugins/ValidationPlugins/Http/HttpValidationOptionsFactory.cs
blob: f98a2e80bdefe7048ec592243c3d4dea4abf46e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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<TPlugin, TOptions> :
        ValidationPluginOptionsFactory<TPlugin, TOptions>
        where TPlugin : IValidationPlugin
        where TOptions : HttpValidationOptions<TPlugin>, new()
    {
        protected readonly IArgumentsService _arguments;

        public HttpValidationOptionsFactory(IArgumentsService arguments) => _arguments = arguments;

        /// <summary>
        /// Get webroot path manually
        /// </summary>
        public async Task<HttpValidationOptions<TPlugin>> 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)
            };
        }

        /// <summary>
        /// By default we don't allow emtpy paths, but FileSystem 
        /// makes an exception because it can read from IIS
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public virtual bool AllowEmtpy(Target target) => false;

        /// <summary>
        /// Check if the webroot makes sense
        /// </summary>
        /// <returns></returns>
        public virtual bool PathIsValid(string path) => false;

        /// <summary>
        /// Get webroot automatically
        /// </summary>
        public HttpValidationOptions<TPlugin> BaseDefault(Target target)
        {
            string? path = null;
            var allowEmpty = AllowEmtpy(target);
            var args = _arguments.GetArguments<HttpValidationArguments>();
            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
            };
        }

        /// <summary>
        /// Hint to show to the user what the webroot should look like
        /// </summary>
        /// <returns></returns>
        public virtual string[] WebrootHint(bool allowEmpty)
        {
            var ret = new List<string> { "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();
        }
    }

}