Skip to content

Commit

Permalink
Satisfy tests, remove obsolete overload
Browse files Browse the repository at this point in the history
  • Loading branch information
esskar committed Jan 6, 2025
1 parent eb45fa7 commit fb5b361
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
7 changes: 0 additions & 7 deletions csharp/Svix/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace Svix
{
internal static class Utils
{
public static bool SecureCompare(string a, string b)
{
ArgumentNullException.ThrowIfNull(a);
ArgumentNullException.ThrowIfNull(b);
return SecureCompare(a.AsSpan(), b.AsSpan());
}

// Borrowed from Stripe-dotnet https://github.com/stripe/stripe-dotnet/blob/7b62c461d7c0cf2c9e06dce5e564b374a9d232e0/src/Stripe.net/Infrastructure/StringUtils.cs#L30
// basically identical to SecureCompare from Rails::ActiveSupport used in our ruby lib
[MethodImpl(MethodImplOptions.NoOptimization)]
Expand Down
33 changes: 23 additions & 10 deletions csharp/Svix/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,38 @@ public Webhook(byte[] key)

public void Verify(ReadOnlySpan<char> payload, WebHeaderCollection headers)
{
ArgumentNullException.ThrowIfNull(headers);
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
if (headers == null)
{
throw new ArgumentNullException(nameof(headers));
}

Verify(payload, headers.Get);
}

public void Verify(string payload, Func<string, string> headersProvider)
public void Verify(ReadOnlySpan<char> payload, Func<string, string> headersProvider)
{
ArgumentNullException.ThrowIfNull(payload);
ArgumentNullException.ThrowIfNull(headersProvider);
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
if (headersProvider == null)
{
throw new ArgumentNullException(nameof(headersProvider));
}

ReadOnlySpan<char> msgId = headerProvider(SVIX_ID_HEADER_KEY);
ReadOnlySpan<char> msgTimestamp = headerProvider(SVIX_TIMESTAMP_HEADER_KEY);
ReadOnlySpan<char> msgSignature = headerProvider(SVIX_SIGNATURE_HEADER_KEY);
ReadOnlySpan<char> msgId = headersProvider(SVIX_ID_HEADER_KEY);
ReadOnlySpan<char> msgTimestamp = headersProvider(SVIX_TIMESTAMP_HEADER_KEY);
ReadOnlySpan<char> msgSignature = headersProvider(SVIX_SIGNATURE_HEADER_KEY);

if (msgId.IsEmpty || msgSignature.IsEmpty || msgTimestamp.IsEmpty)
{
msgId = headerProvider(UNBRANDED_ID_HEADER_KEY);
msgSignature = headerProvider(UNBRANDED_SIGNATURE_HEADER_KEY);
msgTimestamp = headerProvider(UNBRANDED_TIMESTAMP_HEADER_KEY);
msgId = headersProvider(UNBRANDED_ID_HEADER_KEY);
msgSignature = headersProvider(UNBRANDED_SIGNATURE_HEADER_KEY);
msgTimestamp = headersProvider(UNBRANDED_TIMESTAMP_HEADER_KEY);
if (msgId.IsEmpty || msgSignature.IsEmpty || msgTimestamp.IsEmpty)
{
throw new WebhookVerificationException("Missing Required Headers");
Expand Down

0 comments on commit fb5b361

Please sign in to comment.