From cd0906f4c46aad1c0c5856c44ca50f2059161c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Keresztury?= Date: Thu, 18 Jun 2020 11:25:57 +0200 Subject: [PATCH 1/2] Rearrange directories --- Mailgun.Models.SignedEvent.sln | 25 +++++++++++++++++++ .../EventDetails}/ClientInfo.cs | 0 .../EventDetails}/DeliveryStatus.cs | 0 .../EventDetails}/Envelope.cs | 0 .../EventDetails}/Flags.cs | 0 .../EventDetails}/Geolocation.cs | 0 .../EventDetails}/Message.cs | 0 .../EventDetails}/MessageDetails/Headers.cs | 0 .../EventDetails}/Reject.cs | 0 .../EventDetails}/Route.cs | 0 .../EventDetails}/Storage.cs | 0 .../Mailgun.Models.SignedEvent.csproj | 6 ++++- .../MailgunEvent.cs | 0 .../MailgunSignature.cs | 0 .../SignedEvent.cs | 0 .../stylecop.json | 0 16 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Mailgun.Models.SignedEvent.sln rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/ClientInfo.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/DeliveryStatus.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Envelope.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Flags.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Geolocation.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Message.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/MessageDetails/Headers.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Reject.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Route.cs (100%) rename {EventDetails => Mailgun.Models.SignedEvent/EventDetails}/Storage.cs (100%) rename Mailgun.Models.SignedEvent.csproj => Mailgun.Models.SignedEvent/Mailgun.Models.SignedEvent.csproj (65%) rename MailgunEvent.cs => Mailgun.Models.SignedEvent/MailgunEvent.cs (100%) rename MailgunSignature.cs => Mailgun.Models.SignedEvent/MailgunSignature.cs (100%) rename SignedEvent.cs => Mailgun.Models.SignedEvent/SignedEvent.cs (100%) rename stylecop.json => Mailgun.Models.SignedEvent/stylecop.json (100%) diff --git a/Mailgun.Models.SignedEvent.sln b/Mailgun.Models.SignedEvent.sln new file mode 100644 index 0000000..7fbd06c --- /dev/null +++ b/Mailgun.Models.SignedEvent.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30204.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailgun.Models.SignedEvent", "Mailgun.Models.SignedEvent\Mailgun.Models.SignedEvent.csproj", "{1BEB07E0-C800-45A1-990E-5F64962B8C9E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DC36B6DF-0548-4665-AF32-553ED3E0B3A4} + EndGlobalSection +EndGlobal diff --git a/EventDetails/ClientInfo.cs b/Mailgun.Models.SignedEvent/EventDetails/ClientInfo.cs similarity index 100% rename from EventDetails/ClientInfo.cs rename to Mailgun.Models.SignedEvent/EventDetails/ClientInfo.cs diff --git a/EventDetails/DeliveryStatus.cs b/Mailgun.Models.SignedEvent/EventDetails/DeliveryStatus.cs similarity index 100% rename from EventDetails/DeliveryStatus.cs rename to Mailgun.Models.SignedEvent/EventDetails/DeliveryStatus.cs diff --git a/EventDetails/Envelope.cs b/Mailgun.Models.SignedEvent/EventDetails/Envelope.cs similarity index 100% rename from EventDetails/Envelope.cs rename to Mailgun.Models.SignedEvent/EventDetails/Envelope.cs diff --git a/EventDetails/Flags.cs b/Mailgun.Models.SignedEvent/EventDetails/Flags.cs similarity index 100% rename from EventDetails/Flags.cs rename to Mailgun.Models.SignedEvent/EventDetails/Flags.cs diff --git a/EventDetails/Geolocation.cs b/Mailgun.Models.SignedEvent/EventDetails/Geolocation.cs similarity index 100% rename from EventDetails/Geolocation.cs rename to Mailgun.Models.SignedEvent/EventDetails/Geolocation.cs diff --git a/EventDetails/Message.cs b/Mailgun.Models.SignedEvent/EventDetails/Message.cs similarity index 100% rename from EventDetails/Message.cs rename to Mailgun.Models.SignedEvent/EventDetails/Message.cs diff --git a/EventDetails/MessageDetails/Headers.cs b/Mailgun.Models.SignedEvent/EventDetails/MessageDetails/Headers.cs similarity index 100% rename from EventDetails/MessageDetails/Headers.cs rename to Mailgun.Models.SignedEvent/EventDetails/MessageDetails/Headers.cs diff --git a/EventDetails/Reject.cs b/Mailgun.Models.SignedEvent/EventDetails/Reject.cs similarity index 100% rename from EventDetails/Reject.cs rename to Mailgun.Models.SignedEvent/EventDetails/Reject.cs diff --git a/EventDetails/Route.cs b/Mailgun.Models.SignedEvent/EventDetails/Route.cs similarity index 100% rename from EventDetails/Route.cs rename to Mailgun.Models.SignedEvent/EventDetails/Route.cs diff --git a/EventDetails/Storage.cs b/Mailgun.Models.SignedEvent/EventDetails/Storage.cs similarity index 100% rename from EventDetails/Storage.cs rename to Mailgun.Models.SignedEvent/EventDetails/Storage.cs diff --git a/Mailgun.Models.SignedEvent.csproj b/Mailgun.Models.SignedEvent/Mailgun.Models.SignedEvent.csproj similarity index 65% rename from Mailgun.Models.SignedEvent.csproj rename to Mailgun.Models.SignedEvent/Mailgun.Models.SignedEvent.csproj index b69f5ad..3ae2d8f 100644 --- a/Mailgun.Models.SignedEvent.csproj +++ b/Mailgun.Models.SignedEvent/Mailgun.Models.SignedEvent.csproj @@ -1,7 +1,11 @@ - netstandard1.6 + netstandard1.6;net461 + Balazs Keresztury + Copyright (c) 2020 Balazs Keresztury + MIT + 1.0.0-rc0 diff --git a/MailgunEvent.cs b/Mailgun.Models.SignedEvent/MailgunEvent.cs similarity index 100% rename from MailgunEvent.cs rename to Mailgun.Models.SignedEvent/MailgunEvent.cs diff --git a/MailgunSignature.cs b/Mailgun.Models.SignedEvent/MailgunSignature.cs similarity index 100% rename from MailgunSignature.cs rename to Mailgun.Models.SignedEvent/MailgunSignature.cs diff --git a/SignedEvent.cs b/Mailgun.Models.SignedEvent/SignedEvent.cs similarity index 100% rename from SignedEvent.cs rename to Mailgun.Models.SignedEvent/SignedEvent.cs diff --git a/stylecop.json b/Mailgun.Models.SignedEvent/stylecop.json similarity index 100% rename from stylecop.json rename to Mailgun.Models.SignedEvent/stylecop.json From e919ec75f4547dcaa58ce6aefdc4a7d482c79ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Keresztury?= Date: Thu, 18 Jun 2020 12:28:11 +0200 Subject: [PATCH 2/2] Add tests --- .../Mailgun.Models.SignedEvent.Tests.csproj | 34 +++++++ .../MailgunSignatureTests.cs | 91 +++++++++++++++++++ .../stylecop.json | 16 ++++ Mailgun.Models.SignedEvent.sln | 6 ++ 4 files changed, 147 insertions(+) create mode 100644 Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj create mode 100644 Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs create mode 100644 Mailgun.Models.SignedEvent.Tests/stylecop.json diff --git a/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj b/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj new file mode 100644 index 0000000..931cdd5 --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj @@ -0,0 +1,34 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs b/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs new file mode 100644 index 0000000..7abe5a1 --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs @@ -0,0 +1,91 @@ +// +// Copyright (c) Balazs Keresztury. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using NUnit.Framework; + +namespace Mailgun.Models.SignedEvent.Tests +{ + /// + /// Tests MailgunSignature. + /// + public class MailgunSignatureTests + { + private DateTime _originalTimestamp; + private TimeSpan _timeSinceOriginalTimestamp; + private MailgunSignature _validSignature; + private string _apiKey; + + /// + /// Sets up test fixture with a know valid signature. + /// + [SetUp] + public void Setup() + { + this._originalTimestamp = new DateTime(2020, 6, 18, 11, 55, 0).ToUniversalTime(); + var originalTimestampAsUnixEpoch = (this._originalTimestamp - DateTime.UnixEpoch).TotalSeconds.ToString(); + this._timeSinceOriginalTimestamp = DateTime.UtcNow - this._originalTimestamp; + this._apiKey = "ffffffffffffffffffffffffffffffff-ffffffff-ffffffff"; + + this._validSignature = new MailgunSignature() + { + Signature = "de4b938580bb4d84f710cbb8bfa7d224bb2262c8f644f558c2901c1ae645bb03", + Token = "ffffffffffffffffffffffffffffffffffffffffffffffffff", + Timestamp = originalTimestampAsUnixEpoch, + }; + } + + /// + /// Checks if a valid signature returns as valid. + /// + [Test] + public void Valid() + { + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.True); + } + + /// + /// Checks if a slightly old signature returns invalid. + /// + [Test] + public void TooOld() + { + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp - new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the signature invalidates it. + /// + [Test] + public void BadSignature() + { + this._validSignature.Signature += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the token invalidates the signature. + /// + [Test] + public void BadToken() + { + this._validSignature.Token += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the API key invalidates the signature. + /// + [Test] + public void BadApiKey() + { + this._apiKey += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + } +} \ No newline at end of file diff --git a/Mailgun.Models.SignedEvent.Tests/stylecop.json b/Mailgun.Models.SignedEvent.Tests/stylecop.json new file mode 100644 index 0000000..3f0420f --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/stylecop.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "documentationRules": { + "companyName": "Balazs Keresztury", + "copyrightText": "Copyright (c) {companyName}. All rights reserved.\nLicensed under the {licenseName} license. See {licenseFile} file in the project root for full license information.", + "variables": { + "licenseName": "MIT", + "licenseFile": "LICENSE" + } + }, + "orderingRules": { + "usingDirectivesPlacement": "outsideNamespace" + } + } +} diff --git a/Mailgun.Models.SignedEvent.sln b/Mailgun.Models.SignedEvent.sln index 7fbd06c..53a85fb 100644 --- a/Mailgun.Models.SignedEvent.sln +++ b/Mailgun.Models.SignedEvent.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30204.135 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailgun.Models.SignedEvent", "Mailgun.Models.SignedEvent\Mailgun.Models.SignedEvent.csproj", "{1BEB07E0-C800-45A1-990E-5F64962B8C9E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailgun.Models.SignedEvent.Tests", "Mailgun.Models.SignedEvent.Tests\Mailgun.Models.SignedEvent.Tests.csproj", "{8E4EF695-2A4F-4FF9-913D-541F4AF7F841}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.ActiveCfg = Release|Any CPU {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.Build.0 = Release|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE