Cheetah
AccessTokenMetadata.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\Authentication;
25 
27 
37 {
43  protected $metadata = [];
44 
50  protected static $dateProperties = ['expires_at', 'issued_at'];
51 
57  public function __construct(array $metadata)
58  {
59  if (!isset($metadata['data'])) {
60  throw new FacebookSDKException('Unexpected debug token response data.', 401);
61  }
62 
63  $this->metadata = $metadata['data'];
64 
65  $this->castTimestampsToDateTime();
66  }
67 
76  public function getField($field, $default = null)
77  {
78  if (isset($this->metadata[$field])) {
79  return $this->metadata[$field];
80  }
81 
82  return $default;
83  }
84 
96  public function getProperty($field, $default = null)
97  {
98  return $this->getField($field, $default);
99  }
100 
110  public function getChildProperty($parentField, $field, $default = null)
111  {
112  if (!isset($this->metadata[$parentField])) {
113  return $default;
114  }
115 
116  if (!isset($this->metadata[$parentField][$field])) {
117  return $default;
118  }
119 
120  return $this->metadata[$parentField][$field];
121  }
122 
131  public function getErrorProperty($field, $default = null)
132  {
133  return $this->getChildProperty('error', $field, $default);
134  }
135 
144  public function getMetadataProperty($field, $default = null)
145  {
146  return $this->getChildProperty('metadata', $field, $default);
147  }
148 
154  public function getAppId()
155  {
156  return $this->getField('app_id');
157  }
158 
164  public function getApplication()
165  {
166  return $this->getField('application');
167  }
168 
175  public function isError()
176  {
177  return $this->getField('error') !== null;
178  }
179 
185  public function getErrorCode()
186  {
187  return $this->getErrorProperty('code');
188  }
189 
195  public function getErrorMessage()
196  {
197  return $this->getErrorProperty('message');
198  }
199 
205  public function getErrorSubcode()
206  {
207  return $this->getErrorProperty('subcode');
208  }
209 
215  public function getExpiresAt()
216  {
217  return $this->getField('expires_at');
218  }
219 
225  public function getIsValid()
226  {
227  return $this->getField('is_valid');
228  }
229 
240  public function getIssuedAt()
241  {
242  return $this->getField('issued_at');
243  }
244 
251  public function getMetadata()
252  {
253  return $this->getField('metadata');
254  }
255 
261  public function getSso()
262  {
263  return $this->getMetadataProperty('sso');
264  }
265 
271  public function getAuthType()
272  {
273  return $this->getMetadataProperty('auth_type');
274  }
275 
281  public function getAuthNonce()
282  {
283  return $this->getMetadataProperty('auth_nonce');
284  }
285 
292  public function getProfileId()
293  {
294  return $this->getField('profile_id');
295  }
296 
303  public function getScopes()
304  {
305  return $this->getField('scopes');
306  }
307 
313  public function getUserId()
314  {
315  return $this->getField('user_id');
316  }
317 
326  public function validateAppId($appId)
327  {
328  if ($this->getAppId() !== $appId) {
329  throw new FacebookSDKException('Access token metadata contains unexpected app ID.', 401);
330  }
331  }
332 
341  public function validateUserId($userId)
342  {
343  if ($this->getUserId() !== $userId) {
344  throw new FacebookSDKException('Access token metadata contains unexpected user ID.', 401);
345  }
346  }
347 
353  public function validateExpiration()
354  {
355  if (!$this->getExpiresAt() instanceof \DateTime) {
356  return;
357  }
358 
359  if ($this->getExpiresAt()->getTimestamp() < time()) {
360  throw new FacebookSDKException('Inspection of access token metadata shows that the access token has expired.', 401);
361  }
362  }
363 
371  private function convertTimestampToDateTime($timestamp)
372  {
373  $dt = new \DateTime();
374  $dt->setTimestamp($timestamp);
375 
376  return $dt;
377  }
378 
382  private function castTimestampsToDateTime()
383  {
384  foreach (static::$dateProperties as $key) {
385  if (isset($this->metadata[$key]) && $this->metadata[$key] !== 0) {
386  $this->metadata[$key] = $this->convertTimestampToDateTime($this->metadata[$key]);
387  }
388  }
389  }
390 }
Facebook\Authentication\AccessTokenMetadata\getMetadataProperty
getMetadataProperty($field, $default=null)
Definition: AccessTokenMetadata.php:144
Facebook\Authentication\AccessTokenMetadata\__construct
__construct(array $metadata)
Definition: AccessTokenMetadata.php:57
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\Authentication\AccessTokenMetadata\getScopes
getScopes()
Definition: AccessTokenMetadata.php:303
Facebook\Authentication\AccessTokenMetadata\getExpiresAt
getExpiresAt()
Definition: AccessTokenMetadata.php:215
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
Facebook\Authentication\AccessTokenMetadata\getUserId
getUserId()
Definition: AccessTokenMetadata.php:313
Facebook\Authentication\AccessTokenMetadata\getProfileId
getProfileId()
Definition: AccessTokenMetadata.php:292
php
Facebook\Authentication\AccessTokenMetadata\getProperty
getProperty($field, $default=null)
Definition: AccessTokenMetadata.php:96
Facebook\Authentication
Definition: AccessToken.php:24
Facebook\Authentication\AccessTokenMetadata\getSso
getSso()
Definition: AccessTokenMetadata.php:261
Facebook\Authentication\AccessTokenMetadata\getChildProperty
getChildProperty($parentField, $field, $default=null)
Definition: AccessTokenMetadata.php:110
Facebook\Authentication\AccessTokenMetadata\getAppId
getAppId()
Definition: AccessTokenMetadata.php:154
Facebook\Authentication\AccessTokenMetadata\getApplication
getApplication()
Definition: AccessTokenMetadata.php:164
Facebook\Authentication\AccessTokenMetadata\isError
isError()
Definition: AccessTokenMetadata.php:175
Facebook\Authentication\AccessTokenMetadata\getErrorCode
getErrorCode()
Definition: AccessTokenMetadata.php:185
Facebook\Authentication\AccessTokenMetadata\validateExpiration
validateExpiration()
Definition: AccessTokenMetadata.php:353
Facebook\Authentication\AccessTokenMetadata\getErrorMessage
getErrorMessage()
Definition: AccessTokenMetadata.php:195
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\Authentication\AccessTokenMetadata\validateUserId
validateUserId($userId)
Definition: AccessTokenMetadata.php:341
Facebook\Authentication\AccessTokenMetadata
Definition: AccessTokenMetadata.php:37
Facebook\Authentication\AccessTokenMetadata\getAuthType
getAuthType()
Definition: AccessTokenMetadata.php:271
Facebook\Authentication\AccessTokenMetadata\$metadata
$metadata
Definition: AccessTokenMetadata.php:43
Facebook\Authentication\AccessTokenMetadata\getIsValid
getIsValid()
Definition: AccessTokenMetadata.php:225
Facebook\Authentication\AccessTokenMetadata\$dateProperties
static $dateProperties
Definition: AccessTokenMetadata.php:50
Facebook\Authentication\AccessTokenMetadata\getField
getField($field, $default=null)
Definition: AccessTokenMetadata.php:76
Facebook\Authentication\AccessTokenMetadata\getAuthNonce
getAuthNonce()
Definition: AccessTokenMetadata.php:281
Facebook\Authentication\AccessTokenMetadata\getIssuedAt
getIssuedAt()
Definition: AccessTokenMetadata.php:240
Facebook\Authentication\AccessTokenMetadata\getMetadata
getMetadata()
Definition: AccessTokenMetadata.php:251
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\Authentication\AccessTokenMetadata\getErrorSubcode
getErrorSubcode()
Definition: AccessTokenMetadata.php:205
Facebook\Authentication\AccessTokenMetadata\validateAppId
validateAppId($appId)
Definition: AccessTokenMetadata.php:326
Facebook\Authentication\AccessTokenMetadata\getErrorProperty
getErrorProperty($field, $default=null)
Definition: AccessTokenMetadata.php:131