|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter\Event\Subscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Event\BasicAuth\BasicAuthInterface; |
|
15
|
|
|
use Ivory\HttpAdapter\Event\Events; |
|
16
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
|
17
|
|
|
use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author GeLo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class BasicAuthSubscriber implements EventSubscriberInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var BasicAuthInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $basicAuth; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param BasicAuthInterface $basicAuth |
|
32
|
|
|
*/ |
|
33
|
36 |
|
public function __construct(BasicAuthInterface $basicAuth) |
|
34
|
|
|
{ |
|
35
|
36 |
|
$this->basicAuth = $basicAuth; |
|
36
|
36 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return BasicAuthInterface |
|
40
|
|
|
*/ |
|
41
|
9 |
|
public function getBasicAuth() |
|
42
|
|
|
{ |
|
43
|
9 |
|
return $this->basicAuth; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param RequestCreatedEvent $event |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function onRequestCreated(RequestCreatedEvent $event) |
|
50
|
|
|
{ |
|
51
|
9 |
|
$event->setRequest($this->basicAuth->authenticate($event->getRequest())); |
|
52
|
9 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param MultiRequestCreatedEvent $event |
|
56
|
|
|
*/ |
|
57
|
9 |
|
public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
58
|
|
|
{ |
|
59
|
9 |
|
foreach ($event->getRequests() as $request) { |
|
60
|
9 |
|
$event->removeRequest($request); |
|
61
|
9 |
|
$event->addRequest($this->basicAuth->authenticate($request)); |
|
62
|
7 |
|
} |
|
63
|
9 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
9 |
|
public static function getSubscribedEvents() |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
9 |
|
Events::REQUEST_CREATED => ['onRequestCreated', 300], |
|
72
|
8 |
|
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 300], |
|
73
|
7 |
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|