Skip to content

Commit f00193a

Browse files
committed
Optimize DkimPublicKey model
1 parent 6d8c0dd commit f00193a

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

src/Nager.EmailAuthentication/DkimPublicKeyRecordParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public static bool TryParseV1(
116116
PublicKeyData = dkimPublicKeyRecordDataFragment.PublicKeyData ?? string.Empty,
117117
Notes = dkimPublicKeyRecordDataFragment.Notes,
118118
Flags = dkimPublicKeyRecordDataFragment.Flags,
119+
AcceptableHashAlgorithms = dkimPublicKeyRecordDataFragment.AcceptableHashAlgorithms,
120+
ServiceType = dkimPublicKeyRecordDataFragment.ServiceType ?? "*"
119121
};
120122

121123
return true;

src/Nager.EmailAuthentication/FragmentParsers/DkimPublicKeyRecordDataFragmentParserV1.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,20 @@ public static bool TryParse(
8484
Map = (dataFragment, value) => dataFragment.Granularity = value,
8585
Validate = ValidateGranularity
8686
}
87-
}
87+
},
88+
{
89+
"h", new MappingHandler<DkimPublicKeyRecordDataFragmentV1>
90+
{
91+
Map = (dataFragment, value) => dataFragment.AcceptableHashAlgorithms = value
92+
}
93+
},
94+
{
95+
"s", new MappingHandler<DkimPublicKeyRecordDataFragmentV1>
96+
{
97+
Map = (dataFragment, value) => dataFragment.ServiceType = value,
98+
Validate = ValidateServiceType
99+
}
100+
},
88101
};
89102

90103
var parserBase = new KeyValueParserBase<DkimPublicKeyRecordDataFragmentV1>(handlers);
@@ -183,5 +196,25 @@ private static ParsingResult[] ValidateKeyType(ValidateRequest validateRequest)
183196
}
184197
];
185198
}
199+
200+
private static ParsingResult[] ValidateServiceType(ValidateRequest validateRequest)
201+
{
202+
var allowedServiceTypes = new string[] { "*", "email" };
203+
204+
if (allowedServiceTypes.Contains(validateRequest.Value, StringComparer.CurrentCultureIgnoreCase))
205+
{
206+
return [];
207+
}
208+
209+
return
210+
[
211+
new ParsingResult
212+
{
213+
Status = ParsingStatus.Critical,
214+
Field = validateRequest.Field,
215+
Message = "The ServiceType is invalid"
216+
}
217+
];
218+
}
186219
}
187220
}

src/Nager.EmailAuthentication/Models/DkimPublicKeyRecordDataFragmentV1.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,22 @@ public class DkimPublicKeyRecordDataFragmentV1 : DkimPublicKeyRecordDataFragment
2323
/// <summary>
2424
/// Granularity of the key <strong>(g=)</strong>, introduced in RFC 4871 and removed in RFC 6376.
2525
/// </summary>
26+
/// <remarks>set to obsolete</remarks>
2627
public string? Granularity { get; set; }
2728

2829
/// <summary>
2930
/// A set of flags that define boolean attributes <strong>(t=)</strong>
3031
/// </summary>
3132
public string? Flags { get; set; }
33+
34+
/// <summary>
35+
/// A colon-separated list of service types to which this record applies <strong>(s=)</strong>
36+
/// </summary>
37+
public string? ServiceType { get; set; }
38+
39+
/// <summary>
40+
/// Acceptable hash algorithms <strong>(h=)</strong>
41+
/// </summary>
42+
public string? AcceptableHashAlgorithms { get; set; }
3243
}
3344
}

src/Nager.EmailAuthentication/Models/DkimPublicKeyRecordV1.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@ public class DkimPublicKeyRecordV1 : DkimPublicKeyRecordBase
2424
/// A set of flags that define boolean attributes <strong>(t=)</strong>
2525
/// </summary>
2626
public string? Flags { get; set; }
27+
28+
/// <summary>
29+
/// A colon-separated list of service types to which this record applies <strong>(s=)</strong>
30+
/// </summary>
31+
public string ServiceType { get; set; } = "*";
32+
33+
/// <summary>
34+
/// Acceptable hash algorithms <strong>(h=)</strong>
35+
/// </summary>
36+
public string? AcceptableHashAlgorithms { get; set; }
2737
}
2838
}

src/Nager.EmailAuthentication/Nager.EmailAuthentication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
<Version>2.0.3</Version>
23+
<Version>2.0.4</Version>
2424
</PropertyGroup>
2525

2626
<ItemGroup>

0 commit comments

Comments
 (0)