summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Plugins/ValidationPlugins/Validation.cs
blob: e5e42f49a2f180f3fefe5e33494237a192d0d0f7 (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
using ACMESharp.Authorizations;
using PKISharp.WACS.Plugins.Interfaces;
using System;
using System.Threading.Tasks;

namespace PKISharp.WACS.Plugins.ValidationPlugins
{
    /// <summary>
    /// Base implementation for all validation plugins
    /// </summary>
    public abstract class Validation<TChallenge> : IValidationPlugin where TChallenge : class, IChallengeValidationDetails
    {
        public bool HasChallenge => _challenge != null;
        public TChallenge Challenge 
        {
            get
            {
                if (_challenge == null)
                {
                    throw new InvalidOperationException();
                }
                return _challenge;
            }
        }
        private TChallenge? _challenge;


        /// <summary>
        /// Handle the challenge
        /// </summary>
        /// <param name="challenge"></param>
        public async Task PrepareChallenge(IChallengeValidationDetails challenge)
        {
            if (challenge is TChallenge typed)
            {
                _challenge = typed;
                await PrepareChallenge();
            } 
            else
            {
                throw new InvalidOperationException("Unexpected challenge type");
            }
        }

        /// <summary>
        /// Handle the challenge
        /// </summary>
        /// <param name="challenge"></param>
        public abstract Task PrepareChallenge();

        /// <summary>
        /// Clean up after validation
        /// </summary>
        public abstract Task CleanUp();

        /// <summary>
        /// Is the plugin currently disabled
        /// </summary>
        public virtual bool Disabled => false;
    }
}