@@ -12,97 +12,97 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class Message extends Github\Sanity |
14 | 14 | { |
15 | - /** @var array[name => value] */ |
|
16 | - private $headers = []; |
|
17 | - |
|
18 | - /** @var string|NULL */ |
|
19 | - private $content; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array |
|
24 | - * @param string|NULL |
|
25 | - */ |
|
26 | - public function __construct(array $headers = [], $content = NULL) |
|
27 | - { |
|
28 | - $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | - $this->content = $content; |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @param string |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function hasHeader($name) |
|
38 | - { |
|
39 | - return array_key_exists(strtolower($name), $this->headers); |
|
40 | - } |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string |
|
45 | - * @param mixed |
|
46 | - * @return mixed |
|
47 | - */ |
|
48 | - public function getHeader($name, $default = NULL) |
|
49 | - { |
|
50 | - $name = strtolower($name); |
|
51 | - return array_key_exists($name, $this->headers) |
|
52 | - ? $this->headers[$name] |
|
53 | - : $default; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string |
|
59 | - * @param string |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - protected function addHeader($name, $value) |
|
63 | - { |
|
64 | - $name = strtolower($name); |
|
65 | - if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | - $this->headers[$name] = $value; |
|
67 | - } |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @param string |
|
75 | - * @param string|NULL |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - protected function setHeader($name, $value) |
|
79 | - { |
|
80 | - $name = strtolower($name); |
|
81 | - if ($value === NULL) { |
|
82 | - unset($this->headers[$name]); |
|
83 | - } else { |
|
84 | - $this->headers[$name] = $value; |
|
85 | - } |
|
86 | - |
|
87 | - return $this; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function getHeaders() |
|
95 | - { |
|
96 | - return $this->headers; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string|NULL |
|
102 | - */ |
|
103 | - public function getContent() |
|
104 | - { |
|
105 | - return $this->content; |
|
106 | - } |
|
15 | + /** @var array[name => value] */ |
|
16 | + private $headers = []; |
|
17 | + |
|
18 | + /** @var string|NULL */ |
|
19 | + private $content; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array |
|
24 | + * @param string|NULL |
|
25 | + */ |
|
26 | + public function __construct(array $headers = [], $content = NULL) |
|
27 | + { |
|
28 | + $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | + $this->content = $content; |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @param string |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function hasHeader($name) |
|
38 | + { |
|
39 | + return array_key_exists(strtolower($name), $this->headers); |
|
40 | + } |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string |
|
45 | + * @param mixed |
|
46 | + * @return mixed |
|
47 | + */ |
|
48 | + public function getHeader($name, $default = NULL) |
|
49 | + { |
|
50 | + $name = strtolower($name); |
|
51 | + return array_key_exists($name, $this->headers) |
|
52 | + ? $this->headers[$name] |
|
53 | + : $default; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string |
|
59 | + * @param string |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + protected function addHeader($name, $value) |
|
63 | + { |
|
64 | + $name = strtolower($name); |
|
65 | + if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | + $this->headers[$name] = $value; |
|
67 | + } |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @param string |
|
75 | + * @param string|NULL |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + protected function setHeader($name, $value) |
|
79 | + { |
|
80 | + $name = strtolower($name); |
|
81 | + if ($value === NULL) { |
|
82 | + unset($this->headers[$name]); |
|
83 | + } else { |
|
84 | + $this->headers[$name] = $value; |
|
85 | + } |
|
86 | + |
|
87 | + return $this; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function getHeaders() |
|
95 | + { |
|
96 | + return $this->headers; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string|NULL |
|
102 | + */ |
|
103 | + public function getContent() |
|
104 | + { |
|
105 | + return $this->content; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,110 +12,110 @@ |
||
12 | 12 | */ |
13 | 13 | class CurlClient extends AbstractClient |
14 | 14 | { |
15 | - /** @var array|NULL */ |
|
16 | - private $options; |
|
17 | - |
|
18 | - /** @var resource */ |
|
19 | - private $curl; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | - * |
|
25 | - * @throws Github\LogicException |
|
26 | - */ |
|
27 | - public function __construct(array $options = NULL) |
|
28 | - { |
|
29 | - if (!extension_loaded('curl')) { |
|
30 | - throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | - } |
|
32 | - |
|
33 | - $this->options = $options; |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - protected function setupRequest(Request $request) |
|
38 | - { |
|
39 | - parent::setupRequest($request); |
|
40 | - $request->addHeader('Connection', 'keep-alive'); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * @return Response |
|
46 | - * |
|
47 | - * @throws BadResponseException |
|
48 | - */ |
|
49 | - protected function process(Request $request) |
|
50 | - { |
|
51 | - $headers = []; |
|
52 | - foreach ($request->getHeaders() as $name => $value) { |
|
53 | - $headers[] = "$name: $value"; |
|
54 | - } |
|
55 | - |
|
56 | - $responseHeaders = []; |
|
57 | - $softOptions = [ |
|
58 | - CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | - CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | - CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | - CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | - ]; |
|
63 | - |
|
64 | - $hardOptions = [ |
|
65 | - CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | - CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | - CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | - CURLOPT_URL => $request->getUrl(), |
|
70 | - CURLOPT_HTTPHEADER => $headers, |
|
71 | - CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | - CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | - CURLOPT_HEADER => FALSE, |
|
74 | - CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | - if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | - /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | - # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | - $responseHeaders = []; |
|
79 | - |
|
80 | - } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | - $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | - |
|
83 | - } elseif ($line !== "\r\n") { |
|
84 | - list($name, $value) = explode(':', $line, 2); |
|
85 | - $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | - } |
|
87 | - |
|
88 | - return strlen($line); |
|
89 | - }, |
|
90 | - ]; |
|
91 | - |
|
92 | - if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | - $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | - } |
|
95 | - |
|
96 | - if (!$this->curl) { |
|
97 | - $this->curl = curl_init(); |
|
98 | - if ($this->curl === FALSE) { |
|
99 | - throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | - if ($result === FALSE) { |
|
105 | - throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | - } |
|
107 | - |
|
108 | - $content = curl_exec($this->curl); |
|
109 | - if ($content === FALSE) { |
|
110 | - throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | - } |
|
112 | - |
|
113 | - $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | - if ($code === FALSE) { |
|
115 | - throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | - } |
|
117 | - |
|
118 | - return new Response($code, $responseHeaders, $content); |
|
119 | - } |
|
15 | + /** @var array|NULL */ |
|
16 | + private $options; |
|
17 | + |
|
18 | + /** @var resource */ |
|
19 | + private $curl; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | + * |
|
25 | + * @throws Github\LogicException |
|
26 | + */ |
|
27 | + public function __construct(array $options = NULL) |
|
28 | + { |
|
29 | + if (!extension_loaded('curl')) { |
|
30 | + throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | + } |
|
32 | + |
|
33 | + $this->options = $options; |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + protected function setupRequest(Request $request) |
|
38 | + { |
|
39 | + parent::setupRequest($request); |
|
40 | + $request->addHeader('Connection', 'keep-alive'); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * @return Response |
|
46 | + * |
|
47 | + * @throws BadResponseException |
|
48 | + */ |
|
49 | + protected function process(Request $request) |
|
50 | + { |
|
51 | + $headers = []; |
|
52 | + foreach ($request->getHeaders() as $name => $value) { |
|
53 | + $headers[] = "$name: $value"; |
|
54 | + } |
|
55 | + |
|
56 | + $responseHeaders = []; |
|
57 | + $softOptions = [ |
|
58 | + CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | + CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | + CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | + CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | + ]; |
|
63 | + |
|
64 | + $hardOptions = [ |
|
65 | + CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | + CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | + CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | + CURLOPT_URL => $request->getUrl(), |
|
70 | + CURLOPT_HTTPHEADER => $headers, |
|
71 | + CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | + CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | + CURLOPT_HEADER => FALSE, |
|
74 | + CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | + if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | + /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | + # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | + $responseHeaders = []; |
|
79 | + |
|
80 | + } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | + $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | + |
|
83 | + } elseif ($line !== "\r\n") { |
|
84 | + list($name, $value) = explode(':', $line, 2); |
|
85 | + $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | + } |
|
87 | + |
|
88 | + return strlen($line); |
|
89 | + }, |
|
90 | + ]; |
|
91 | + |
|
92 | + if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | + $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | + } |
|
95 | + |
|
96 | + if (!$this->curl) { |
|
97 | + $this->curl = curl_init(); |
|
98 | + if ($this->curl === FALSE) { |
|
99 | + throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | + if ($result === FALSE) { |
|
105 | + throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | + } |
|
107 | + |
|
108 | + $content = curl_exec($this->curl); |
|
109 | + if ($content === FALSE) { |
|
110 | + throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | + } |
|
112 | + |
|
113 | + $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | + if ($code === FALSE) { |
|
115 | + throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | + } |
|
117 | + |
|
118 | + return new Response($code, $responseHeaders, $content); |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | CURLOPT_CONNECTTIMEOUT => 10, |
59 | 59 | CURLOPT_SSL_VERIFYHOST => 2, |
60 | 60 | CURLOPT_SSL_VERIFYPEER => 1, |
61 | - CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
61 | + CURLOPT_CAINFO => realpath(__DIR__.'/../ca-chain.crt'), |
|
62 | 62 | ]; |
63 | 63 | |
64 | 64 | $hardOptions = [ |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | $responseHeaders = []; |
79 | 79 | |
80 | 80 | } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
81 | - $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
81 | + $responseHeaders[$last] .= ' '.trim($line); # RFC2616, 2.2 |
|
82 | 82 | |
83 | 83 | } elseif ($line !== "\r\n") { |
84 | 84 | list($name, $value) = explode(':', $line, 2); |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | |
103 | 103 | $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
104 | 104 | if ($result === FALSE) { |
105 | - throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
105 | + throw new BadResponseException('Setting cURL options failed: '.curl_error($this->curl), curl_errno($this->curl)); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | $content = curl_exec($this->curl); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | |
113 | 113 | $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
114 | 114 | if ($code === FALSE) { |
115 | - throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
115 | + throw new BadResponseException('HTTP status code is missing:'.curl_error($this->curl), curl_errno($this->curl)); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | return new Response($code, $responseHeaders, $content); |
@@ -12,80 +12,80 @@ |
||
12 | 12 | */ |
13 | 13 | class Response extends Message |
14 | 14 | { |
15 | - /** HTTP 1.1 code */ |
|
16 | - const |
|
17 | - S200_OK = 200, |
|
18 | - S301_MOVED_PERMANENTLY = 301, |
|
19 | - S302_FOUND = 302, |
|
20 | - S304_NOT_MODIFIED = 304, |
|
21 | - S307_TEMPORARY_REDIRECT = 307, |
|
22 | - S400_BAD_REQUEST = 400, |
|
23 | - S401_UNAUTHORIZED = 401, |
|
24 | - S403_FORBIDDEN = 403, |
|
25 | - S404_NOT_FOUND = 404, |
|
26 | - S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | - |
|
28 | - /** @var int */ |
|
29 | - private $code; |
|
30 | - |
|
31 | - /** @var Response */ |
|
32 | - private $previous; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * @param int |
|
37 | - * @param array |
|
38 | - * @param string |
|
39 | - */ |
|
40 | - public function __construct($code, array $headers, $content) |
|
41 | - { |
|
42 | - $this->code = (int) $code; |
|
43 | - parent::__construct($headers, $content); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * HTTP code. |
|
49 | - * @return int |
|
50 | - */ |
|
51 | - public function getCode() |
|
52 | - { |
|
53 | - return $this->code; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param int |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function isCode($code) |
|
62 | - { |
|
63 | - return $this->code === (int) $code; |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @return Response|NULL |
|
69 | - */ |
|
70 | - public function getPrevious() |
|
71 | - { |
|
72 | - return $this->previous; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * @return self |
|
78 | - * |
|
79 | - * @throws Github\LogicException |
|
80 | - */ |
|
81 | - public function setPrevious(Response $previous = NULL) |
|
82 | - { |
|
83 | - if ($this->previous) { |
|
84 | - throw new Github\LogicException('Previous response is already set.'); |
|
85 | - } |
|
86 | - $this->previous = $previous; |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
15 | + /** HTTP 1.1 code */ |
|
16 | + const |
|
17 | + S200_OK = 200, |
|
18 | + S301_MOVED_PERMANENTLY = 301, |
|
19 | + S302_FOUND = 302, |
|
20 | + S304_NOT_MODIFIED = 304, |
|
21 | + S307_TEMPORARY_REDIRECT = 307, |
|
22 | + S400_BAD_REQUEST = 400, |
|
23 | + S401_UNAUTHORIZED = 401, |
|
24 | + S403_FORBIDDEN = 403, |
|
25 | + S404_NOT_FOUND = 404, |
|
26 | + S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | + |
|
28 | + /** @var int */ |
|
29 | + private $code; |
|
30 | + |
|
31 | + /** @var Response */ |
|
32 | + private $previous; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * @param int |
|
37 | + * @param array |
|
38 | + * @param string |
|
39 | + */ |
|
40 | + public function __construct($code, array $headers, $content) |
|
41 | + { |
|
42 | + $this->code = (int) $code; |
|
43 | + parent::__construct($headers, $content); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * HTTP code. |
|
49 | + * @return int |
|
50 | + */ |
|
51 | + public function getCode() |
|
52 | + { |
|
53 | + return $this->code; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param int |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function isCode($code) |
|
62 | + { |
|
63 | + return $this->code === (int) $code; |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @return Response|NULL |
|
69 | + */ |
|
70 | + public function getPrevious() |
|
71 | + { |
|
72 | + return $this->previous; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * @return self |
|
78 | + * |
|
79 | + * @throws Github\LogicException |
|
80 | + */ |
|
81 | + public function setPrevious(Response $previous = NULL) |
|
82 | + { |
|
83 | + if ($this->previous) { |
|
84 | + throw new Github\LogicException('Previous response is already set.'); |
|
85 | + } |
|
86 | + $this->previous = $previous; |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | 90 | |
91 | 91 | } |
@@ -12,98 +12,98 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class AbstractClient extends Github\Sanity implements IClient |
14 | 14 | { |
15 | - /** @var int[] will follow Location header on these response codes */ |
|
16 | - public $redirectCodes = [ |
|
17 | - Response::S301_MOVED_PERMANENTLY, |
|
18 | - Response::S302_FOUND, |
|
19 | - Response::S307_TEMPORARY_REDIRECT, |
|
20 | - ]; |
|
21 | - |
|
22 | - /** @var int maximum redirects per request*/ |
|
23 | - public $maxRedirects = 5; |
|
24 | - |
|
25 | - /** @var callable|NULL */ |
|
26 | - private $onRequest; |
|
27 | - |
|
28 | - /** @var callable|NULL */ |
|
29 | - private $onResponse; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @see https://developer.github.com/v3/#http-redirects |
|
34 | - * |
|
35 | - * @return Response |
|
36 | - * |
|
37 | - * @throws BadResponseException |
|
38 | - */ |
|
39 | - public function request(Request $request) |
|
40 | - { |
|
41 | - $request = clone $request; |
|
42 | - |
|
43 | - $counter = $this->maxRedirects; |
|
44 | - $previous = NULL; |
|
45 | - do { |
|
46 | - $this->setupRequest($request); |
|
47 | - |
|
48 | - $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | - $response = $this->process($request); |
|
50 | - $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | - |
|
52 | - $previous = $response->setPrevious($previous); |
|
53 | - |
|
54 | - if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | - /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | - $request = new Request( |
|
57 | - $request->getMethod(), |
|
58 | - $response->getHeader('Location'), |
|
59 | - $request->getHeaders(), |
|
60 | - $request->getContent() |
|
61 | - ); |
|
62 | - |
|
63 | - $counter--; |
|
64 | - continue; |
|
65 | - } |
|
66 | - break; |
|
67 | - |
|
68 | - } while (TRUE); |
|
69 | - |
|
70 | - return $response; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param callable|NULL function(Request $request) |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function onRequest($callback) |
|
79 | - { |
|
80 | - $this->onRequest = $callback; |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @param callable|NULL function(Response $response) |
|
87 | - * @return self |
|
88 | - */ |
|
89 | - public function onResponse($callback) |
|
90 | - { |
|
91 | - $this->onResponse = $callback; |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - protected function setupRequest(Request $request) |
|
97 | - { |
|
98 | - $request->addHeader('Expect', ''); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @return Response |
|
104 | - * |
|
105 | - * @throws BadResponseException |
|
106 | - */ |
|
107 | - abstract protected function process(Request $request); |
|
15 | + /** @var int[] will follow Location header on these response codes */ |
|
16 | + public $redirectCodes = [ |
|
17 | + Response::S301_MOVED_PERMANENTLY, |
|
18 | + Response::S302_FOUND, |
|
19 | + Response::S307_TEMPORARY_REDIRECT, |
|
20 | + ]; |
|
21 | + |
|
22 | + /** @var int maximum redirects per request*/ |
|
23 | + public $maxRedirects = 5; |
|
24 | + |
|
25 | + /** @var callable|NULL */ |
|
26 | + private $onRequest; |
|
27 | + |
|
28 | + /** @var callable|NULL */ |
|
29 | + private $onResponse; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @see https://developer.github.com/v3/#http-redirects |
|
34 | + * |
|
35 | + * @return Response |
|
36 | + * |
|
37 | + * @throws BadResponseException |
|
38 | + */ |
|
39 | + public function request(Request $request) |
|
40 | + { |
|
41 | + $request = clone $request; |
|
42 | + |
|
43 | + $counter = $this->maxRedirects; |
|
44 | + $previous = NULL; |
|
45 | + do { |
|
46 | + $this->setupRequest($request); |
|
47 | + |
|
48 | + $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | + $response = $this->process($request); |
|
50 | + $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | + |
|
52 | + $previous = $response->setPrevious($previous); |
|
53 | + |
|
54 | + if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | + /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | + $request = new Request( |
|
57 | + $request->getMethod(), |
|
58 | + $response->getHeader('Location'), |
|
59 | + $request->getHeaders(), |
|
60 | + $request->getContent() |
|
61 | + ); |
|
62 | + |
|
63 | + $counter--; |
|
64 | + continue; |
|
65 | + } |
|
66 | + break; |
|
67 | + |
|
68 | + } while (TRUE); |
|
69 | + |
|
70 | + return $response; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param callable|NULL function(Request $request) |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function onRequest($callback) |
|
79 | + { |
|
80 | + $this->onRequest = $callback; |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @param callable|NULL function(Response $response) |
|
87 | + * @return self |
|
88 | + */ |
|
89 | + public function onResponse($callback) |
|
90 | + { |
|
91 | + $this->onResponse = $callback; |
|
92 | + return $this; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + protected function setupRequest(Request $request) |
|
97 | + { |
|
98 | + $request->addHeader('Expect', ''); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @return Response |
|
104 | + * |
|
105 | + * @throws BadResponseException |
|
106 | + */ |
|
107 | + abstract protected function process(Request $request); |
|
108 | 108 | |
109 | 109 | } |
@@ -10,21 +10,21 @@ |
||
10 | 10 | */ |
11 | 11 | interface IClient |
12 | 12 | { |
13 | - /** |
|
14 | - * @return Response |
|
15 | - */ |
|
16 | - function request(Request $request); |
|
13 | + /** |
|
14 | + * @return Response |
|
15 | + */ |
|
16 | + function request(Request $request); |
|
17 | 17 | |
18 | - /** |
|
19 | - * @param callable|NULL |
|
20 | - * @return self |
|
21 | - */ |
|
22 | - function onRequest($callback); |
|
18 | + /** |
|
19 | + * @param callable|NULL |
|
20 | + * @return self |
|
21 | + */ |
|
22 | + function onRequest($callback); |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param callable|NULL |
|
26 | - * @return self |
|
27 | - */ |
|
28 | - function onResponse($callback); |
|
24 | + /** |
|
25 | + * @param callable|NULL |
|
26 | + * @return self |
|
27 | + */ |
|
28 | + function onResponse($callback); |
|
29 | 29 | |
30 | 30 | } |
@@ -12,97 +12,97 @@ |
||
12 | 12 | */ |
13 | 13 | class Token extends Github\Sanity |
14 | 14 | { |
15 | - /** @var string */ |
|
16 | - private $value; |
|
17 | - |
|
18 | - /** @var string */ |
|
19 | - private $type; |
|
20 | - |
|
21 | - /** @var string[] */ |
|
22 | - private $scopes; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string |
|
27 | - * @param string |
|
28 | - * @param string[] |
|
29 | - */ |
|
30 | - public function __construct($value, $type = '', array $scopes = []) |
|
31 | - { |
|
32 | - $this->value = $value; |
|
33 | - $this->type = $type; |
|
34 | - $this->scopes = $scopes; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getValue() |
|
42 | - { |
|
43 | - return $this->value; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getType() |
|
51 | - { |
|
52 | - return $this->type; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string[] |
|
58 | - */ |
|
59 | - public function getScopes() |
|
60 | - { |
|
61 | - return $this->scopes; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @see https://developer.github.com/v3/oauth/#scopes |
|
67 | - * |
|
68 | - * @param string |
|
69 | - * @return bool |
|
70 | - */ |
|
71 | - public function hasScope($scope) |
|
72 | - { |
|
73 | - if (in_array($scope, $this->scopes, TRUE)) { |
|
74 | - return TRUE; |
|
75 | - } |
|
76 | - |
|
77 | - static $superiors = [ |
|
78 | - 'user:email' => 'user', |
|
79 | - 'user:follow' => 'user', |
|
80 | - 'notifications' => 'repo', |
|
81 | - ]; |
|
82 | - |
|
83 | - if (array_key_exists($scope, $superiors) && in_array($superiors[$scope], $this->scopes, TRUE)) { |
|
84 | - return TRUE; |
|
85 | - } |
|
86 | - |
|
87 | - return FALSE; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** @internal */ |
|
92 | - public function toArray() |
|
93 | - { |
|
94 | - return [ |
|
95 | - 'value' => $this->value, |
|
96 | - 'type' => $this->type, |
|
97 | - 'scopes' => $this->scopes, |
|
98 | - ]; |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** @internal */ |
|
103 | - public static function createFromArray(array $data) |
|
104 | - { |
|
105 | - return new static($data['value'], $data['type'], $data['scopes']); |
|
106 | - } |
|
15 | + /** @var string */ |
|
16 | + private $value; |
|
17 | + |
|
18 | + /** @var string */ |
|
19 | + private $type; |
|
20 | + |
|
21 | + /** @var string[] */ |
|
22 | + private $scopes; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string |
|
27 | + * @param string |
|
28 | + * @param string[] |
|
29 | + */ |
|
30 | + public function __construct($value, $type = '', array $scopes = []) |
|
31 | + { |
|
32 | + $this->value = $value; |
|
33 | + $this->type = $type; |
|
34 | + $this->scopes = $scopes; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getValue() |
|
42 | + { |
|
43 | + return $this->value; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getType() |
|
51 | + { |
|
52 | + return $this->type; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string[] |
|
58 | + */ |
|
59 | + public function getScopes() |
|
60 | + { |
|
61 | + return $this->scopes; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @see https://developer.github.com/v3/oauth/#scopes |
|
67 | + * |
|
68 | + * @param string |
|
69 | + * @return bool |
|
70 | + */ |
|
71 | + public function hasScope($scope) |
|
72 | + { |
|
73 | + if (in_array($scope, $this->scopes, TRUE)) { |
|
74 | + return TRUE; |
|
75 | + } |
|
76 | + |
|
77 | + static $superiors = [ |
|
78 | + 'user:email' => 'user', |
|
79 | + 'user:follow' => 'user', |
|
80 | + 'notifications' => 'repo', |
|
81 | + ]; |
|
82 | + |
|
83 | + if (array_key_exists($scope, $superiors) && in_array($superiors[$scope], $this->scopes, TRUE)) { |
|
84 | + return TRUE; |
|
85 | + } |
|
86 | + |
|
87 | + return FALSE; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** @internal */ |
|
92 | + public function toArray() |
|
93 | + { |
|
94 | + return [ |
|
95 | + 'value' => $this->value, |
|
96 | + 'type' => $this->type, |
|
97 | + 'scopes' => $this->scopes, |
|
98 | + ]; |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** @internal */ |
|
103 | + public static function createFromArray(array $data) |
|
104 | + { |
|
105 | + return new static($data['value'], $data['type'], $data['scopes']); |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,35 +12,35 @@ |
||
12 | 12 | */ |
13 | 13 | class Configuration extends Github\Sanity |
14 | 14 | { |
15 | - /** @var string */ |
|
16 | - public $clientId; |
|
17 | - |
|
18 | - /** @var string */ |
|
19 | - public $clientSecret; |
|
20 | - |
|
21 | - /** @var string[] */ |
|
22 | - public $scopes; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string |
|
27 | - * @param string |
|
28 | - * @param string[] |
|
29 | - */ |
|
30 | - public function __construct($clientId, $clientSecret, array $scopes = []) |
|
31 | - { |
|
32 | - $this->clientId = $clientId; |
|
33 | - $this->clientSecret = $clientSecret; |
|
34 | - $this->scopes = $scopes; |
|
15 | + /** @var string */ |
|
16 | + public $clientId; |
|
17 | + |
|
18 | + /** @var string */ |
|
19 | + public $clientSecret; |
|
20 | + |
|
21 | + /** @var string[] */ |
|
22 | + public $scopes; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string |
|
27 | + * @param string |
|
28 | + * @param string[] |
|
29 | + */ |
|
30 | + public function __construct($clientId, $clientSecret, array $scopes = []) |
|
31 | + { |
|
32 | + $this->clientId = $clientId; |
|
33 | + $this->clientSecret = $clientSecret; |
|
34 | + $this->scopes = $scopes; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @return Configuration |
|
40 | - */ |
|
41 | - public static function fromArray(array $conf) |
|
42 | - { |
|
43 | - return new static($conf['clientId'], $conf['clientSecret'], isset($conf['scopes']) ? $conf['scopes'] : []); |
|
44 | - } |
|
38 | + /** |
|
39 | + * @return Configuration |
|
40 | + */ |
|
41 | + public static function fromArray(array $conf) |
|
42 | + { |
|
43 | + return new static($conf['clientId'], $conf['clientSecret'], isset($conf['scopes']) ? $conf['scopes'] : []); |
|
44 | + } |
|
45 | 45 | |
46 | 46 | } |
@@ -14,158 +14,158 @@ |
||
14 | 14 | */ |
15 | 15 | class Login extends Github\Sanity |
16 | 16 | { |
17 | - /** @var string */ |
|
18 | - private $authUrl = 'https://github.com/login/oauth/authorize'; |
|
19 | - |
|
20 | - /** @var string */ |
|
21 | - private $tokenUrl = 'https://github.com/login/oauth/access_token'; |
|
22 | - |
|
23 | - /** @var Configuration */ |
|
24 | - private $conf; |
|
25 | - |
|
26 | - /** @var Storages\ISessionStorage */ |
|
27 | - private $storage; |
|
28 | - |
|
29 | - /** @var Http\IClient */ |
|
30 | - private $client; |
|
31 | - |
|
32 | - |
|
33 | - public function __construct(Configuration $conf, Storages\ISessionStorage $storage = NULL, Http\IClient $client = NULL) |
|
34 | - { |
|
35 | - $this->conf = $conf; |
|
36 | - $this->storage = $storage ?: new Storages\SessionStorage; |
|
37 | - $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
38 | - } |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * @return Http\IClient |
|
43 | - */ |
|
44 | - public function getClient() |
|
45 | - { |
|
46 | - return $this->client; |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string URL to redirect back from Github when user approves the permissions request |
|
52 | - * @param callable function($githubUrl) makes HTTP redirect to Github |
|
53 | - */ |
|
54 | - public function askPermissions($backUrl, $redirectCb = NULL) |
|
55 | - { |
|
56 | - /** @todo Something more safe? */ |
|
57 | - $state = sha1(uniqid(microtime(TRUE), TRUE)); |
|
58 | - $params = [ |
|
59 | - 'client_id' => $this->conf->clientId, |
|
60 | - 'redirect_uri' => $backUrl, |
|
61 | - 'scope' => implode(',', $this->conf->scopes), |
|
62 | - 'state' => $state, |
|
63 | - ]; |
|
64 | - |
|
65 | - $this->storage->set('auth.state', $state); |
|
66 | - |
|
67 | - $url = $this->authUrl . '?' . http_build_query($params); |
|
68 | - if ($redirectCb === NULL) { |
|
69 | - header("Location: $url"); |
|
70 | - die(); |
|
71 | - } else { |
|
72 | - call_user_func($redirectCb, $url); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @param string |
|
79 | - * @param string |
|
80 | - * @return Token |
|
81 | - * |
|
82 | - * @throws LoginException |
|
83 | - */ |
|
84 | - public function obtainToken($code, $state) |
|
85 | - { |
|
86 | - if ($state !== $this->storage->get('auth.state')) { |
|
87 | - throw new LoginException('OAuth security state does not match.'); |
|
88 | - } |
|
89 | - |
|
90 | - $params = [ |
|
91 | - 'client_id' => $this->conf->clientId, |
|
92 | - 'client_secret' => $this->conf->clientSecret, |
|
93 | - 'code' => $code, |
|
94 | - ]; |
|
95 | - |
|
96 | - $headers = [ |
|
97 | - 'Accept' => 'application/json', |
|
98 | - 'Content-Type' => 'application/x-www-form-urlencoded', |
|
99 | - ]; |
|
100 | - |
|
101 | - $request = new Http\Request(Http\Request::POST, $this->tokenUrl, $headers, http_build_query($params)); |
|
102 | - try { |
|
103 | - $response = $this->client->request($request); |
|
104 | - } catch (Http\BadResponseException $e) { |
|
105 | - throw new LoginException('HTTP request failed.', 0, $e); |
|
106 | - } |
|
107 | - |
|
108 | - try { |
|
109 | - /** @var $json \stdClass */ |
|
110 | - if ($response->isCode(Http\Response::S404_NOT_FOUND)) { |
|
111 | - $json = Github\Helpers::jsonDecode($response->getContent()); |
|
112 | - throw new LoginException($json->error, $response->getCode()); |
|
113 | - |
|
114 | - } elseif (!$response->isCode(Http\Response::S200_OK)) { |
|
115 | - throw new LoginException('Unexpected response.', $response->getCode()); |
|
116 | - } |
|
117 | - |
|
118 | - $json = Github\Helpers::jsonDecode($response->getContent()); |
|
119 | - |
|
120 | - } catch (Github\JsonException $e) { |
|
121 | - throw new LoginException('Bad JSON in response.', 0, $e); |
|
122 | - } |
|
123 | - |
|
124 | - $token = new Token($json->access_token, $json->token_type, strlen($json->scope) ? explode(',', $json->scope) : []); |
|
125 | - $this->storage->set('auth.token', $token->toArray()); |
|
126 | - $this->storage->remove('auth.state'); |
|
127 | - |
|
128 | - return $token; |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * @return bool |
|
134 | - */ |
|
135 | - public function hasToken() |
|
136 | - { |
|
137 | - return $this->storage->get('auth.token') !== NULL; |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * @return Token |
|
143 | - * |
|
144 | - * @throws Github\LogicException when token has not been obtained yet |
|
145 | - */ |
|
146 | - public function getToken() |
|
147 | - { |
|
148 | - $token = $this->storage->get('auth.token'); |
|
149 | - if ($token === NULL) { |
|
150 | - throw new Github\LogicException('Token has not been obtained yet.'); |
|
151 | - |
|
152 | - } elseif ($token instanceof Token) { |
|
153 | - /** @deprecated */ |
|
154 | - $token = $token->toArray(); |
|
155 | - $this->storage->set('auth.token', $token); |
|
156 | - } |
|
157 | - |
|
158 | - return Token::createFromArray($token); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * @return self |
|
164 | - */ |
|
165 | - public function dropToken() |
|
166 | - { |
|
167 | - $this->storage->remove('auth.token'); |
|
168 | - return $this; |
|
169 | - } |
|
17 | + /** @var string */ |
|
18 | + private $authUrl = 'https://github.com/login/oauth/authorize'; |
|
19 | + |
|
20 | + /** @var string */ |
|
21 | + private $tokenUrl = 'https://github.com/login/oauth/access_token'; |
|
22 | + |
|
23 | + /** @var Configuration */ |
|
24 | + private $conf; |
|
25 | + |
|
26 | + /** @var Storages\ISessionStorage */ |
|
27 | + private $storage; |
|
28 | + |
|
29 | + /** @var Http\IClient */ |
|
30 | + private $client; |
|
31 | + |
|
32 | + |
|
33 | + public function __construct(Configuration $conf, Storages\ISessionStorage $storage = NULL, Http\IClient $client = NULL) |
|
34 | + { |
|
35 | + $this->conf = $conf; |
|
36 | + $this->storage = $storage ?: new Storages\SessionStorage; |
|
37 | + $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
38 | + } |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * @return Http\IClient |
|
43 | + */ |
|
44 | + public function getClient() |
|
45 | + { |
|
46 | + return $this->client; |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string URL to redirect back from Github when user approves the permissions request |
|
52 | + * @param callable function($githubUrl) makes HTTP redirect to Github |
|
53 | + */ |
|
54 | + public function askPermissions($backUrl, $redirectCb = NULL) |
|
55 | + { |
|
56 | + /** @todo Something more safe? */ |
|
57 | + $state = sha1(uniqid(microtime(TRUE), TRUE)); |
|
58 | + $params = [ |
|
59 | + 'client_id' => $this->conf->clientId, |
|
60 | + 'redirect_uri' => $backUrl, |
|
61 | + 'scope' => implode(',', $this->conf->scopes), |
|
62 | + 'state' => $state, |
|
63 | + ]; |
|
64 | + |
|
65 | + $this->storage->set('auth.state', $state); |
|
66 | + |
|
67 | + $url = $this->authUrl . '?' . http_build_query($params); |
|
68 | + if ($redirectCb === NULL) { |
|
69 | + header("Location: $url"); |
|
70 | + die(); |
|
71 | + } else { |
|
72 | + call_user_func($redirectCb, $url); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @param string |
|
79 | + * @param string |
|
80 | + * @return Token |
|
81 | + * |
|
82 | + * @throws LoginException |
|
83 | + */ |
|
84 | + public function obtainToken($code, $state) |
|
85 | + { |
|
86 | + if ($state !== $this->storage->get('auth.state')) { |
|
87 | + throw new LoginException('OAuth security state does not match.'); |
|
88 | + } |
|
89 | + |
|
90 | + $params = [ |
|
91 | + 'client_id' => $this->conf->clientId, |
|
92 | + 'client_secret' => $this->conf->clientSecret, |
|
93 | + 'code' => $code, |
|
94 | + ]; |
|
95 | + |
|
96 | + $headers = [ |
|
97 | + 'Accept' => 'application/json', |
|
98 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
|
99 | + ]; |
|
100 | + |
|
101 | + $request = new Http\Request(Http\Request::POST, $this->tokenUrl, $headers, http_build_query($params)); |
|
102 | + try { |
|
103 | + $response = $this->client->request($request); |
|
104 | + } catch (Http\BadResponseException $e) { |
|
105 | + throw new LoginException('HTTP request failed.', 0, $e); |
|
106 | + } |
|
107 | + |
|
108 | + try { |
|
109 | + /** @var $json \stdClass */ |
|
110 | + if ($response->isCode(Http\Response::S404_NOT_FOUND)) { |
|
111 | + $json = Github\Helpers::jsonDecode($response->getContent()); |
|
112 | + throw new LoginException($json->error, $response->getCode()); |
|
113 | + |
|
114 | + } elseif (!$response->isCode(Http\Response::S200_OK)) { |
|
115 | + throw new LoginException('Unexpected response.', $response->getCode()); |
|
116 | + } |
|
117 | + |
|
118 | + $json = Github\Helpers::jsonDecode($response->getContent()); |
|
119 | + |
|
120 | + } catch (Github\JsonException $e) { |
|
121 | + throw new LoginException('Bad JSON in response.', 0, $e); |
|
122 | + } |
|
123 | + |
|
124 | + $token = new Token($json->access_token, $json->token_type, strlen($json->scope) ? explode(',', $json->scope) : []); |
|
125 | + $this->storage->set('auth.token', $token->toArray()); |
|
126 | + $this->storage->remove('auth.state'); |
|
127 | + |
|
128 | + return $token; |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * @return bool |
|
134 | + */ |
|
135 | + public function hasToken() |
|
136 | + { |
|
137 | + return $this->storage->get('auth.token') !== NULL; |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * @return Token |
|
143 | + * |
|
144 | + * @throws Github\LogicException when token has not been obtained yet |
|
145 | + */ |
|
146 | + public function getToken() |
|
147 | + { |
|
148 | + $token = $this->storage->get('auth.token'); |
|
149 | + if ($token === NULL) { |
|
150 | + throw new Github\LogicException('Token has not been obtained yet.'); |
|
151 | + |
|
152 | + } elseif ($token instanceof Token) { |
|
153 | + /** @deprecated */ |
|
154 | + $token = $token->toArray(); |
|
155 | + $this->storage->set('auth.token', $token); |
|
156 | + } |
|
157 | + |
|
158 | + return Token::createFromArray($token); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * @return self |
|
164 | + */ |
|
165 | + public function dropToken() |
|
166 | + { |
|
167 | + $this->storage->remove('auth.token'); |
|
168 | + return $this; |
|
169 | + } |
|
170 | 170 | |
171 | 171 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | |
65 | 65 | $this->storage->set('auth.state', $state); |
66 | 66 | |
67 | - $url = $this->authUrl . '?' . http_build_query($params); |
|
67 | + $url = $this->authUrl.'?'.http_build_query($params); |
|
68 | 68 | if ($redirectCb === NULL) { |
69 | 69 | header("Location: $url"); |
70 | 70 | die(); |
@@ -12,86 +12,86 @@ |
||
12 | 12 | */ |
13 | 13 | class FileCache extends Github\Sanity implements ICache |
14 | 14 | { |
15 | - /** @var string */ |
|
16 | - private $dir; |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * @param string temporary directory |
|
21 | - * |
|
22 | - * @throws MissingDirectoryException |
|
23 | - */ |
|
24 | - public function __construct($tempDir) |
|
25 | - { |
|
26 | - if (!is_dir($tempDir)) { |
|
27 | - throw new MissingDirectoryException("Directory '$tempDir' is missing."); |
|
28 | - } |
|
29 | - |
|
30 | - $dir = $tempDir . DIRECTORY_SEPARATOR . 'milo.github-api'; |
|
31 | - |
|
32 | - if (!is_dir($dir)) { |
|
33 | - set_error_handler(function($severity, $message, $file, $line) use ($dir, & $valid) { |
|
34 | - restore_error_handler(); |
|
35 | - if (!is_dir($dir)) { |
|
36 | - throw new MissingDirectoryException("Cannot create '$dir' directory.", 0, new \ErrorException($message, 0, $severity, $file, $line)); |
|
37 | - } |
|
38 | - }); |
|
39 | - mkdir($dir); |
|
40 | - restore_error_handler(); |
|
41 | - } |
|
42 | - |
|
43 | - $this->dir = $dir; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @param string |
|
49 | - * @param mixed |
|
50 | - * @return mixed stored value |
|
51 | - */ |
|
52 | - public function save($key, $value) |
|
53 | - { |
|
54 | - file_put_contents( |
|
55 | - $this->filePath($key), |
|
56 | - serialize($value), |
|
57 | - LOCK_EX |
|
58 | - ); |
|
59 | - |
|
60 | - return $value; |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * @param string |
|
66 | - * @return mixed|NULL |
|
67 | - */ |
|
68 | - public function load($key) |
|
69 | - { |
|
70 | - $path = $this->filePath($key); |
|
71 | - if (is_file($path) && ($fd = fopen($path, 'rb')) && flock($fd, LOCK_SH)) { |
|
72 | - $cached = stream_get_contents($fd); |
|
73 | - flock($fd, LOCK_UN); |
|
74 | - fclose($fd); |
|
75 | - |
|
76 | - $success = TRUE; |
|
77 | - set_error_handler(function() use (& $success) { return $success = FALSE; }, E_NOTICE); |
|
78 | - $cached = unserialize($cached); |
|
79 | - restore_error_handler(); |
|
80 | - |
|
81 | - if ($success) { |
|
82 | - return $cached; |
|
83 | - } |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * @param string |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - private function filePath($key) |
|
93 | - { |
|
94 | - return $this->dir . DIRECTORY_SEPARATOR . sha1($key) . '.php'; |
|
95 | - } |
|
15 | + /** @var string */ |
|
16 | + private $dir; |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * @param string temporary directory |
|
21 | + * |
|
22 | + * @throws MissingDirectoryException |
|
23 | + */ |
|
24 | + public function __construct($tempDir) |
|
25 | + { |
|
26 | + if (!is_dir($tempDir)) { |
|
27 | + throw new MissingDirectoryException("Directory '$tempDir' is missing."); |
|
28 | + } |
|
29 | + |
|
30 | + $dir = $tempDir . DIRECTORY_SEPARATOR . 'milo.github-api'; |
|
31 | + |
|
32 | + if (!is_dir($dir)) { |
|
33 | + set_error_handler(function($severity, $message, $file, $line) use ($dir, & $valid) { |
|
34 | + restore_error_handler(); |
|
35 | + if (!is_dir($dir)) { |
|
36 | + throw new MissingDirectoryException("Cannot create '$dir' directory.", 0, new \ErrorException($message, 0, $severity, $file, $line)); |
|
37 | + } |
|
38 | + }); |
|
39 | + mkdir($dir); |
|
40 | + restore_error_handler(); |
|
41 | + } |
|
42 | + |
|
43 | + $this->dir = $dir; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @param string |
|
49 | + * @param mixed |
|
50 | + * @return mixed stored value |
|
51 | + */ |
|
52 | + public function save($key, $value) |
|
53 | + { |
|
54 | + file_put_contents( |
|
55 | + $this->filePath($key), |
|
56 | + serialize($value), |
|
57 | + LOCK_EX |
|
58 | + ); |
|
59 | + |
|
60 | + return $value; |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * @param string |
|
66 | + * @return mixed|NULL |
|
67 | + */ |
|
68 | + public function load($key) |
|
69 | + { |
|
70 | + $path = $this->filePath($key); |
|
71 | + if (is_file($path) && ($fd = fopen($path, 'rb')) && flock($fd, LOCK_SH)) { |
|
72 | + $cached = stream_get_contents($fd); |
|
73 | + flock($fd, LOCK_UN); |
|
74 | + fclose($fd); |
|
75 | + |
|
76 | + $success = TRUE; |
|
77 | + set_error_handler(function() use (& $success) { return $success = FALSE; }, E_NOTICE); |
|
78 | + $cached = unserialize($cached); |
|
79 | + restore_error_handler(); |
|
80 | + |
|
81 | + if ($success) { |
|
82 | + return $cached; |
|
83 | + } |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * @param string |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + private function filePath($key) |
|
93 | + { |
|
94 | + return $this->dir . DIRECTORY_SEPARATOR . sha1($key) . '.php'; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | throw new MissingDirectoryException("Directory '$tempDir' is missing."); |
28 | 28 | } |
29 | 29 | |
30 | - $dir = $tempDir . DIRECTORY_SEPARATOR . 'milo.github-api'; |
|
30 | + $dir = $tempDir.DIRECTORY_SEPARATOR.'milo.github-api'; |
|
31 | 31 | |
32 | 32 | if (!is_dir($dir)) { |
33 | 33 | set_error_handler(function($severity, $message, $file, $line) use ($dir, & $valid) { |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | */ |
92 | 92 | private function filePath($key) |
93 | 93 | { |
94 | - return $this->dir . DIRECTORY_SEPARATOR . sha1($key) . '.php'; |
|
94 | + return $this->dir.DIRECTORY_SEPARATOR.sha1($key).'.php'; |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | } |