|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Invoiced\OAuth1\Client\Server; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7; |
|
6
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
7
|
|
|
use League\OAuth1\Client\Signature\Signature; |
|
8
|
|
|
use League\OAuth1\Client\Signature\SignatureInterface; |
|
9
|
|
|
|
|
10
|
|
|
class RsaSha1Signature extends Signature implements SignatureInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
public function method() |
|
16
|
|
|
{ |
|
17
|
|
|
return 'RSA-SHA1'; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function sign($uri, array $parameters = array(), $method = 'POST') |
|
24
|
|
|
{ |
|
25
|
|
|
$url = $this->createUrl($uri); |
|
26
|
|
|
$baseString = $this->baseString($url, $method, $parameters); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$privateKey = $this->clientCredentials->getRsaPrivateKey(); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
openssl_sign($baseString, $signature, $privateKey); |
|
31
|
|
|
|
|
32
|
|
|
return base64_encode($signature); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a Guzzle url for the given URI. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $uri |
|
39
|
|
|
* |
|
40
|
|
|
* @return Url |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function createUrl($uri) |
|
43
|
|
|
{ |
|
44
|
|
|
return Psr7\uri_for($uri); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Generate a base string for a RSA-SHA1 signature |
|
49
|
|
|
* based on the given a url, method, and any parameters. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Url $url |
|
52
|
|
|
* @param string $method |
|
53
|
|
|
* @param array $parameters |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function baseString(Uri $url, $method = 'POST', array $parameters = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
$baseString = rawurlencode($method).'&'; |
|
60
|
|
|
|
|
61
|
|
|
$schemeHostPath = Uri::fromParts(array( |
|
62
|
|
|
'scheme' => $url->getScheme(), |
|
63
|
|
|
'host' => $url->getHost(), |
|
64
|
|
|
'path' => $url->getPath(), |
|
65
|
|
|
)); |
|
66
|
|
|
|
|
67
|
|
|
$baseString .= rawurlencode($schemeHostPath).'&'; |
|
68
|
|
|
|
|
69
|
|
|
$data = array(); |
|
|
|
|
|
|
70
|
|
|
parse_str($url->getQuery(), $query); |
|
71
|
|
|
$data = array_merge($query, $parameters); |
|
72
|
|
|
|
|
73
|
|
|
// normalize data key/values |
|
74
|
|
|
array_walk_recursive($data, function (&$key, &$value) { |
|
75
|
|
|
$key = rawurlencode(rawurldecode($key)); |
|
76
|
|
|
$value = rawurlencode(rawurldecode($value)); |
|
77
|
|
|
}); |
|
78
|
|
|
ksort($data); |
|
79
|
|
|
|
|
80
|
|
|
$baseString .= $this->queryStringFromData($data); |
|
81
|
|
|
|
|
82
|
|
|
return $baseString; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Creates an array of rawurlencoded strings out of each array key/value pair |
|
87
|
|
|
* Handles multi-demensional arrays recursively. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $data Array of parameters to convert. |
|
90
|
|
|
* @param array $queryParams Array to extend. False by default. |
|
91
|
|
|
* @param string $prevKey Optional Array key to append |
|
92
|
|
|
* |
|
93
|
|
|
* @return string rawurlencoded string version of data |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function queryStringFromData($data, $queryParams = false, $prevKey = '') |
|
96
|
|
|
{ |
|
97
|
|
|
if ($initial = (false === $queryParams)) { |
|
98
|
|
|
$queryParams = array(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
foreach ($data as $key => $value) { |
|
102
|
|
|
if ($prevKey) { |
|
103
|
|
|
$key = $prevKey.'['.$key.']'; // Handle multi-dimensional array |
|
104
|
|
|
} |
|
105
|
|
|
if (is_array($value)) { |
|
106
|
|
|
$queryParams = $this->queryStringFromData($value, $queryParams, $key); |
|
107
|
|
|
} else { |
|
108
|
|
|
$queryParams[] = rawurlencode($key.'='.$value); // join with equals sign |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($initial) { |
|
113
|
|
|
return implode('%26', $queryParams); // join with ampersand |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $queryParams; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.