Repository navigation
Expand file tree
/
Copy pathAuthListener.php
More file actions
118 lines (100 loc) · 3.32 KB
/
Copy pathAuthListener.php
File metadata and controls
118 lines (100 loc) · 3.32 KB
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
<?php
namespace Github\HttpClient\Listener;
use Github\Client;
use Github\Exception\InvalidArgumentException;
use Buzz\Listener\ListenerInterface;
use Buzz\Message\MessageInterface;
use Buzz\Message\RequestInterface;
use Buzz\Util\Url;
/**
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class AuthListener implements ListenerInterface
{
/**
* @var string
*/
private $method;
/**
* @var array
*/
private $options;
/**
* @param string $method
* @param array $options
*/
public function __construct($method, array $options)
{
$this->method = $method;
$this->options = $options;
}
/**
* {@inheritDoc}
*
* @throws InvalidArgumentException
*/
public function preSend(RequestInterface $request)
{
// Skip by default
if (null === $this->method) {
return;
}
switch ($this->method) {
case Client::AUTH_HTTP_PASSWORD:
if (!isset($this->options['tokenOrLogin'], $this->options['password'])) {
throw new InvalidArgumentException('You need to set username with password!');
}
$request->addHeader('Authorization: Basic '. base64_encode($this->options['tokenOrLogin'] .':'. $this->options['password']));
break;
case Client::AUTH_HTTP_TOKEN:
if (!isset($this->options['tokenOrLogin'])) {
throw new InvalidArgumentException('You need to set OAuth token!');
}
$request->addHeader('Authorization: token '. $this->options['tokenOrLogin']);
break;
case Client::AUTH_URL_CLIENT_ID:
if (!isset($this->options['tokenOrLogin'], $this->options['password'])) {
throw new InvalidArgumentException('You need to set client_id and client_secret!');
}
$this->setRequestUrl(
$request,
array(
'client_id' => $this->options['tokenOrLogin'],
'client_secret' => $this->options['password'],
)
);
break;
case Client::AUTH_URL_TOKEN:
if (!isset($this->options['tokenOrLogin'])) {
throw new InvalidArgumentException('You need to set OAuth token!');
}
$this->setRequestUrl(
$request,
array(
'access_token' => $this->options['tokenOrLogin'],
)
);
break;
default:
throw new InvalidArgumentException(sprintf('Unknown method called "%s".', $this->method));
}
}
/**
* {@inheritDoc}
*/
public function postSend(RequestInterface $request, MessageInterface $response)
{
}
/**
* @param RequestInterface $request
* @param array $parameters
*
* @return Url
*/
private function setRequestUrl(RequestInterface $request, array $parameters = array())
{
$url = $request->getUrl();
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($parameters, '', '&'));
$request->fromUrl(new Url($url));
}
}