Cheetah
SignedRequest.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook;
25 
27 
34 {
38  protected $app;
39 
43  protected $rawSignedRequest;
44 
48  protected $payload;
49 
56  public function __construct(FacebookApp $facebookApp, $rawSignedRequest = null)
57  {
58  $this->app = $facebookApp;
59 
60  if (!$rawSignedRequest) {
61  return;
62  }
63 
64  $this->rawSignedRequest = $rawSignedRequest;
65 
66  $this->parse();
67  }
68 
74  public function getRawSignedRequest()
75  {
76  return $this->rawSignedRequest;
77  }
78 
84  public function getPayload()
85  {
86  return $this->payload;
87  }
88 
97  public function get($key, $default = null)
98  {
99  if (isset($this->payload[$key])) {
100  return $this->payload[$key];
101  }
102 
103  return $default;
104  }
105 
111  public function getUserId()
112  {
113  return $this->get('user_id');
114  }
115 
121  public function hasOAuthData()
122  {
123  return $this->get('oauth_token') || $this->get('code');
124  }
125 
133  public function make(array $payload)
134  {
135  $payload['algorithm'] = isset($payload['algorithm']) ? $payload['algorithm'] : 'HMAC-SHA256';
136  $payload['issued_at'] = isset($payload['issued_at']) ? $payload['issued_at'] : time();
137  $encodedPayload = $this->base64UrlEncode(json_encode($payload));
138 
139  $hashedSig = $this->hashSignature($encodedPayload);
140  $encodedSig = $this->base64UrlEncode($hashedSig);
141 
142  return $encodedSig . '.' . $encodedPayload;
143  }
144 
149  protected function parse()
150  {
151  list($encodedSig, $encodedPayload) = $this->split();
152 
153  // Signature validation
154  $sig = $this->decodeSignature($encodedSig);
155  $hashedSig = $this->hashSignature($encodedPayload);
156  $this->validateSignature($hashedSig, $sig);
157 
158  $this->payload = $this->decodePayload($encodedPayload);
159 
160  // Payload validation
161  $this->validateAlgorithm();
162  }
163 
171  protected function split()
172  {
173  if (strpos($this->rawSignedRequest, '.') === false) {
174  throw new FacebookSDKException('Malformed signed request.', 606);
175  }
176 
177  return explode('.', $this->rawSignedRequest, 2);
178  }
179 
189  protected function decodeSignature($encodedSig)
190  {
191  $sig = $this->base64UrlDecode($encodedSig);
192 
193  if (!$sig) {
194  throw new FacebookSDKException('Signed request has malformed encoded signature data.', 607);
195  }
196 
197  return $sig;
198  }
199 
209  protected function decodePayload($encodedPayload)
210  {
211  $payload = $this->base64UrlDecode($encodedPayload);
212 
213  if ($payload) {
214  $payload = json_decode($payload, true);
215  }
216 
217  if (!is_array($payload)) {
218  throw new FacebookSDKException('Signed request has malformed encoded payload data.', 607);
219  }
220 
221  return $payload;
222  }
223 
229  protected function validateAlgorithm()
230  {
231  if ($this->get('algorithm') !== 'HMAC-SHA256') {
232  throw new FacebookSDKException('Signed request is using the wrong algorithm.', 605);
233  }
234  }
235 
245  protected function hashSignature($encodedData)
246  {
247  $hashedSig = hash_hmac(
248  'sha256',
249  $encodedData,
250  $this->app->getSecret(),
251  $raw_output = true
252  );
253 
254  if (!$hashedSig) {
255  throw new FacebookSDKException('Unable to hash signature from encoded payload data.', 602);
256  }
257 
258  return $hashedSig;
259  }
260 
269  protected function validateSignature($hashedSig, $sig)
270  {
271  if (\hash_equals($hashedSig, $sig)) {
272  return;
273  }
274 
275  throw new FacebookSDKException('Signed request has an invalid signature.', 602);
276  }
277 
289  public function base64UrlDecode($input)
290  {
291  $urlDecodedBase64 = strtr($input, '-_', '+/');
292  $this->validateBase64($urlDecodedBase64);
293 
294  return base64_decode($urlDecodedBase64);
295  }
296 
308  public function base64UrlEncode($input)
309  {
310  return strtr(base64_encode($input), '+/', '-_');
311  }
312 
320  protected function validateBase64($input)
321  {
322  if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $input)) {
323  throw new FacebookSDKException('Signed request contains malformed base64 encoding.', 608);
324  }
325  }
326 }
Facebook\SignedRequest\validateSignature
validateSignature($hashedSig, $sig)
Definition: SignedRequest.php:269
Facebook\SignedRequest\hashSignature
hashSignature($encodedData)
Definition: SignedRequest.php:245
Facebook\SignedRequest\__construct
__construct(FacebookApp $facebookApp, $rawSignedRequest=null)
Definition: SignedRequest.php:56
Facebook\SignedRequest\base64UrlEncode
base64UrlEncode($input)
Definition: SignedRequest.php:308
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Facebook\SignedRequest\getPayload
getPayload()
Definition: SignedRequest.php:84
Facebook\SignedRequest\hasOAuthData
hasOAuthData()
Definition: SignedRequest.php:121
Facebook\SignedRequest\$app
$app
Definition: SignedRequest.php:38
Facebook\SignedRequest\base64UrlDecode
base64UrlDecode($input)
Definition: SignedRequest.php:289
Facebook\SignedRequest\split
split()
Definition: SignedRequest.php:171
Facebook\SignedRequest\make
make(array $payload)
Definition: SignedRequest.php:133
Facebook\SignedRequest\decodeSignature
decodeSignature($encodedSig)
Definition: SignedRequest.php:189
Facebook\SignedRequest\$payload
$payload
Definition: SignedRequest.php:48
Facebook\FacebookApp
Definition: FacebookApp.php:30
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
Facebook
Facebook\SignedRequest
Definition: SignedRequest.php:34
Facebook\SignedRequest\$rawSignedRequest
$rawSignedRequest
Definition: SignedRequest.php:43
Facebook\SignedRequest\validateAlgorithm
validateAlgorithm()
Definition: SignedRequest.php:229
Facebook\SignedRequest\getUserId
getUserId()
Definition: SignedRequest.php:111
Facebook\SignedRequest\decodePayload
decodePayload($encodedPayload)
Definition: SignedRequest.php:209
Facebook\SignedRequest\getRawSignedRequest
getRawSignedRequest()
Definition: SignedRequest.php:74
Facebook\SignedRequest\parse
parse()
Definition: SignedRequest.php:149
Facebook\SignedRequest\validateBase64
validateBase64($input)
Definition: SignedRequest.php:320