-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathFileRequester.php
More file actions
146 lines (129 loc) · 4.61 KB
/
Copy pathFileRequester.php
File metadata and controls
146 lines (129 loc) · 4.61 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace DuoAPI;
class FileRequester implements Requester
{
public function __construct()
{
$this->http_options = [
"http" => [
/*
* We'll handle HTTP errors on our own
*/
"ignore_errors" => true,
],
"ssl" => [
/*
* Disallow self-signed certificates
*/
"allow_self_signed" => false,
/*
* Enforce CN verification
*/
"verify_peer" => true,
/*
* Avoid compression (CRIME attack)
*/
"disable_compression" => true,
/*
* Require good ciphers. View the list with:
*
* openssl ciphers -v 'HIGH:!SSLv2:!SSLv3'
*/
"ciphers" => "HIGH:!SSLv2:!SSLv3",
],
];
}
public function __destruct()
{
}
protected static function parse_http_response_header($headers)
{
/*
* It's possible that there will be multiple HTTP status codes in
* our array. For example, this can happen when we're redirected so
* we receive a 301 then 200. We should take the last status code
* received.
*/
$status_code_regex = '#^HTTP/\d\.\d[ \t]+(?<http_status_code>\d+)#i';
$status_code_headers = preg_grep($status_code_regex, $headers);
$status_codes = array_map(
function ($header) use ($status_code_regex) {
$status_code = preg_match($status_code_regex, $header, $matches);
return (int) $matches['http_status_code'];
},
$status_code_headers
);
return end($status_codes);
}
public function options($options)
{
assert(is_array($options));
if (isset($options["user_agent"])) {
$this->http_options["http"]["user_agent"] = $options["user_agent"];
}
if (isset($options["timeout"])) {
$this->http_options["http"]["timeout"] = $options["timeout"];
}
if (isset($options["proxy_url"])) {
$uri = $options["proxy_url"];
$uri .= (isset($options["proxy_port"]) ? ":" . $options["proxy_port"] : "");
$this->http_options["http"]["proxy"] = $uri;
}
if (isset($options["ca"])) {
$this->http_options["ssl"]["cafile"] = $options["ca"];
}
}
public function execute($url, $method, $headers, $body = null)
{
assert(is_string($url));
assert(is_string($method));
assert(is_array($headers));
assert(is_string($body) || is_null($body));
$headers = array_map(function ($key, $value) {
return sprintf("%s: %s", $key, $value);
}, array_keys($headers), array_values($headers));
$this->http_options['http']['method'] = $method;
$this->http_options['http']['header'] = $headers;
if ($method === "POST") {
$this->http_options['http']['content'] = $body;
} else {
unset($this->http_options['http']['content']);
}
$context = stream_context_create($this->http_options);
$result = @file_get_contents($url, false, $context);
$http_status_code = null;
$success = true;
if ($result === false) {
$error = error_get_last();
$errno = $error["type"];
$message = $error["message"];
/**
* We could simply leave the result as FALSE and return that, but
* let's convert it to what looks like an actual Duo web response.
* This is beneficial because it simplifies the two error cases
* we expect:
*
* 1. We had some sort of malformed request and Duo rejected it.
*
* 2. We couldn't reach Duo (this is the case we'd expect to
* return FALSE).
*/
$result = json_encode(
[
'stat' => 'FAIL',
'code' => $errno,
'message' => $message,
]
);
$success = false;
} else {
// https://secure.php.net/manual/en/reserved.variables.httpresponseheader.php
$http_status_code = self::parse_http_response_header($http_response_header);
}
return [
"response" => $result,
"success" => $success,
"http_status_code" => $http_status_code
];
}
}