forked from aws/aws-sdk-php
/
Message.php
121 lines (105 loc) · 3.27 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Aws\Sns\MessageValidator;
class Message
{
private static $requiredKeys = [
'__default' => ['Message', 'MessageId', 'Timestamp', 'TopicArn',
'Type', 'Signature', 'SigningCertURL',],
'SubscriptionConfirmation' => ['SubscribeURL', 'Token'],
'UnsubscribeConfirmation' => ['SubscribeURL', 'Token']
];
private static $signableKeys = ['Message', 'MessageId', 'Subject',
'SubscribeURL', 'Timestamp', 'Token', 'TopicArn', 'Type'];
/** @var array The message data */
private $data;
/**
* Creates a Message object from an array of raw message data
*
* @param array $data The message data
*
* @return Message
* @throws \InvalidArgumentException If a valid type is not provided or
* there are other required keys missing
*/
public static function fromArray(array $data)
{
// Make sure the type key is set
if (!isset($data['Type'])) {
throw new \InvalidArgumentException('The "Type" key must be '
. 'provided to instantiate a Message object.');
}
// Determine required keys and create a collection from the message data
$requiredKeys = array_merge(
self::$requiredKeys['__default'],
isset(self::$requiredKeys[$data['Type']])
? self::$requiredKeys[$data['Type']]
: []
);
foreach ($requiredKeys as $key) {
if (empty($data[$key])) {
throw new \InvalidArgumentException($key . ' is required');
}
}
return new self($data);
}
/**
* Creates a message object from the raw POST data
*
* @return Message
* @throws \RuntimeException If the POST data is absent, or not a valid JSON document
*/
public static function fromRawPostData()
{
if (!isset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE'])) {
throw new \RuntimeException('SNS message type header not provided.');
}
$data = json_decode(file_get_contents('php://input'), true);
if (!is_array($data)) {
throw new \RuntimeException('POST data invalid');
}
return self::fromArray($data);
}
/**
* @param array $data Message data with all required keys.
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Get the entire message data as an array.
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Gets a single key from the message data
*
* @param string $key Key to retrieve
*
* @return string
*/
public function get($key)
{
return isset($this->data[$key]) ? $this->data[$key] : null;
}
/**
* Builds a newline delimited string to sign according to the specs
*
* @return string
* @link http://docs.aws.amazon.com/sns/latest/gsg/SendMessageToHttp.verify.signature.html
*/
public function getStringToSign()
{
$stringToSign = '';
foreach (self::$signableKeys as $key) {
if ($value = $this->get($key)) {
$stringToSign .= "{$key}\n{$value}\n";
}
}
return $stringToSign;
}
}