|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acquia\Search; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Common\Event; |
|
6
|
|
|
use Guzzle\Http\Message\EntityEnclosingRequest; |
|
7
|
|
|
use Guzzle\Http\Message\Request; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Guzzle plugin that adds Acquia Search credentials. |
|
12
|
|
|
*/ |
|
13
|
|
|
class AcquiaSearchAuthPlugin implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $indexId; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Acquia\Search\Signature |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $signature; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $indexId |
|
27
|
|
|
* @param \Acquia\Search\Signature $signature |
|
28
|
|
|
*/ |
|
29
|
27 |
|
public function __construct($indexId, Signature $signature) |
|
30
|
|
|
{ |
|
31
|
27 |
|
$this->indexId = $indexId; |
|
32
|
27 |
|
$this->signature = $signature; |
|
33
|
27 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
21 |
|
public static function getSubscribedEvents() |
|
39
|
|
|
{ |
|
40
|
|
|
return array( |
|
41
|
21 |
|
'request.before_send' => array('onRequestBeforeSend', -1000) |
|
42
|
21 |
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $indexId |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Acquia\Search\AcquiaSearchPligin |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function setIndexId($indexId) |
|
51
|
|
|
{ |
|
52
|
3 |
|
$this->indexId = $indexId; |
|
53
|
3 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function getIndexId() |
|
60
|
|
|
{ |
|
61
|
6 |
|
return $this->indexId; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function getDerivedKey() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->signature->getSecretKey(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Request before-send event handler. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Guzzle\Common\Event $event |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function onRequestBeforeSend(Event $event) |
|
78
|
|
|
{ |
|
79
|
3 |
|
if ($event['request']->getMethod() != 'HEAD') { |
|
80
|
3 |
|
$this->signRequest($event['request']); |
|
81
|
3 |
|
} |
|
82
|
3 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \Guzzle\Http\Message\Request $request |
|
86
|
|
|
*/ |
|
87
|
9 |
|
public function signRequest(Request $request) |
|
88
|
|
|
{ |
|
89
|
9 |
|
$url = $request->getPath(); |
|
90
|
9 |
|
if ('POST' == $request->getMethod() && $request instanceof EntityEnclosingRequest) { |
|
91
|
3 |
|
$body = (string) $request->getBody(); |
|
92
|
3 |
|
$hash = $this->signature->generate($body); |
|
93
|
3 |
|
} else { |
|
94
|
6 |
|
$url .= '?' . $request->getQuery(); |
|
95
|
6 |
|
$hash = $this->signature->generate($url); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
9 |
|
$request->addCookie('acquia_solr_time', $this->signature->getRequestTime()); |
|
99
|
9 |
|
$request->addCookie('acquia_solr_nonce', $this->signature->getNonce()); |
|
100
|
9 |
|
$request->addCookie('acquia_solr_hmac', $hash . ';'); |
|
101
|
|
|
|
|
102
|
|
|
// The timestamp should be current for each request. |
|
103
|
9 |
|
$this->signature->unsetRequestTime(); |
|
104
|
9 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|