Nextrek
@ 461
Nextrek / Android / SmartCharging / endPoints / nightly / fb_SDK / src / Facebook / Entities / SignedRequest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
<?php /** * Copyright 2014 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary * form for use in connection with the web services and APIs provided by * Facebook. * * As with any software that integrates with the Facebook platform, your use * of this software is subject to the Facebook Developer Principles and * Policies [http://developers.facebook.com/policy/]. This copyright notice * shall be included in all copies or substantial portions of the software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * */ namespace Facebook\Entities; use Facebook\FacebookSDKException; use Facebook\FacebookSession; /** * Class SignedRequest * @package Facebook */ class SignedRequest { /** * @var string */ public $rawSignedRequest; /** * @var array */ public $payload; /** * Instantiate a new SignedRequest entity. * * @param string|null $rawSignedRequest The raw signed request. * @param string|null $state random string to prevent CSRF. * @param string|null $appSecret */ public function __construct($rawSignedRequest = null, $state = null, $appSecret = null) { if (!$rawSignedRequest) { return; } $this->rawSignedRequest = $rawSignedRequest; $this->payload = static::parse($rawSignedRequest, $state, $appSecret); } /** * Returns the raw signed request data. * * @return string|null */ public function getRawSignedRequest() { return $this->rawSignedRequest; } /** * Returns the parsed signed request data. * * @return array|null */ public function getPayload() { return $this->payload; } /** * Returns a property from the signed request data if available. * * @param string $key * @param mixed|null $default * * @return mixed|null */ public function get($key, $default = null) { if (isset($this->payload[$key])) { return $this->payload[$key]; } return $default; } /** * Returns user_id from signed request data if available. * * @return string|null */ public function getUserId() { return $this->get('user_id'); } /** * Checks for OAuth data in the payload. * * @return boolean */ public function hasOAuthData() { return isset($this->payload['oauth_token']) || isset($this->payload['code']); } /** * Creates a signed request from an array of data. * * @param array $payload * @param string|null $appSecret * * @return string */ public static function make(array $payload, $appSecret = null) { $payload['algorithm'] = 'HMAC-SHA256'; $payload['issued_at'] = time(); $encodedPayload = static::base64UrlEncode(json_encode($payload)); $hashedSig = static::hashSignature($encodedPayload, $appSecret); $encodedSig = static::base64UrlEncode($hashedSig); return $encodedSig.'.'.$encodedPayload; } /** * Validates and decodes a signed request and returns * the payload as an array. * * @param string $signedRequest * @param string|null $state * @param string|null $appSecret * * @return array */ public static function parse($signedRequest, $state = null, $appSecret = null) { list($encodedSig, $encodedPayload) = static::split($signedRequest); // Signature validation $sig = static::decodeSignature($encodedSig); $hashedSig = static::hashSignature($encodedPayload, $appSecret); static::validateSignature($hashedSig, $sig); // Payload validation $data = static::decodePayload($encodedPayload); static::validateAlgorithm($data); if ($state) { static::validateCsrf($data, $state); } return $data; } /** * Validates the format of a signed request. * * @param string $signedRequest * * @throws FacebookSDKException */ public static function validateFormat($signedRequest) { if (strpos($signedRequest, '.') !== false) { return; } throw new FacebookSDKException( 'Malformed signed request.', 606 ); } /** * Decodes a raw valid signed request. * * @param string $signedRequest * * @return array */ public static function split($signedRequest) { static::validateFormat($signedRequest); return explode('.', $signedRequest, 2); } /** * Decodes the raw signature from a signed request. * * @param string $encodedSig * * @return string * * @throws FacebookSDKException */ public static function decodeSignature($encodedSig) { $sig = static::base64UrlDecode($encodedSig); if ($sig) { return $sig; } throw new FacebookSDKException( 'Signed request has malformed encoded signature data.', 607 ); } /** * Decodes the raw payload from a signed request. * * @param string $encodedPayload * * @return array * * @throws FacebookSDKException */ public static function decodePayload($encodedPayload) { $payload = static::base64UrlDecode($encodedPayload); if ($payload) { $payload = json_decode($payload, true); } if (is_array($payload)) { return $payload; } throw new FacebookSDKException( 'Signed request has malformed encoded payload data.', 607 ); } /** * Validates the algorithm used in a signed request. * * @param array $data * * @throws FacebookSDKException */ public static function validateAlgorithm(array $data) { if (isset($data['algorithm']) && $data['algorithm'] === 'HMAC-SHA256') { return; } throw new FacebookSDKException( 'Signed request is using the wrong algorithm.', 605 ); } /** * Hashes the signature used in a signed request. * * @param string $encodedData * @param string|null $appSecret * * @return string * * @throws FacebookSDKException */ public static function hashSignature($encodedData, $appSecret = null) { $hashedSig = hash_hmac( 'sha256', $encodedData, FacebookSession::_getTargetAppSecret($appSecret), $raw_output = true ); if ($hashedSig) { return $hashedSig; } throw new FacebookSDKException( 'Unable to hash signature from encoded payload data.', 602 ); } /** * Validates the signature used in a signed request. * * @param string $hashedSig * @param string $sig * * @throws FacebookSDKException */ public static function validateSignature($hashedSig, $sig) { $intSignatureLength = mb_strlen($sig); if (mb_strlen($hashedSig) === $intSignatureLength) { $validate = 0; for ($i = 0; $i < $intSignatureLength; $i++) { $validate |= ord($hashedSig[$i]) ^ ord($sig[$i]); } if ($validate === 0) { return; } } throw new FacebookSDKException( 'Signed request has an invalid signature.', 602 ); } /** * Validates a signed request against CSRF. * * @param array $data * @param string $state * * @throws FacebookSDKException */ public static function validateCsrf(array $data, $state) { if (isset($data['state'])) { $savedLen = strlen($state); $givenLen = strlen($data['state']); if ($savedLen == $givenLen) { $result = 0; for ($i = 0; $i < $savedLen; $i++) { $result |= ord($state[$i]) ^ ord($data['state'][$i]); } if ($result === 0) { return; } } } throw new FacebookSDKException( 'Signed request did not pass CSRF validation.', 604 ); } /** * Base64 decoding which replaces characters: * + instead of - * / instead of _ * @link http://en.wikipedia.org/wiki/Base64#URL_applications * * @param string $input base64 url encoded input * * @return string decoded string */ public static function base64UrlDecode($input) { $urlDecodedBase64 = strtr($input, '-_', '+/'); static::validateBase64($urlDecodedBase64); return base64_decode($urlDecodedBase64); } /** * Base64 encoding which replaces characters: * + instead of - * / instead of _ * @link http://en.wikipedia.org/wiki/Base64#URL_applications * * @param string $input string to encode * * @return string base64 url encoded input */ public static function base64UrlEncode($input) { return strtr(base64_encode($input), '+/', '-_'); } /** * Validates a base64 string. * * @param string $input base64 value to validate * * @throws FacebookSDKException */ public static function validateBase64($input) { $pattern = '/^[a-zA-Z0-9\/\r\n+]*={0,2}$/'; if (preg_match($pattern, $input)) { return; } throw new FacebookSDKException( 'Signed request contains malformed base64 encoding.', 608 ); } } |
Commits for Nextrek/Android/SmartCharging/endPoints/nightly/fb_SDK/src/Facebook/Entities/SignedRequest.php
Revision | Author | Commited | Message |
---|---|---|---|
461 | FSallustio | Mon 03 Aug, 2015 10:04:56 +0000 | Aggiunto supporto login Facebook, logout utente e modifiche (solo lato repo) all’utente e al locale. |