PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Application/Library/facebook_full/src/Facebook/Entities/AccessToken.php

https://gitlab.com/hoanghung.dev/aloads
PHP | 379 lines | 164 code | 39 blank | 176 comment | 16 complexity | c9cebb528b53a2c3ca6f5e530af98b89 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2014 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook\Entities;
  25. use Facebook\FacebookRequest;
  26. use Facebook\FacebookRequestException;
  27. use Facebook\FacebookSession;
  28. use Facebook\GraphSessionInfo;
  29. /**
  30. * Class AccessToken
  31. * @package Facebook
  32. */
  33. class AccessToken
  34. {
  35. /**
  36. * The access token.
  37. *
  38. * @var string
  39. */
  40. protected $accessToken;
  41. /**
  42. * A unique ID to identify a client.
  43. *
  44. * @var string
  45. */
  46. protected $machineId;
  47. /**
  48. * Date when token expires.
  49. *
  50. * @var \DateTime|null
  51. */
  52. protected $expiresAt;
  53. /**
  54. * Create a new access token entity.
  55. *
  56. * @param string $accessToken
  57. * @param int $expiresAt
  58. * @param string|null machineId
  59. */
  60. public function __construct($accessToken, $expiresAt = 0, $machineId = null)
  61. {
  62. $this->accessToken = $accessToken;
  63. if ($expiresAt) {
  64. $this->setExpiresAtFromTimeStamp($expiresAt);
  65. }
  66. $this->machineId = $machineId;
  67. }
  68. /**
  69. * Setter for expires_at.
  70. *
  71. * @param int $timeStamp
  72. */
  73. protected function setExpiresAtFromTimeStamp($timeStamp)
  74. {
  75. $dt = new \DateTime();
  76. $dt->setTimestamp($timeStamp);
  77. $this->expiresAt = $dt;
  78. }
  79. /**
  80. * Getter for expiresAt.
  81. *
  82. * @return \DateTime|null
  83. */
  84. public function getExpiresAt()
  85. {
  86. return $this->expiresAt;
  87. }
  88. /**
  89. * Getter for machineId.
  90. *
  91. * @return string|null
  92. */
  93. public function getMachineId()
  94. {
  95. return $this->machineId;
  96. }
  97. /**
  98. * Determines whether or not this is a long-lived token.
  99. *
  100. * @return bool
  101. */
  102. public function isLongLived()
  103. {
  104. if ($this->expiresAt) {
  105. return $this->expiresAt->getTimestamp() > time() + (60 * 60 * 2);
  106. }
  107. return false;
  108. }
  109. /**
  110. * Checks the validity of the access token.
  111. *
  112. * @param string|null $appId Application ID to use
  113. * @param string|null $appSecret App secret value to use
  114. * @param string|null $machineId
  115. *
  116. * @return boolean
  117. */
  118. public function isValid($appId = null, $appSecret = null, $machineId = null)
  119. {
  120. $accessTokenInfo = $this->getInfo($appId, $appSecret);
  121. $machineId = $machineId ?: $this->machineId;
  122. return static::validateAccessToken($accessTokenInfo, $appId, $machineId);
  123. }
  124. /**
  125. * Ensures the provided GraphSessionInfo object is valid,
  126. * throwing an exception if not. Ensures the appId matches,
  127. * that the machineId matches if it's being used,
  128. * that the token is valid and has not expired.
  129. *
  130. * @param GraphSessionInfo $tokenInfo
  131. * @param string|null $appId Application ID to use
  132. * @param string|null $machineId
  133. *
  134. * @return boolean
  135. */
  136. public static function validateAccessToken(GraphSessionInfo $tokenInfo,
  137. $appId = null, $machineId = null)
  138. {
  139. $targetAppId = FacebookSession::_getTargetAppId($appId);
  140. $appIdIsValid = $tokenInfo->getAppId() == $targetAppId;
  141. $machineIdIsValid = $tokenInfo->getProperty('machine_id') == $machineId;
  142. $accessTokenIsValid = $tokenInfo->isValid();
  143. $accessTokenIsStillAlive = true;
  144. // Not all access tokens return an expiration. E.g. an app access token.
  145. if ($tokenInfo->getExpiresAt() instanceof \DateTime) {
  146. $accessTokenIsStillAlive = $tokenInfo->getExpiresAt()->getTimestamp() >= time();
  147. }
  148. return $appIdIsValid && $machineIdIsValid && $accessTokenIsValid && $accessTokenIsStillAlive;
  149. }
  150. /**
  151. * Get a valid access token from a code.
  152. *
  153. * @param string $code
  154. * @param string|null $appId
  155. * @param string|null $appSecret
  156. * @param string|null $machineId
  157. *
  158. * @return AccessToken
  159. */
  160. public static function getAccessTokenFromCode($code, $appId = null, $appSecret = null, $machineId = null)
  161. {
  162. $params = array(
  163. 'code' => $code,
  164. 'redirect_uri' => '',
  165. );
  166. if ($machineId) {
  167. $params['machine_id'] = $machineId;
  168. }
  169. return static::requestAccessToken($params, $appId, $appSecret);
  170. }
  171. /**
  172. * Get a valid code from an access token.
  173. *
  174. * @param AccessToken|string $accessToken
  175. * @param string|null $appId
  176. * @param string|null $appSecret
  177. *
  178. * @return AccessToken
  179. */
  180. public static function getCodeFromAccessToken($accessToken, $appId = null, $appSecret = null)
  181. {
  182. $accessToken = (string) $accessToken;
  183. $params = array(
  184. 'access_token' => $accessToken,
  185. 'redirect_uri' => '',
  186. );
  187. return static::requestCode($params, $appId, $appSecret);
  188. }
  189. /**
  190. * Exchanges a short lived access token with a long lived access token.
  191. *
  192. * @param string|null $appId
  193. * @param string|null $appSecret
  194. *
  195. * @return AccessToken
  196. */
  197. public function extend($appId = null, $appSecret = null)
  198. {
  199. $params = array(
  200. 'grant_type' => 'fb_exchange_token',
  201. 'fb_exchange_token' => $this->accessToken,
  202. );
  203. return static::requestAccessToken($params, $appId, $appSecret);
  204. }
  205. /**
  206. * Request an access token based on a set of params.
  207. *
  208. * @param array $params
  209. * @param string|null $appId
  210. * @param string|null $appSecret
  211. *
  212. * @return AccessToken
  213. *
  214. * @throws FacebookRequestException
  215. */
  216. public static function requestAccessToken(array $params, $appId = null, $appSecret = null)
  217. {
  218. $response = static::request('/oauth/access_token', $params, $appId, $appSecret);
  219. $data = $response->getResponse();
  220. /**
  221. * @TODO fix this malarkey - getResponse() should always return an object
  222. * @see https://github.com/facebook/facebook-php-sdk-v4/issues/36
  223. */
  224. if (is_array($data)) {
  225. if (isset($data['access_token'])) {
  226. $expiresAt = isset($data['expires']) ? time() + $data['expires'] : 0;
  227. return new static($data['access_token'], $expiresAt);
  228. }
  229. } elseif($data instanceof \stdClass) {
  230. if (isset($data->access_token)) {
  231. $expiresAt = isset($data->expires_in) ? time() + $data->expires_in : 0;
  232. $machineId = isset($data->machine_id) ? (string) $data->machine_id : null;
  233. return new static((string) $data->access_token, $expiresAt, $machineId);
  234. }
  235. }
  236. throw FacebookRequestException::create(
  237. $response->getRawResponse(),
  238. $data,
  239. 401
  240. );
  241. }
  242. /**
  243. * Request a code from a long lived access token.
  244. *
  245. * @param array $params
  246. * @param string|null $appId
  247. * @param string|null $appSecret
  248. *
  249. * @return string
  250. *
  251. * @throws FacebookRequestException
  252. */
  253. public static function requestCode(array $params, $appId = null, $appSecret = null)
  254. {
  255. $response = static::request('/oauth/client_code', $params, $appId, $appSecret);
  256. $data = $response->getResponse();
  257. if (isset($data->code)) {
  258. return (string) $data->code;
  259. }
  260. throw FacebookRequestException::create(
  261. $response->getRawResponse(),
  262. $data,
  263. 401
  264. );
  265. }
  266. /**
  267. * Send a request to Graph with an app access token.
  268. *
  269. * @param string $endpoint
  270. * @param array $params
  271. * @param string|null $appId
  272. * @param string|null $appSecret
  273. *
  274. * @return \Facebook\FacebookResponse
  275. *
  276. * @throws FacebookRequestException
  277. */
  278. protected static function request($endpoint, array $params, $appId = null, $appSecret = null)
  279. {
  280. $targetAppId = FacebookSession::_getTargetAppId($appId);
  281. $targetAppSecret = FacebookSession::_getTargetAppSecret($appSecret);
  282. if (!isset($params['client_id'])) {
  283. $params['client_id'] = $targetAppId;
  284. }
  285. if (!isset($params['client_secret'])) {
  286. $params['client_secret'] = $targetAppSecret;
  287. }
  288. // The response for this endpoint is not JSON, so it must be handled
  289. // differently, not as a GraphObject.
  290. $request = new FacebookRequest(
  291. FacebookSession::newAppSession($targetAppId, $targetAppSecret),
  292. 'GET',
  293. $endpoint,
  294. $params
  295. );
  296. return $request->execute();
  297. }
  298. /**
  299. * Get more info about an access token.
  300. *
  301. * @param string|null $appId
  302. * @param string|null $appSecret
  303. *
  304. * @return GraphSessionInfo
  305. */
  306. public function getInfo($appId = null, $appSecret = null)
  307. {
  308. $params = array('input_token' => $this->accessToken);
  309. $request = new FacebookRequest(
  310. FacebookSession::newAppSession($appId, $appSecret),
  311. 'GET',
  312. '/debug_token',
  313. $params
  314. );
  315. $response = $request->execute()->getGraphObject(GraphSessionInfo::className());
  316. // Update the data on this token
  317. if ($response->getExpiresAt()) {
  318. $this->expiresAt = $response->getExpiresAt();
  319. }
  320. return $response;
  321. }
  322. /**
  323. * Returns the access token as a string.
  324. *
  325. * @return string
  326. */
  327. public function __toString()
  328. {
  329. return $this->accessToken;
  330. }
  331. /**
  332. * Returns true if the access token is an app session token.
  333. *
  334. * @return bool
  335. */
  336. public function isAppSession()
  337. {
  338. return strpos($this->accessToken, '|') !== false;
  339. }
  340. }