Browse Source

Added ability to specify message signature

tags/2.0.6
Klimov Paul 9 years ago
parent
commit
338ec46f26
  1. 2
      CHANGELOG.md
  2. 97
      Message.php
  3. 98
      tests/MessageTest.php

2
CHANGELOG.md

@ -4,7 +4,7 @@ Yii Framework 2 swiftmailer extension Change Log
2.0.6 under development 2.0.6 under development
----------------------- -----------------------
- no changes in this release. - Enh #27: Added ability to specify message signature (klimov-paul)
2.0.5 March 17, 2016 2.0.5 March 17, 2016

97
Message.php

@ -7,6 +7,9 @@
namespace yii\swiftmailer; namespace yii\swiftmailer;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\mail\BaseMessage; use yii\mail\BaseMessage;
/** /**
@ -28,6 +31,10 @@ class Message extends BaseMessage
* @var \Swift_Message Swift message instance. * @var \Swift_Message Swift message instance.
*/ */
private $_swiftMessage; private $_swiftMessage;
/**
* @var \Swift_Signer[] attached signers
*/
private $signers = [];
/** /**
@ -301,6 +308,96 @@ class Message extends BaseMessage
} }
/** /**
* Sets message signature
* @param array|callable|\Swift_Signer $signature signature specification.
* See [[addSignature()]] for details on how it should be specified.
* @return $this self reference.
* @since 2.0.6
*/
public function setSignature($signature)
{
if (!empty($this->signers)) {
// clear previously set signers
$swiftMessage = $this->getSwiftMessage();
foreach ($this->signers as $signer) {
$swiftMessage->detachSigner($signer);
}
$this->signers = [];
}
return $this->addSignature($signature);
}
/**
* Adds message signature.
* @param array|callable|\Swift_Signer $signature signature specification, this can be:
*
* - [[\Swift_Signer]] instance
* - callable, which returns [[\Swift_Signer]] instance
* - configuration array for the signer creation
*
* @return $this self reference
* @throws InvalidConfigException on invalid signature configuration
* @since 2.0.6
*/
public function addSignature($signature)
{
if ($signature instanceof \Swift_Signer) {
$signer = $signature;
} elseif (is_callable($signature)) {
$signer = call_user_func($signature);
} elseif (is_array($signature)) {
$signer = $this->createSwiftSigner($signature);
} else {
throw new InvalidConfigException('Signature should be instance of "Swift_Signer", callable or array configuration');
}
$this->getSwiftMessage()->attachSigner($signer);
$this->signers[] = $signer;
return $this;
}
/**
* Creates signer from its configuration
* @param array $signature signature configuration
* @return \Swift_Signer signer instance
* @throws InvalidConfigException on invalid configuration provided
* @since 2.0.6
*/
protected function createSwiftSigner($signature)
{
if (!isset($signature['type'])) {
throw new InvalidConfigException('Signature configuration should contain "type" key');
}
switch (strtolower($signature['type'])) {
case 'dkim' :
$domain = ArrayHelper::getValue($signature, 'domain', null);
$selector = ArrayHelper::getValue($signature, 'selector', null);
if (isset($signature['key'])) {
$privateKey = $signature['key'];
} elseif (isset($signature['file'])) {
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
} else {
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
}
return new \Swift_Signers_DKIMSigner($privateKey, $domain, $selector);
case 'opendkim' :
$domain = ArrayHelper::getValue($signature, 'domain', null);
$selector = ArrayHelper::getValue($signature, 'selector', null);
if (isset($signature['key'])) {
$privateKey = $signature['key'];
} elseif (isset($signature['file'])) {
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
} else {
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
}
return new \Swift_Signers_OpenDKIMSigner($privateKey, $domain, $selector);
default:
throw new InvalidConfigException("Unrecognized signature type '{$signature['type']}'");
}
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function toString() public function toString()

98
tests/MessageTest.php

@ -19,7 +19,8 @@ class MessageTest extends TestCase
/** /**
* @var string test email address, which will be used as receiver for the messages. * @var string test email address, which will be used as receiver for the messages.
*/ */
protected $testEmailReceiver = 'someuser@somedomain.com'; //protected $testEmailReceiver = 'someuser@somedomain.com';
protected $testEmailReceiver = 'klimov.paul@gmail.com';
public function setUp() public function setUp()
{ {
@ -110,6 +111,24 @@ class MessageTest extends TestCase
return $attachment; return $attachment;
} }
/**
* @param Message $message
* @return array list of attached swift signers
*/
protected function getSwiftSigners(Message $message)
{
$swiftMessage = $message->getSwiftMessage();
$reflection = new \ReflectionObject($message->getSwiftMessage());
$headerSignersReflection = $reflection->getProperty('headerSigners');
$headerSignersReflection->setAccessible(true);
$bodySignersReflection = $reflection->getProperty('bodySigners');
$bodySignersReflection->setAccessible(true);
return array_merge(
$headerSignersReflection->getValue($swiftMessage),
$bodySignersReflection->getValue($swiftMessage)
);
}
// Tests : // Tests :
public function testGetSwiftMessage() public function testGetSwiftMessage()
@ -186,6 +205,37 @@ class MessageTest extends TestCase
$this->assertContains('Bcc: ' . $bcc, $messageString, 'Incorrect "Bcc" header!'); $this->assertContains('Bcc: ' . $bcc, $messageString, 'Incorrect "Bcc" header!');
} }
public function testSetupSignature()
{
$message = new Message();
$message->addSignature([
'type' => 'dkim',
'key' => 'private key',
]);
$signers = $this->getSwiftSigners($message);
$this->assertTrue($signers[0] instanceof \Swift_Signers_DKIMSigner);
$signer = new \Swift_Signers_DKIMSigner('manual', null, null);
$message->addSignature($signer);
$signers = $this->getSwiftSigners($message);
$this->assertSame($signer, $signers[1]);
$signer = new \Swift_Signers_DKIMSigner('callable', null, null);
$message->addSignature(function () use ($signer) {
return $signer;
});
$signers = $this->getSwiftSigners($message);
$this->assertSame($signer, $signers[2]);
$message->setSignature([
'type' => 'dkim',
'key' => 'override',
]);
$signers = $this->getSwiftSigners($message);
$this->assertCount(1, $signers);
}
/** /**
* @depends testGetSwiftMessage * @depends testGetSwiftMessage
*/ */
@ -202,6 +252,52 @@ class MessageTest extends TestCase
/** /**
* @depends testSend * @depends testSend
*/ */
public function testSendSigned()
{
$privateKey = "-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyehiMTRxvfQz8nbQQAgL481QipVMF+E7ljWKHTQQSYfqktR+
zFYqX81vKeK9/2D6AiK5KJSBVdF7aURasppuDaxFJWrPvacd3IQCrGxsGkwwlWPO
ggB1WpOEKhVUZnGzdm96Fk23oHFKrEiQlSG0cB9P/wUKz57b8tsaPve5sKBG0Kww
9YIDRM0x4w3c9fupPz8H5p2HHn4uPbn+whJyALZHD1+CftIGOHq8AUH4w4Z7bjF4
DD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UR71kG5aT/idYFvHNy513CYtIXxY
zYzI1dOsUC6GvIZbDZgO0Jm7MMEMiVM8eIsLfGlzRm82RkSsbDsuPf183L/rTj46
tphI6QKBgQDobarzJhVUdME4QKAlhJecKBO1xlVCXWbKGdRcJn0Gzq6iwZKdx64C
hQGpKaZBDDCHLk7dDzoKXF1udriW9EcImh09uIKGYYWS8poy8NUzmZ3fy/1o2C2O
U41eAdnQ3dDGzUNedIJkSh6Z0A4VMZIEOag9hPNYqQXZBQgfobvPKw==
-----END RSA PRIVATE KEY-----
";
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Signed message');
$message->setTextBody('Signed message body');
$message->setSignature([
'type' => 'dkim',
'key' => $privateKey,
]);
$this->assertTrue($message->send());
}
/**
* @depends testSend
*/
public function testAttachFile() public function testAttachFile()
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();

Loading…
Cancel
Save