Skip to content

Commit 6c103db

Browse files
committed
Adding support to check regions
Signed-off-by: Chris Hallgren <[email protected]>
1 parent 758a5de commit 6c103db

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.phpunit.result.cache
22
/.idea/
33
/vendor
4+
/composer.lock

src/PhpAwsSmtpPassword.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44

55
class PhpAwsSmtpPassword
66
{
7+
protected static $allowed_regions = [
8+
'us-east-1', # US East (N. Virginia)
9+
'us-east-2', # US East (Ohio)
10+
'us-west-1', # US West (N. California)
11+
'us-west-2', # US West (Oregon)
12+
'af-south-1', # Africa (Cape Town)
13+
'ap-south-1', # Asia Pacific (Mumbai)
14+
'ap-northeast-2', # Asia Pacific (Seoul)
15+
'ap-southeast-1', # Asia Pacific (Singapore)
16+
'ap-southeast-2', # Asia Pacific (Sydney)
17+
'ap-northeast-1', # Asia Pacific (Tokyo)
18+
'ca-central-1', # Canada (Central)
19+
'eu-central-1', # Europe (Frankfurt)
20+
'eu-west-1', # Europe (Ireland)
21+
'eu-west-2', # Europe (London)
22+
'eu-south-1', # Europe (Milan)
23+
'eu-west-3', # Europe (Paris)
24+
'eu-north-1', # Europe (Stockholm)
25+
'me-south-1', # Middle East (Bahrain)
26+
'sa-east-1', # South America (Sao Paulo)
27+
'us-gov-west-1', # AWS GovCloud (US)
28+
];
29+
730
/**
831
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
932
* @var string Date to hash with
@@ -43,6 +66,10 @@ class PhpAwsSmtpPassword
4366
*/
4467
public static function convert($key, $region)
4568
{
69+
$region = strtolower($region);
70+
if (!in_array($region, PhpAwsSmtpPassword::$allowed_regions)) {
71+
throw new \InvalidArgumentException($region . ' is not setup for SES.');
72+
}
4673
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$date, 'AWS4' . $key, true);
4774
$signature = hash_hmac('sha256', $region, $signature, true);
4875
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$service, $signature, true);

tests/TestCase/PhpAwsSmtpPasswordTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66

77
class PhpAwsSmtpPasswordTest extends TestCase
88
{
9+
protected $key = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
10+
protected $region = 'us-east-1';
11+
912
/**
1013
* Tests the convert function
1114
*/
1215
public function testConvert()
1316
{
14-
$key = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
15-
$region = 'us-east-1';
1617
$expected = 'WzRdsEz/2FJQQt+rwbD6tTVhwGOQ7EZuFPZcbvrfFUsZYv0=';
1718

18-
$this->assertEquals($expected, PhpAwsSmtpPassword::convert($key, $region));
19+
$this->assertEquals($expected, PhpAwsSmtpPassword::convert($this->key, $this->region));
20+
}
21+
22+
public function testConvertInvalidRegion()
23+
{
24+
$this->expectException('InvalidArgumentException');
25+
$this->expectExceptionMessage('us-central-1 is not setup for SES.');
26+
PhpAwsSmtpPassword::convert($this->key, 'us-central-1');
1927
}
2028
}

0 commit comments

Comments
 (0)