3 namespace Friendica\Security\OAuth1;
6 use Friendica\Security\OAuth1\OAuthUtil;
15 public static $version = '1.0';
16 public static $POST_INPUT = 'php://input';
18 function __construct($http_method, $http_url, $parameters = null)
20 @$parameters or $parameters = [];
21 $parameters = array_merge(OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
22 $this->parameters = $parameters;
23 $this->http_method = $http_method;
24 $this->http_url = $http_url;
29 * attempt to build up a request from what was passed to the server
31 * @param string|null $http_method
32 * @param string|null $http_url
33 * @param string|null $parameters
35 * @return OAuthRequest
37 public static function from_request($http_method = null, $http_url = null, $parameters = null)
39 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
42 @$http_url or $http_url = $scheme .
43 '://' . $_SERVER['HTTP_HOST'] .
45 $_SERVER['SERVER_PORT'] .
46 $_SERVER['REQUEST_URI'];
47 @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
49 // We weren't handed any parameters, so let's find the ones relevant to
51 // If you run XML-RPC or similar you should use this to provide your own
52 // parsed parameter-list
54 // Find request headers
55 $request_headers = OAuthUtil::get_headers();
57 // Parse the query-string to find GET parameters
58 $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
60 // It's a POST request of the proper content-type, so parse POST
61 // parameters and add those overriding any duplicates from GET
63 $http_method == "POST"
65 $request_headers["Content-Type"],
66 "application/x-www-form-urlencoded"
69 $post_data = OAuthUtil::parse_parameters(
70 file_get_contents(self::$POST_INPUT)
72 $parameters = array_merge($parameters, $post_data);
75 // We have a Authorization-header with OAuth data. Parse the header
76 // and add those overriding any duplicates from GET or POST
77 if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
78 $header_parameters = OAuthUtil::split_header(
79 $request_headers['Authorization']
81 $parameters = array_merge($parameters, $header_parameters);
84 // fix for friendica redirect system
86 $http_url = substr($http_url, 0, strpos($http_url, $parameters['pagename']) + strlen($parameters['pagename']));
87 unset($parameters['pagename']);
89 return new OAuthRequest($http_method, $http_url, $parameters);
93 * pretty much a helper function to set up the request
95 * @param \Friendica\Security\OAuth1\OAuthConsumer $consumer
96 * @param \Friendica\Security\OAuth1\OAuthToken $token
97 * @param string $http_method
98 * @param string $http_url
99 * @param array|null $parameters
101 * @return OAuthRequest
103 public static function from_consumer_and_token(\Friendica\Security\OAuth1\OAuthConsumer $consumer, $http_method, $http_url, array $parameters = null, \Friendica\Security\OAuth1\OAuthToken $token = null)
105 @$parameters or $parameters = [];
107 "oauth_version" => OAuthRequest::$version,
108 "oauth_nonce" => OAuthRequest::generate_nonce(),
109 "oauth_timestamp" => OAuthRequest::generate_timestamp(),
110 "oauth_consumer_key" => $consumer->key,
113 $defaults['oauth_token'] = $token->key;
115 $parameters = array_merge($defaults, $parameters);
117 return new OAuthRequest($http_method, $http_url, $parameters);
120 public function set_parameter($name, $value, $allow_duplicates = true)
122 if ($allow_duplicates && isset($this->parameters[$name])) {
123 // We have already added parameter(s) with this name, so add to the list
124 if (is_scalar($this->parameters[$name])) {
125 // This is the first duplicate, so transform scalar (string)
126 // into an array so we can add the duplicates
127 $this->parameters[$name] = [$this->parameters[$name]];
130 $this->parameters[$name][] = $value;
132 $this->parameters[$name] = $value;
136 public function get_parameter($name)
138 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
141 public function get_parameters()
143 return $this->parameters;
146 public function unset_parameter($name)
148 unset($this->parameters[$name]);
152 * The request parameters, sorted and concatenated into a normalized string.
156 public function get_signable_parameters()
158 // Grab all parameters
159 $params = $this->parameters;
161 // Remove oauth_signature if present
162 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
163 if (isset($params['oauth_signature'])) {
164 unset($params['oauth_signature']);
167 return OAuthUtil::build_http_query($params);
171 * Returns the base string of this request
173 * The base string defined as the method, the url
174 * and the parameters (normalized), each urlencoded
175 * and the concated with &.
177 public function get_signature_base_string()
180 $this->get_normalized_http_method(),
181 $this->get_normalized_http_url(),
182 $this->get_signable_parameters(),
185 $parts = OAuthUtil::urlencode_rfc3986($parts);
187 return implode('&', $parts);
191 * just uppercases the http method
193 public function get_normalized_http_method()
195 return strtoupper($this->http_method);
199 * parses the url and rebuilds it to be
202 public function get_normalized_http_url()
204 $parts = parse_url($this->http_url);
206 $port = @$parts['port'];
207 $scheme = $parts['scheme'];
208 $host = $parts['host'];
209 $path = @$parts['path'];
211 $port or $port = ($scheme == 'https') ? '443' : '80';
213 if (($scheme == 'https' && $port != '443')
214 || ($scheme == 'http' && $port != '80')
216 $host = "$host:$port";
218 return "$scheme://$host$path";
222 * builds a url usable for a GET request
224 public function to_url()
226 $post_data = $this->to_postdata();
227 $out = $this->get_normalized_http_url();
229 $out .= '?' . $post_data;
235 * builds the data one would send in a POST request
239 * @return array|string
241 public function to_postdata(bool $raw = false)
244 return $this->parameters;
246 return OAuthUtil::build_http_query($this->parameters);
250 * builds the Authorization: header
252 * @param string|null $realm
255 * @throws \Friendica\Security\OAuth1\OAuthException
257 public function to_header($realm = null)
261 $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
264 $out = 'Authorization: OAuth';
266 foreach ($this->parameters as $k => $v) {
267 if (substr($k, 0, 5) != "oauth") continue;
269 throw new \Friendica\Security\OAuth1\OAuthException('Arrays not supported in headers');
271 $out .= ($first) ? ' ' : ',';
272 $out .= OAuthUtil::urlencode_rfc3986($k) .
274 OAuthUtil::urlencode_rfc3986($v) .
281 public function __toString()
283 return $this->to_url();
287 public function sign_request(\Friendica\Security\OAuth1\OAuthSignatureMethod $signature_method, $consumer, $token)
289 $this->set_parameter(
290 "oauth_signature_method",
291 $signature_method->get_name(),
294 $signature = $this->build_signature($signature_method, $consumer, $token);
295 $this->set_parameter("oauth_signature", $signature, false);
298 public function build_signature(\Friendica\Security\OAuth1\OAuthSignatureMethod $signature_method, $consumer, $token)
300 $signature = $signature_method->build_signature($this, $consumer, $token);
305 * util function: current timestamp
307 private static function generate_timestamp()
313 * util function: current nonce
315 private static function generate_nonce()
317 return Friendica\Util\Strings::getRandomHex(32);