Skip to content

Commit 758a5de

Browse files
committed
Initial Import
Signed-off-by: Chris Hallgren <[email protected]>
1 parent 1425e0c commit 758a5de

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.phpunit.result.cache
2+
/.idea/
3+
/vendor

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2021-present, Chris Hallgren.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
# php-aws-smtp-password
22
Converts an AWS secret access key to a SMTP password.
3+
4+
This library helps you convert and IAM user credentials to SES SMTP credentials.
5+
6+
The SMTP username is the Access Key ID. The password can be derived by using a formula provided by AWS at https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
7+
8+
## Installation
9+
PhpAwsSmtpPassword convert is available on Packagist (using semantic versioning), and installation via Composer is the recommended way to install this. Just add this line to your `composer.json` file:
10+
```json
11+
"challgren/php-aws-smtp-password": "^1.0"
12+
```
13+
or run
14+
```
15+
composer require challgren/php-aws-smtp-password
16+
```
17+
18+
## A Simple example
19+
20+
```php
21+
<?php
22+
use PhpAwsSmtpPassword\PhpAwsSmtpPassword;
23+
24+
$smtp_password = PhpAwsSmtpPassword::convert($key, $region)
25+
```
26+

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "challgren/php-aws-smtp-password",
3+
"type": "library",
4+
"description": "Converts an AWS secret access key to a SMTP password.",
5+
"keywords": [
6+
"aws",
7+
"access secret",
8+
"smtp",
9+
"smpt password",
10+
"aws smtp password"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Chris Hallgren",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7.4"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^8.5 || ^9.3"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"PhpAwsSmtpPassword\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"PhpAwsSmtpPassword\\Test\\": "tests/"
33+
}
34+
}
35+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" backupGlobals="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="app">
10+
<directory>tests/TestCase/</directory>
11+
</testsuite>
12+
</testsuites>
13+
<!-- Prevent coverage reports from looking in tests, vendors, config folders -->
14+
<php>
15+
<ini name="memory_limit" value="-1"/>
16+
<!-- E_ALL & ~E_USER_DEPRECATED (16383)-->
17+
<!-- E_ALL (32767) -->
18+
<ini name="error_reporting" value="32767"/>
19+
</php>
20+
</phpunit>

src/PhpAwsSmtpPassword.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PhpAwsSmtpPassword;
4+
5+
class PhpAwsSmtpPassword
6+
{
7+
/**
8+
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
9+
* @var string Date to hash with
10+
*/
11+
protected static $date = '11111111';
12+
13+
/**
14+
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
15+
* @var string Service to hash with
16+
*/
17+
protected static $service = 'ses';
18+
19+
/**
20+
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
21+
* @var string Terminal to hash with
22+
*/
23+
protected static $terminal = 'aws4_request';
24+
25+
/**
26+
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
27+
* @var string Message to hash with
28+
*/
29+
protected static $message = 'SendRawEmail';
30+
31+
/**
32+
* Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change
33+
* @var string Version to hash with
34+
*/
35+
protected static $version = '[4]';
36+
37+
/**
38+
* Converts a secret key to a password that can be used with SMTP
39+
*
40+
* @param string $key Access secret key
41+
* @param string $region AWS Region
42+
* @return string SMTP password
43+
*/
44+
public static function convert($key, $region)
45+
{
46+
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$date, 'AWS4' . $key, true);
47+
$signature = hash_hmac('sha256', $region, $signature, true);
48+
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$service, $signature, true);
49+
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$terminal, $signature, true);
50+
$signature = hash_hmac('sha256', PhpAwsSmtpPassword::$message, $signature, true);
51+
$signatureAndVersion = PhpAwsSmtpPassword::$version . $signature;
52+
53+
return base64_encode($signatureAndVersion);
54+
}
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace PhpAwsSmtpPassword\Test\TestCase;
3+
4+
use PhpAwsSmtpPassword\PhpAwsSmtpPassword;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class PhpAwsSmtpPasswordTest extends TestCase
8+
{
9+
/**
10+
* Tests the convert function
11+
*/
12+
public function testConvert()
13+
{
14+
$key = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
15+
$region = 'us-east-1';
16+
$expected = 'WzRdsEz/2FJQQt+rwbD6tTVhwGOQ7EZuFPZcbvrfFUsZYv0=';
17+
18+
$this->assertEquals($expected, PhpAwsSmtpPassword::convert($key, $region));
19+
}
20+
}

0 commit comments

Comments
 (0)