1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jaeger |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
6
|
|
|
* @link http://jaeger-app.com |
7
|
|
|
* @version 1.0 |
8
|
|
|
* @filesource ./Rest/Hmac.php |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace JaegerApp\Rest; |
12
|
|
|
|
13
|
|
|
use PhilipBrown\Signature\Auth; |
14
|
|
|
use PhilipBrown\Signature\Token; |
15
|
|
|
use PhilipBrown\Signature\Guards\CheckKey; |
16
|
|
|
use PhilipBrown\Signature\Guards\CheckVersion; |
17
|
|
|
use PhilipBrown\Signature\Guards\CheckTimestamp; |
18
|
|
|
use PhilipBrown\Signature\Guards\CheckSignature; |
19
|
|
|
use PhilipBrown\Signature\Exceptions\SignatureException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Jaeger - REST Hmac Object |
23
|
|
|
* |
24
|
|
|
* Allows for Hmac authentication |
25
|
|
|
* |
26
|
|
|
* @package Rest\Authentication |
27
|
|
|
* @author Eric Lamb <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Hmac |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The prefix authentication keys will be named after |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $prefix = 'm62_auth_'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The REQUEST method we're working with |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $method = 'get'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The route being requested |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $route = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The data to compare |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $data = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Authenticates a request |
57
|
|
|
* @param string $key |
58
|
|
|
* @param string $secret |
59
|
|
|
*/ |
60
|
|
|
public function auth($key, $secret) |
61
|
|
|
{ |
62
|
|
|
//make sure we have the require attributes |
63
|
|
|
$required = array('timestamp'); |
64
|
|
|
$data = $this->getData(); |
65
|
|
|
foreach($required AS $require) { |
66
|
|
|
if(!isset($data[$this->prefix.$require])) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//looks good, now let's process it |
72
|
|
|
$auth = new Auth($this->getMethod(), $this->getRoute(), $this->getData(), [ |
|
|
|
|
73
|
|
|
new CheckKey, |
74
|
|
|
new CheckVersion, |
75
|
|
|
new CheckTimestamp, |
76
|
|
|
new CheckSignature |
77
|
|
|
]); |
78
|
|
|
$token = new Token($key, $secret); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
return $auth->attempt($token, $this->prefix); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
catch (SignatureException $e) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the HTTP method to use |
91
|
|
|
* @param string $method |
92
|
|
|
* @return \JaegerApp\Rest\Hmac |
93
|
|
|
*/ |
94
|
|
|
public function setMethod($method) |
95
|
|
|
{ |
96
|
|
|
$this->method = $method; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the HTTP method |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getMethod() |
105
|
|
|
{ |
106
|
|
|
return $this->method; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets the route to use |
111
|
|
|
* @param string $route |
112
|
|
|
* @return \JaegerApp\Rest\Hmac |
113
|
|
|
*/ |
114
|
|
|
public function setRoute($route) |
115
|
|
|
{ |
116
|
|
|
$this->route = $route; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the route |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getRoute() |
125
|
|
|
{ |
126
|
|
|
return $this->route; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the data payload to use |
131
|
|
|
* @param string $data |
132
|
|
|
* @return \JaegerApp\Rest\Hmac |
133
|
|
|
*/ |
134
|
|
|
public function setData($data) |
135
|
|
|
{ |
136
|
|
|
$this->data = $data; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the data |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getData() |
145
|
|
|
{ |
146
|
|
|
return $this->data; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getPrefix() |
150
|
|
|
{ |
151
|
|
|
return $this->prefix; |
152
|
|
|
} |
153
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: