Added zone security.txt functions
This commit is contained in:
@@ -13,7 +13,8 @@
|
||||
<Description>Zone management features of the Cloudflare API</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(CI_COMMIT_TAG)', '^zones\/v[0-9.]+'))">
|
||||
<!-- Only build package for tagged releases or Debug on CI (only dev NuGet feed) -->
|
||||
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(CI_COMMIT_TAG)', '^zones\/v[0-9.]+')) or ('$(Configuration)' == 'Debug' and '$(GITLAB_CI)' == 'true')">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AMWD.Net.Api.Cloudflare.Zones.Internals.Requests
|
||||
{
|
||||
internal class InternalUpdateSecurityTxtRequest
|
||||
{
|
||||
[JsonProperty("acknowledgements")]
|
||||
public IList<string>? Acknowledgements { get; set; }
|
||||
|
||||
[JsonProperty("canonical")]
|
||||
public IList<string>? Canonical { get; set; }
|
||||
|
||||
[JsonProperty("contact")]
|
||||
public IList<string>? Contact { get; set; }
|
||||
|
||||
[JsonProperty("enabled")]
|
||||
public bool? Enabled { get; set; }
|
||||
|
||||
[JsonProperty("encryption")]
|
||||
public IList<string>? Encryption { get; set; }
|
||||
|
||||
[JsonProperty("expires")]
|
||||
public DateTime? Expires { get; set; }
|
||||
|
||||
[JsonProperty("hiring")]
|
||||
public IList<string>? Hiring { get; set; }
|
||||
|
||||
[JsonProperty("policy")]
|
||||
public IList<string>? Policy { get; set; }
|
||||
|
||||
[JsonProperty("preferredLanguages")]
|
||||
public string? PreferredLanguages { get; set; }
|
||||
}
|
||||
}
|
||||
65
Extensions/Cloudflare.Zones/Models/SecurityTxt.cs
Normal file
65
Extensions/Cloudflare.Zones/Models/SecurityTxt.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||
{
|
||||
/// <summary>
|
||||
/// The security TXT record.
|
||||
/// </summary>
|
||||
public class SecurityTxt
|
||||
{
|
||||
/// <summary>
|
||||
/// The acknowledgements.
|
||||
/// </summary>
|
||||
[JsonProperty("acknowledgements")]
|
||||
public IList<string>? Acknowledgements { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The canonical.
|
||||
/// </summary>
|
||||
[JsonProperty("canonical")]
|
||||
public IList<string>? Canonical { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The contact.
|
||||
/// </summary>
|
||||
[JsonProperty("contact")]
|
||||
public IList<string>? Contact { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A value indicating whether this security.txt is enabled.
|
||||
/// </summary>
|
||||
[JsonProperty("enabled")]
|
||||
public bool? Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///The encryption.
|
||||
/// </summary>
|
||||
[JsonProperty("encryption")]
|
||||
public IList<string>? Encryption { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The expiry.
|
||||
/// </summary>
|
||||
[JsonProperty("expires")]
|
||||
public DateTime? Expires { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The hiring.
|
||||
/// </summary>
|
||||
[JsonProperty("hiring")]
|
||||
public IList<string>? Hiring { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The policies.
|
||||
/// </summary>
|
||||
[JsonProperty("policy")]
|
||||
public IList<string>? Policy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The preferred languages.
|
||||
/// </summary>
|
||||
[JsonProperty("preferredLanguages")]
|
||||
public string? PreferredLanguages { get; set; }
|
||||
}
|
||||
}
|
||||
100
Extensions/Cloudflare.Zones/Requests/UpdateSecurityTxtRequest.cs
Normal file
100
Extensions/Cloudflare.Zones/Requests/UpdateSecurityTxtRequest.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||
{
|
||||
/// <summary>
|
||||
/// Request to update security.txt
|
||||
/// </summary>
|
||||
public class UpdateSecurityTxtRequest(string zoneId)
|
||||
{
|
||||
/// <summary>
|
||||
/// The zone identifier.
|
||||
/// </summary>
|
||||
public string ZoneId { get; set; } = zoneId;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the acknowledgements.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Example: <c>https://example.com/hall-of-fame.html</c>
|
||||
/// </remarks>
|
||||
public IList<string>? Acknowledgements { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the canonical.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Example: <c>https://www.example.com/.well-known/security.txt</c>
|
||||
/// </remarks>
|
||||
public IList<string>? Canonical { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the contact.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Examples:
|
||||
/// <list type="bullet">
|
||||
/// <item>mailto:security@example.com</item>
|
||||
/// <item>tel:+1-201-555-0123</item>
|
||||
/// <item>https://example.com/security-contact.html</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public IList<string>? Contact { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this security.txt is enabled.
|
||||
/// </summary>
|
||||
public bool? Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the encryption.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Examples:
|
||||
/// <list type="bullet">
|
||||
/// <item>https://example.com/pgp-key.txt</item>
|
||||
/// <item>dns:5d2d37ab76d47d36._openpgpkey.example.com?type=OPENPGPKEY</item>
|
||||
/// <item>openpgp4fpr:5f2de5521c63a801ab59ccb603d49de44b29100f</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public IList<string>? Encryption { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the expires.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <strong>NOTE</strong>: The value will be converted to UTC when the <see cref="DateTime.Kind"/> is not <see cref="DateTimeKind.Utc"/>.
|
||||
/// </remarks>
|
||||
public DateTime? Expires { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the hiring.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Example: <c>https://example.com/jobs.html</c>
|
||||
/// </remarks>
|
||||
public IList<string>? Hiring { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the policies.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Example: <c>https://example.com/disclosure-policy.html</c>
|
||||
/// </remarks>
|
||||
public IList<string>? Policy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the preferred languages.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Examples:
|
||||
/// <list type="bullet">
|
||||
/// <item>en</item>
|
||||
/// <item>es</item>
|
||||
/// <item>fr</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public IList<string>? PreferredLanguages { get; set; }
|
||||
}
|
||||
}
|
||||
78
Extensions/Cloudflare.Zones/SecurityCenterExtensions.cs
Normal file
78
Extensions/Cloudflare.Zones/SecurityCenterExtensions.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AMWD.Net.Api.Cloudflare.Zones.Internals.Requests;
|
||||
|
||||
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for security center section of a zone.
|
||||
/// </summary>
|
||||
public static class SecurityCenterExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Delete security.txt
|
||||
/// </summary>
|
||||
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||
/// <param name="zoneId">The zone ID.</param>
|
||||
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||
public static async Task<CloudflareResponse> DeleteSecurityTxt(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
zoneId.ValidateCloudflareId();
|
||||
return await client.DeleteAsync<object>($"zones/{zoneId}/security-center/securitytxt", cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get security.txt.
|
||||
/// </summary>
|
||||
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||
/// <param name="zoneId">The zone ID.</param>
|
||||
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||
public static Task<CloudflareResponse<SecurityTxt>> GetSecurityTxt(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
zoneId.ValidateCloudflareId();
|
||||
return client.GetAsync<SecurityTxt>($"zones/{zoneId}/security-center/securitytxt", cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update security.txt
|
||||
/// </summary>
|
||||
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||
/// <param name="request">The request information.</param>
|
||||
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||
public static async Task<CloudflareResponse> UpdateSecurityTxt(this ICloudflareClient client, UpdateSecurityTxtRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
request.ZoneId.ValidateCloudflareId();
|
||||
|
||||
var req = new InternalUpdateSecurityTxtRequest();
|
||||
|
||||
if (request.Acknowledgements != null)
|
||||
req.Acknowledgements = request.Acknowledgements.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
if (request.Canonical != null)
|
||||
req.Canonical = request.Canonical.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
if (request.Contact != null)
|
||||
req.Contact = request.Contact.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
req.Enabled = request.Enabled;
|
||||
|
||||
if (request.Encryption != null)
|
||||
req.Encryption = request.Encryption.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
if (request.Expires.HasValue)
|
||||
req.Expires = request.Expires.Value.ToUniversalTime();
|
||||
|
||||
if (request.Hiring != null)
|
||||
req.Hiring = request.Hiring.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
if (request.Policy != null)
|
||||
req.Policy = request.Policy.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||
|
||||
if (request.PreferredLanguages != null)
|
||||
req.PreferredLanguages = string.Join(", ", request.PreferredLanguages.Where(s => !string.IsNullOrWhiteSpace(s)));
|
||||
|
||||
return await client.PutAsync<object, InternalUpdateSecurityTxtRequest>($"zones/{request.ZoneId}/security-center/securitytxt", req, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user