Nextrek
@ 461
Nextrek / Android / SmartCharging / endPoints / nightly / fb_SDK / src / Facebook / Entities / AccessToken.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 |
<?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\FacebookRequest; use Facebook\FacebookRequestException; use Facebook\FacebookSession; use Facebook\GraphSessionInfo; /** * Class AccessToken * @package Facebook */ class AccessToken { /** * The access token. * * @var string */ protected $accessToken; /** * A unique ID to identify a client. * * @var string */ protected $machineId; /** * Date when token expires. * * @var \DateTime|null */ protected $expiresAt; /** * Create a new access token entity. * * @param string $accessToken * @param int $expiresAt * @param string|null machineId */ public function __construct($accessToken, $expiresAt = 0, $machineId = null) { $this->accessToken = $accessToken; if ($expiresAt) { $this->setExpiresAtFromTimeStamp($expiresAt); } $this->machineId = $machineId; } /** * Setter for expires_at. * * @param int $timeStamp */ protected function setExpiresAtFromTimeStamp($timeStamp) { $dt = new \DateTime(); $dt->setTimestamp($timeStamp); $this->expiresAt = $dt; } /** * Getter for expiresAt. * * @return \DateTime|null */ public function getExpiresAt() { return $this->expiresAt; } /** * Getter for machineId. * * @return string|null */ public function getMachineId() { return $this->machineId; } /** * Determines whether or not this is a long-lived token. * * @return bool */ public function isLongLived() { if ($this->expiresAt) { return $this->expiresAt->getTimestamp() > time() + (60 * 60 * 2); } return false; } /** * Checks the validity of the access token. * * @param string|null $appId Application ID to use * @param string|null $appSecret App secret value to use * @param string|null $machineId * * @return boolean */ public function isValid($appId = null, $appSecret = null, $machineId = null) { $accessTokenInfo = $this->getInfo($appId, $appSecret); $machineId = $machineId ?: $this->machineId; return static::validateAccessToken($accessTokenInfo, $appId, $machineId); } /** * Ensures the provided GraphSessionInfo object is valid, * throwing an exception if not. Ensures the appId matches, * that the machineId matches if it's being used, * that the token is valid and has not expired. * * @param GraphSessionInfo $tokenInfo * @param string|null $appId Application ID to use * @param string|null $machineId * * @return boolean */ public static function validateAccessToken(GraphSessionInfo $tokenInfo, $appId = null, $machineId = null) { $targetAppId = FacebookSession::_getTargetAppId($appId); $appIdIsValid = $tokenInfo->getAppId() == $targetAppId; $machineIdIsValid = $tokenInfo->getProperty('machine_id') == $machineId; $accessTokenIsValid = $tokenInfo->isValid(); $accessTokenIsStillAlive = true; // Not all access tokens return an expiration. E.g. an app access token. if ($tokenInfo->getExpiresAt() instanceof \DateTime) { $accessTokenIsStillAlive = $tokenInfo->getExpiresAt()->getTimestamp() >= time(); } return $appIdIsValid && $machineIdIsValid && $accessTokenIsValid && $accessTokenIsStillAlive; } /** * Get a valid access token from a code. * * @param string $code * @param string|null $appId * @param string|null $appSecret * @param string|null $machineId * * @return AccessToken */ public static function getAccessTokenFromCode($code, $appId = null, $appSecret = null, $machineId = null) { $params = array( 'code' => $code, 'redirect_uri' => '', ); if ($machineId) { $params['machine_id'] = $machineId; } return static::requestAccessToken($params, $appId, $appSecret); } /** * Get a valid code from an access token. * * @param AccessToken|string $accessToken * @param string|null $appId * @param string|null $appSecret * * @return AccessToken */ public static function getCodeFromAccessToken($accessToken, $appId = null, $appSecret = null) { $accessToken = (string) $accessToken; $params = array( 'access_token' => $accessToken, 'redirect_uri' => '', ); return static::requestCode($params, $appId, $appSecret); } /** * Exchanges a short lived access token with a long lived access token. * * @param string|null $appId * @param string|null $appSecret * * @return AccessToken */ public function extend($appId = null, $appSecret = null) { $params = array( 'grant_type' => 'fb_exchange_token', 'fb_exchange_token' => $this->accessToken, ); return static::requestAccessToken($params, $appId, $appSecret); } /** * Request an access token based on a set of params. * * @param array $params * @param string|null $appId * @param string|null $appSecret * * @return AccessToken * * @throws FacebookRequestException */ public static function requestAccessToken(array $params, $appId = null, $appSecret = null) { $response = static::request('/oauth/access_token', $params, $appId, $appSecret); $data = $response->getResponse(); /** * @TODO fix this malarkey - getResponse() should always return an object * @see https://github.com/facebook/facebook-php-sdk-v4/issues/36 */ if (is_array($data)) { if (isset($data['access_token'])) { $expiresAt = isset($data['expires']) ? time() + $data['expires'] : 0; return new static($data['access_token'], $expiresAt); } } elseif($data instanceof \stdClass) { if (isset($data->access_token)) { $expiresAt = isset($data->expires_in) ? time() + $data->expires_in : 0; $machineId = isset($data->machine_id) ? (string) $data->machine_id : null; return new static((string) $data->access_token, $expiresAt, $machineId); } } throw FacebookRequestException::create( $response->getRawResponse(), $data, 401 ); } /** * Request a code from a long lived access token. * * @param array $params * @param string|null $appId * @param string|null $appSecret * * @return string * * @throws FacebookRequestException */ public static function requestCode(array $params, $appId = null, $appSecret = null) { $response = static::request('/oauth/client_code', $params, $appId, $appSecret); $data = $response->getResponse(); if (isset($data->code)) { return (string) $data->code; } throw FacebookRequestException::create( $response->getRawResponse(), $data, 401 ); } /** * Send a request to Graph with an app access token. * * @param string $endpoint * @param array $params * @param string|null $appId * @param string|null $appSecret * * @return \Facebook\FacebookResponse * * @throws FacebookRequestException */ protected static function request($endpoint, array $params, $appId = null, $appSecret = null) { $targetAppId = FacebookSession::_getTargetAppId($appId); $targetAppSecret = FacebookSession::_getTargetAppSecret($appSecret); if (!isset($params['client_id'])) { $params['client_id'] = $targetAppId; } if (!isset($params['client_secret'])) { $params['client_secret'] = $targetAppSecret; } // The response for this endpoint is not JSON, so it must be handled // differently, not as a GraphObject. $request = new FacebookRequest( FacebookSession::newAppSession($targetAppId, $targetAppSecret), 'GET', $endpoint, $params ); return $request->execute(); } /** * Get more info about an access token. * * @param string|null $appId * @param string|null $appSecret * * @return GraphSessionInfo */ public function getInfo($appId = null, $appSecret = null) { $params = array('input_token' => $this->accessToken); $request = new FacebookRequest( FacebookSession::newAppSession($appId, $appSecret), 'GET', '/debug_token', $params ); $response = $request->execute()->getGraphObject(GraphSessionInfo::className()); // Update the data on this token if ($response->getExpiresAt()) { $this->expiresAt = $response->getExpiresAt(); } return $response; } /** * Returns the access token as a string. * * @return string */ public function __toString() { return $this->accessToken; } /** * Returns true if the access token is an app session token. * * @return bool */ public function isAppSession() { return strpos($this->accessToken, '|') !== false; } } |
Commits for Nextrek/Android/SmartCharging/endPoints/nightly/fb_SDK/src/Facebook/Entities/AccessToken.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. |