aboutsummaryrefslogtreecommitdiff
path: root/VNLib.Plugins.Essentials.Accounts.Registration/src/EmailSystemConfig.cs
blob: 238ae3718caea38a74b4ee9b468189f74f812800 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.Accounts.Registration
* File: EmailSystemConfig.cs 
*
* EmailSystemConfig.cs is part of VNLib.Plugins.Essentials.Accounts.Registration which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.Accounts.Registration is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.Accounts.Registration is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Text;
using System.Text.Json;

using RestSharp;

using Emails.Transactional.Client;

using VNLib.Utils.Extensions;
using VNLib.Net.Rest.Client;
using VNLib.Net.Rest.Client.OAuth2;
using VNLib.Plugins.Extensions.Loading;

namespace VNLib.Plugins.Essentials.Accounts.Registration
{
    /// <summary>
    /// An extended <see cref="TransactionalEmailConfig"/> configuration 
    /// object that contains a <see cref="Net.Rest.Client.RestClientPool"/> pool for making 
    /// transactions
    /// </summary>
    internal sealed class EmailSystemConfig : TransactionalEmailConfig
    {
        public const string REG_TEMPLATE_NAME = "Registration";

        public EmailSystemConfig(PluginBase pbase)
        {
            IReadOnlyDictionary<string, JsonElement> conf = pbase.GetConfig("email");
            EmailFromName = conf["from_name"].GetString() ?? throw new KeyNotFoundException("");
            EmailFromAddress = conf["from_address"].GetString() ?? throw new KeyNotFoundException("");
            Uri baseServerPath = new(conf["base_url"].GetString()!, UriKind.RelativeOrAbsolute);
            Uri tokenServerBase = new(conf["token_server_url"].GetString()!, UriKind.RelativeOrAbsolute);
            Uri transactionEndpoint = new(conf["transaction_path"].GetString()!, UriKind.RelativeOrAbsolute);
            //Load templates
            Dictionary<string, string> templates = conf["templates"].EnumerateObject().ToDictionary(jp => jp.Name, jp => jp.Value.GetString()!);
            //Init base config
            WithTemplates(templates)
            .WithUrl(transactionEndpoint);
            //Load credentials
            string authEndpoint = conf["token_path"].GetString() ?? throw new KeyNotFoundException();
            int maxClients = conf["max_clients"].GetInt32();

            
            //Load oauth secrets from vault
            Task<SecretResult?> oauth2ClientID = pbase.TryGetSecretAsync("oauth2_client_id");
            Task<SecretResult?> oauth2Password = pbase.TryGetSecretAsync("oauth2_client_secret");

            //Lazy cred loaded, tasks should be loaded before this method will ever get called
            Credential lazyCredentialGet()
            {
                //Load the results 
                SecretResult cliendId = oauth2ClientID.Result ?? throw new KeyNotFoundException("Missing required oauth2 client id");
                SecretResult password = oauth2Password.Result ?? throw new KeyNotFoundException("Missing required oauth2 client secret");

                //Creat credential
                return Credential.Create(cliendId.Result, password.Result);
            }


            //Init client creation options
            RestClientOptions poolOptions = new()
            {
                AllowMultipleDefaultParametersWithSameName = true,
                AutomaticDecompression = System.Net.DecompressionMethods.All,
                PreAuthenticate = true,
                Encoding = Encoding.UTF8,
                MaxTimeout = conf["request_timeout_ms"].GetInt32(),
                UserAgent = "Essentials.EmailRegistation",
                FollowRedirects = false,
                BaseUrl = baseServerPath
            };
            //Options for auth token endpoint
            RestClientOptions oAuth2ClientOptions = new()
            {
                AllowMultipleDefaultParametersWithSameName = true,
                AutomaticDecompression = System.Net.DecompressionMethods.All,
                PreAuthenticate = false,
                Encoding = Encoding.UTF8,
                MaxTimeout = conf["request_timeout_ms"].GetInt32(),
                UserAgent = "Essentials.EmailRegistation",
                FollowRedirects = false,
                BaseUrl = baseServerPath
            };

            //Init Oauth authenticator
            OAuth2Authenticator authenticator = new(oAuth2ClientOptions, lazyCredentialGet, authEndpoint);            
            //Store pool
            RestClientPool = new(maxClients, poolOptions, authenticator:authenticator);

            void Cleanup()
            {
                authenticator.Dispose();
                RestClientPool.Dispose();
                oauth2ClientID.Dispose();
                oauth2Password.Dispose();
            }

            //register password cleanup
            _ = pbase.UnloadToken.RegisterUnobserved(Cleanup);
        }

        /// <summary>
        /// A shared <see cref="Net.Rest.Client.RestClientPool"/> for renting configuraed 
        /// <see cref="RestClient"/>
        /// </summary>
        public RestClientPool RestClientPool { get; }
        /// <summary>
        /// A global from email address name
        /// </summary>
        public string EmailFromName { get; }
        /// <summary>
        /// A global from email address
        /// </summary>
        public string EmailFromAddress { get; }

        /// <summary>
        /// Prepares a new registration email transaction request
        /// </summary>
        /// <returns>The prepared <see cref="EmailTransactionRequest"/> object</returns>
        public EmailTransactionRequest GetRegistrationMessage()
        {
            EmailTransactionRequest req = GetTemplateRequest(REG_TEMPLATE_NAME);
            req.FromAddress = EmailFromAddress;
            req.FromName = EmailFromName;
            //set reg subject
            req.Subject = "One more step to register";
            return req;
        }
    }
}