Skip to content

Commit f3ef60f

Browse files
committed
Optimize DkimPublicKeyRecordParser
1 parent 5e29481 commit f3ef60f

5 files changed

+116
-3
lines changed

src/Nager.EmailAuthentication/DkimPublicKeyRecordDataFragmentParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static ParsingResult[] ValidateGranularity(ValidateRequest validateReque
161161
{
162162
Status = ParsingStatus.Warning,
163163
Field = validateRequest.Field,
164-
Message = "Granularity is deprecated."
164+
Message = "Granularity is deprecated. Introduced in RFC 4871 and removed in RFC 6376."
165165
}
166166
];
167167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Nager.EmailAuthentication.Models;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace Nager.EmailAuthentication
5+
{
6+
/// <summary>
7+
/// Dkim Public Key Record Parser
8+
/// </summary>
9+
public static class DkimPublicKeyRecordParser
10+
{
11+
/// <summary>
12+
/// TryParse
13+
/// </summary>
14+
/// <param name="dkimPublicKeyRecordRaw"></param>
15+
/// <param name="dkimPublicKeyRecord"></param>
16+
/// <returns></returns>
17+
public static bool TryParse(
18+
string? dkimPublicKeyRecordRaw,
19+
[NotNullWhen(true)] out DkimPublicKeyRecord? dkimPublicKeyRecord)
20+
{
21+
if (!DkimPublicKeyRecordDataFragmentParser.TryParse(dkimPublicKeyRecordRaw, out var dataFragment, out _))
22+
{
23+
dkimPublicKeyRecord = null;
24+
return false;
25+
}
26+
27+
return TryParse(dataFragment, out dkimPublicKeyRecord);
28+
}
29+
30+
/// <summary>
31+
/// TryParse
32+
/// </summary>
33+
/// <param name="dkimPublicKeyRecordRaw"></param>
34+
/// <param name="dkimPublicKeyRecord"></param>
35+
/// <param name="parsingResults"></param>
36+
/// <returns></returns>
37+
public static bool TryParse(
38+
string? dkimPublicKeyRecordRaw,
39+
[NotNullWhen(true)] out DkimPublicKeyRecord? dkimPublicKeyRecord,
40+
out ParsingResult[]? parsingResults)
41+
{
42+
if (!DkimPublicKeyRecordDataFragmentParser.TryParse(dkimPublicKeyRecordRaw, out var dataFragment, out parsingResults))
43+
{
44+
dkimPublicKeyRecord = null;
45+
return false;
46+
}
47+
48+
return TryParse(dataFragment, out dkimPublicKeyRecord);
49+
}
50+
51+
/// <summary>
52+
/// TryParse
53+
/// </summary>
54+
/// <param name="dkimPublicKeyRecordDataFragment"></param>
55+
/// <param name="dkimPublicKeyRecord"></param>
56+
/// <returns></returns>
57+
public static bool TryParse(
58+
DkimPublicKeyRecordDataFragment? dkimPublicKeyRecordDataFragment,
59+
[NotNullWhen(true)] out DkimPublicKeyRecord? dkimPublicKeyRecord)
60+
{
61+
dkimPublicKeyRecord = null;
62+
63+
if (dkimPublicKeyRecordDataFragment == null)
64+
{
65+
return false;
66+
}
67+
68+
dkimPublicKeyRecord = new DkimPublicKeyRecord
69+
{
70+
Version = dkimPublicKeyRecordDataFragment.Version ?? "DKIM1",
71+
KeyType = dkimPublicKeyRecordDataFragment.KeyType ?? "rsa",
72+
PublicKeyData = dkimPublicKeyRecordDataFragment.PublicKeyData ?? string.Empty,
73+
Notes = dkimPublicKeyRecordDataFragment.Notes,
74+
Flags = dkimPublicKeyRecordDataFragment.Flags,
75+
};
76+
77+
return true;
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Nager.EmailAuthentication.Models
2+
{
3+
/// <summary>
4+
/// Dkim Public Key Record
5+
/// </summary>
6+
public class DkimPublicKeyRecord
7+
{
8+
/// <summary>
9+
/// Dkim Version <strong>(v=)</strong>
10+
/// </summary>
11+
public required string Version { get; set; } = "DKIM1";
12+
13+
/// <summary>
14+
/// Key Type <strong>(k=)</strong>
15+
/// </summary>
16+
public string KeyType { get; set; } = "rsa";
17+
18+
/// <summary>
19+
/// Notes that may be of interest to a human <strong>(n=)</strong>
20+
/// </summary>
21+
public string? Notes { get; set; }
22+
23+
/// <summary>
24+
/// Public key data <strong>(p=)</strong>
25+
/// </summary>
26+
public required string PublicKeyData { get; set; }
27+
28+
/// <summary>
29+
/// A set of flags that define boolean attributes <strong>(t=)</strong>
30+
/// </summary>
31+
public string? Flags { get; set; }
32+
}
33+
}

src/Nager.EmailAuthentication/Models/DkimPublicKeyRecordDataFragment.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DkimPublicKeyRecordDataFragment
2626
public string? PublicKeyData { get; set; }
2727

2828
/// <summary>
29-
/// Granularity of the key
29+
/// Granularity of the key <strong>(g=)</strong>, introduced in RFC 4871 and removed in RFC 6376.
3030
/// </summary>
3131
public string? Granularity { get; set; }
3232

src/Nager.EmailAuthentication/Nager.EmailAuthentication.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
2222

23-
<Version>1.5.4</Version>
23+
<Version>1.5.5</Version>
2424
</PropertyGroup>
2525

2626
<ItemGroup>

0 commit comments

Comments
 (0)