-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQueryStringAuthPlugin.php
114 lines (100 loc) · 3.1 KB
/
QueryStringAuthPlugin.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
<?php
/**
* @package Guzzle PHP <http://www.guzzlephp.org>
* @license See the LICENSE file that was distributed with this source code.
*/
namespace Guzzle\Aws;
use Guzzle\Common\Event\Observer;
use Guzzle\Common\Event\Subject;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Aws\Filter\AddRequiredQueryStringFilter;
use Guzzle\Aws\Filter\QueryStringSignatureFilter;
class QueryStringAuthPlugin implements Observer
{
/**
* {@inheritdoc}
*/
protected $priority = -999;
/**
* @var Signature\AbstractSignature
*/
private $signature;
/**
* @var string API version of the service
*/
private $apiVersion;
/**
* Construct a new request signing plugin
*
* @param Signature\AbstractSignature $signature Signature object used to sign requests
* @param string $apiVersion API version of the service
*/
public function __construct(Signature\AbstractSignature $signature, $apiVersion)
{
$this->signature = $signature;
$this->apiVersion = $apiVersion;
}
/**
* Get the signature object used to sign requests
*
* @return Signature\AbstractSignature
*/
public function getSignature()
{
return $this->signature;
}
/**
* Get the API version of the service
*
* @return string
*/
public function getApiVersion()
{
return $this->apiVersion;
}
/**
* Add required query string fields to a request
*
* @param RequestInterface $request Request to modify
*/
public function addRequiredQueryString(RequestInterface $request)
{
$qs = $request->getQuery();
// Add required parameters to the request
$qs->set('Timestamp', gmdate('c'));
$qs->set('Version', $this->apiVersion);
$qs->set('SignatureVersion', $this->signature->getVersion());
// Signature V2 and onward functionality
if ((int) $this->signature->getVersion() > 1) {
$qs->set('SignatureMethod', $this->signature->getAwsHashingAlgorithm());
}
$qs->set('AWSAccessKeyId', $this->signature->getAccessKeyId());
}
/**
* Add a query string signature to a request
*
* @param RequestInterface $request Request to modify
*/
public function addQueryStringSignature(RequestInterface $request)
{
$qs = $request->getQuery();
// Create a string that needs to be signed using the request settings
$strToSign = $this->signature->calculateStringToSign($qs->getAll(), array(
'endpoint' => $request->getUrl(),
'method' => $request->getMethod()
));
// Add the signature to the query string of the request
$qs->set('Signature', $this->signature->signString($strToSign));
return true;
}
/**
* {@inheritdoc}
*/
public function update(Subject $subject, $event, $context = null)
{
if ($subject instanceof RequestInterface && $event == 'request.before_send') {
$this->addRequiredQueryString($subject);
$this->addQueryStringSignature($subject);
}
}
}