From 07837600e6874eb5711a1452073aaf73798ecfa4 Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Fri, 16 Aug 2013 11:20:01 -0700 Subject: [PATCH] Added new OptInHandler and tests. --- src/Mailgun/Lists/OptInHandler.php | 34 +++++++++++++++++++ src/Mailgun/Mailgun.php | 5 +++ .../Mailgun/Tests/Lists/OptInHandlerTest.php | 32 +++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/Mailgun/Lists/OptInHandler.php create mode 100644 tests/Mailgun/Tests/Lists/OptInHandlerTest.php diff --git a/src/Mailgun/Lists/OptInHandler.php b/src/Mailgun/Lists/OptInHandler.php new file mode 100644 index 0000000..5fed661 --- /dev/null +++ b/src/Mailgun/Lists/OptInHandler.php @@ -0,0 +1,34 @@ + hash('md5', $concatStrings), 'l' => $mailingList, 'r' => $recipientAddress)))); + } + + public function validateHash($secretAppId, $uniqueHash){ + $urlParameters = json_decode(base64_decode(urldecode($uniqueHash))); + $concatStrings = $secretAppId . "" . $urlParameters->r; + + if($urlParameters->s == hash('md5', $concatStrings)){ + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index bae4838..f722c0f 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -8,6 +8,7 @@ use Mailgun\Messages\Messages; use Mailgun\Connection\Exceptions; use Mailgun\Connection\RestClient; use Mailgun\Messages\BatchMessage; +use Mailgun\Lists\OptInHandler; use Mailgun\Messages\MessageBuilder; /* @@ -75,6 +76,10 @@ class Mailgun{ return new MessageBuilder(); } + public function OptInHandler(){ + return new OptInHandler(); + } + public function BatchMessage($workingDomain, $autoSend = true){ return new BatchMessage($this->restClient, $workingDomain, $autoSend); } diff --git a/tests/Mailgun/Tests/Lists/OptInHandlerTest.php b/tests/Mailgun/Tests/Lists/OptInHandlerTest.php new file mode 100644 index 0000000..452ac72 --- /dev/null +++ b/tests/Mailgun/Tests/Lists/OptInHandlerTest.php @@ -0,0 +1,32 @@ +client = new MailgunTest("My-Super-Awesome-API-Key"); + $this->optInHandler = $this->client->OptInHandler(); + } + + public function testReturnOfGenerateHash(){ + $generatedHash = $this->optInHandler->generateHash('mytestlist@example.com', 'mysupersecretappid', 'testrecipient@example.com'); + $knownHash = "eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D"; + $this->assertEquals($generatedHash, $knownHash); + } + + public function testGoodHash(){ + $validation = $this->optInHandler->validateHash('mysupersecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D'); + $this->assertTrue($validation); + } + public function testBadHash(){ + $validation = $this->optInHandler->validateHash('mybadsecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D'); + $this->assertFalse($validation); + } +} +?> \ No newline at end of file