]> git.mxchange.org Git - friendica.git/blob - src/Security/OAuth1/OAuthRequest.php
Merge remote-tracking branch 'upstream/2021.12-rc' into lemmy
[friendica.git] / src / Security / OAuth1 / OAuthRequest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Security\OAuth1;
23
24 use Friendica\Util\Strings;
25
26 class OAuthRequest
27 {
28         private $parameters;
29         private $http_method;
30         private $http_url;
31         // for debug purposes
32         public $base_string;
33         public static $version = '1.0';
34         public static $POST_INPUT = 'php://input';
35
36         function __construct($http_method, $http_url, $parameters = null)
37         {
38                 @$parameters or $parameters = [];
39                 $parameters        = array_merge(OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
40                 $this->parameters  = $parameters;
41                 $this->http_method = $http_method;
42                 $this->http_url    = $http_url;
43         }
44
45
46         /**
47          * attempt to build up a request from what was passed to the server
48          *
49          * @param string|null $http_method
50          * @param string|null $http_url
51          * @param string|null $parameters
52          *
53          * @return OAuthRequest
54          */
55         public static function from_request($http_method = null, $http_url = null, $parameters = null)
56         {
57                 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
58                         ? 'http'
59                         : 'https';
60                 @$http_url or $http_url = $scheme .
61                                                                   '://' . $_SERVER['HTTP_HOST'] .
62                                                                   ':' .
63                                                                   $_SERVER['SERVER_PORT'] .
64                                                                   $_SERVER['REQUEST_URI'];
65                 @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
66
67                 // We weren't handed any parameters, so let's find the ones relevant to
68                 // this request.
69                 // If you run XML-RPC or similar you should use this to provide your own
70                 // parsed parameter-list
71                 if (!$parameters) {
72                         // Find request headers
73                         $request_headers = OAuthUtil::get_headers();
74
75                         // Parse the query-string to find GET parameters
76                         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
77
78                         // It's a POST request of the proper content-type, so parse POST
79                         // parameters and add those overriding any duplicates from GET
80                         if (
81                                 $http_method == "POST"
82                                 && @strstr(
83                                         $request_headers["Content-Type"],
84                                         "application/x-www-form-urlencoded"
85                                 )
86                         ) {
87                                 $post_data  = OAuthUtil::parse_parameters(
88                                         file_get_contents(self::$POST_INPUT)
89                                 );
90                                 $parameters = array_merge($parameters, $post_data);
91                         }
92
93                         // We have a Authorization-header with OAuth data. Parse the header
94                         // and add those overriding any duplicates from GET or POST
95                         if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
96                                 $header_parameters = OAuthUtil::split_header(
97                                         $request_headers['Authorization']
98                                 );
99                                 $parameters        = array_merge($parameters, $header_parameters);
100                         }
101                 }
102                 // fix for friendica redirect system
103
104                 $http_url = substr($http_url, 0, strpos($http_url, $parameters['pagename']) + strlen($parameters['pagename']));
105                 unset($parameters['pagename']);
106
107                 return new OAuthRequest($http_method, $http_url, $parameters);
108         }
109
110         /**
111          * pretty much a helper function to set up the request
112          *
113          * @param OAuthConsumer $consumer
114          * @param OAuthToken    $token
115          * @param string        $http_method
116          * @param string        $http_url
117          * @param array|null    $parameters
118          *
119          * @return OAuthRequest
120          */
121         public static function from_consumer_and_token(OAuthConsumer $consumer, $http_method, $http_url, array $parameters = null, OAuthToken $token = null)
122         {
123                 @$parameters or $parameters = [];
124                 $defaults = [
125                         "oauth_version"      => OAuthRequest::$version,
126                         "oauth_nonce"        => OAuthRequest::generate_nonce(),
127                         "oauth_timestamp"    => OAuthRequest::generate_timestamp(),
128                         "oauth_consumer_key" => $consumer->key,
129                 ];
130                 if ($token)
131                         $defaults['oauth_token'] = $token->key;
132
133                 $parameters = array_merge($defaults, $parameters);
134
135                 return new OAuthRequest($http_method, $http_url, $parameters);
136         }
137
138         public function set_parameter($name, $value, $allow_duplicates = true)
139         {
140                 if ($allow_duplicates && isset($this->parameters[$name])) {
141                         // We have already added parameter(s) with this name, so add to the list
142                         if (is_scalar($this->parameters[$name])) {
143                                 // This is the first duplicate, so transform scalar (string)
144                                 // into an array so we can add the duplicates
145                                 $this->parameters[$name] = [$this->parameters[$name]];
146                         }
147
148                         $this->parameters[$name][] = $value;
149                 } else {
150                         $this->parameters[$name] = $value;
151                 }
152         }
153
154         public function get_parameter($name)
155         {
156                 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
157         }
158
159         public function get_parameters()
160         {
161                 return $this->parameters;
162         }
163
164         public function unset_parameter($name)
165         {
166                 unset($this->parameters[$name]);
167         }
168
169         /**
170          * The request parameters, sorted and concatenated into a normalized string.
171          *
172          * @return string
173          */
174         public function get_signable_parameters()
175         {
176                 // Grab all parameters
177                 $params = $this->parameters;
178
179                 // Remove oauth_signature if present
180                 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
181                 if (isset($params['oauth_signature'])) {
182                         unset($params['oauth_signature']);
183                 }
184
185                 return OAuthUtil::build_http_query($params);
186         }
187
188         /**
189          * Returns the base string of this request
190          *
191          * The base string defined as the method, the url
192          * and the parameters (normalized), each urlencoded
193          * and the concated with &.
194          */
195         public function get_signature_base_string()
196         {
197                 $parts = [
198                         $this->get_normalized_http_method(),
199                         $this->get_normalized_http_url(),
200                         $this->get_signable_parameters(),
201                 ];
202
203                 $parts = OAuthUtil::urlencode_rfc3986($parts);
204
205                 return implode('&', $parts);
206         }
207
208         /**
209          * just uppercases the http method
210          */
211         public function get_normalized_http_method()
212         {
213                 return strtoupper($this->http_method);
214         }
215
216         /**
217          * parses the url and rebuilds it to be
218          * scheme://host/path
219          */
220         public function get_normalized_http_url()
221         {
222                 $parts = parse_url($this->http_url);
223
224                 $port   = @$parts['port'];
225                 $scheme = $parts['scheme'];
226                 $host   = $parts['host'];
227                 $path   = @$parts['path'];
228
229                 $port or $port = ($scheme == 'https') ? '443' : '80';
230
231                 if (($scheme == 'https' && $port != '443')
232                         || ($scheme == 'http' && $port != '80')
233                 ) {
234                         $host = "$host:$port";
235                 }
236                 return "$scheme://$host$path";
237         }
238
239         /**
240          * builds a url usable for a GET request
241          */
242         public function to_url()
243         {
244                 $post_data = $this->to_postdata();
245                 $out       = $this->get_normalized_http_url();
246                 if ($post_data) {
247                         $out .= '?' . $post_data;
248                 }
249                 return $out;
250         }
251
252         /**
253          * builds the data one would send in a POST request
254          *
255          * @param bool $raw
256          *
257          * @return array|string
258          */
259         public function to_postdata(bool $raw = false)
260         {
261                 if ($raw)
262                         return $this->parameters;
263                 else
264                         return OAuthUtil::build_http_query($this->parameters);
265         }
266
267         /**
268          * builds the Authorization: header
269          *
270          * @param string|null $realm
271          *
272          * @return string
273          * @throws OAuthException
274          */
275         public function to_header($realm = null)
276         {
277                 $first = true;
278                 if ($realm) {
279                         $out   = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
280                         $first = false;
281                 } else
282                         $out = 'Authorization: OAuth';
283
284                 foreach ($this->parameters as $k => $v) {
285                         if (substr($k, 0, 5) != "oauth") continue;
286                         if (is_array($v)) {
287                                 throw new OAuthException('Arrays not supported in headers');
288                         }
289                         $out   .= ($first) ? ' ' : ',';
290                         $out   .= OAuthUtil::urlencode_rfc3986($k) .
291                                           '="' .
292                                           OAuthUtil::urlencode_rfc3986($v) .
293                                           '"';
294                         $first = false;
295                 }
296                 return $out;
297         }
298
299         public function __toString()
300         {
301                 return $this->to_url();
302         }
303
304
305         public function sign_request(Signature\OAuthSignatureMethod $signature_method, $consumer, $token)
306         {
307                 $this->set_parameter(
308                         "oauth_signature_method",
309                         $signature_method->get_name(),
310                         false
311                 );
312                 $signature = $this->build_signature($signature_method, $consumer, $token);
313                 $this->set_parameter("oauth_signature", $signature, false);
314         }
315
316         public function build_signature(Signature\OAuthSignatureMethod $signature_method, $consumer, $token)
317         {
318                 $signature = $signature_method->build_signature($this, $consumer, $token);
319                 return $signature;
320         }
321
322         /**
323          * util function: current timestamp
324          */
325         private static function generate_timestamp()
326         {
327                 return time();
328         }
329
330         /**
331          * util function: current nonce
332          */
333         private static function generate_nonce()
334         {
335                 return Strings::getRandomHex(32);
336         }
337 }