using System; using System.Net.Http; using System.Text.RegularExpressions; namespace AMWD.Net.Api.Cloudflare.Auth { /// /// Implements the interface to authenticate using an API key and email address. /// public class ApiKeyAuthentication : IAuthentication { private static readonly Regex _emailCheckRegex = new(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled); private readonly string _emailAddress; private readonly string _apiKey; /// /// Initializes a new instance of the class. /// /// The email address. /// The global API key. public ApiKeyAuthentication(string emailAddress, string apiKey) { if (string.IsNullOrWhiteSpace(emailAddress)) throw new ArgumentNullException(nameof(emailAddress)); if (string.IsNullOrWhiteSpace(apiKey)) throw new ArgumentNullException(nameof(apiKey)); if (!_emailCheckRegex.IsMatch(emailAddress)) throw new ArgumentException("Invalid email address", nameof(emailAddress)); _emailAddress = emailAddress; _apiKey = apiKey; } /// public void AddHeader(HttpClient httpClient) { httpClient.DefaultRequestHeaders.Add("X-Auth-Email", _emailAddress); httpClient.DefaultRequestHeaders.Add("X-Auth-Key", _apiKey); } } }