1<?php 2/** 3 * Copyright 2017 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 */ 24namespace Facebook\Authentication; 25 26/** 27 * Class AccessToken 28 * 29 * @package Facebook 30 */ 31class AccessToken 32{ 33 /** 34 * The access token value. 35 * 36 * @var string 37 */ 38 protected $value = ''; 39 40 /** 41 * Date when token expires. 42 * 43 * @var \DateTime|null 44 */ 45 protected $expiresAt; 46 47 /** 48 * Create a new access token entity. 49 * 50 * @param string $accessToken 51 * @param int $expiresAt 52 */ 53 public function __construct($accessToken, $expiresAt = 0) 54 { 55 $this->value = $accessToken; 56 if ($expiresAt) { 57 $this->setExpiresAtFromTimeStamp($expiresAt); 58 } 59 } 60 61 /** 62 * Generate an app secret proof to sign a request to Graph. 63 * 64 * @param string $appSecret The app secret. 65 * 66 * @return string 67 */ 68 public function getAppSecretProof($appSecret) 69 { 70 return hash_hmac('sha256', $this->value, $appSecret); 71 } 72 73 /** 74 * Getter for expiresAt. 75 * 76 * @return \DateTime|null 77 */ 78 public function getExpiresAt() 79 { 80 return $this->expiresAt; 81 } 82 83 /** 84 * Determines whether or not this is an app access token. 85 * 86 * @return bool 87 */ 88 public function isAppAccessToken() 89 { 90 return strpos($this->value, '|') !== false; 91 } 92 93 /** 94 * Determines whether or not this is a long-lived token. 95 * 96 * @return bool 97 */ 98 public function isLongLived() 99 { 100 if ($this->expiresAt) { 101 return $this->expiresAt->getTimestamp() > time() + (60 * 60 * 2); 102 } 103 104 if ($this->isAppAccessToken()) { 105 return true; 106 } 107 108 return false; 109 } 110 111 /** 112 * Checks the expiration of the access token. 113 * 114 * @return boolean|null 115 */ 116 public function isExpired() 117 { 118 if ($this->getExpiresAt() instanceof \DateTime) { 119 return $this->getExpiresAt()->getTimestamp() < time(); 120 } 121 122 if ($this->isAppAccessToken()) { 123 return false; 124 } 125 126 return null; 127 } 128 129 /** 130 * Returns the access token as a string. 131 * 132 * @return string 133 */ 134 public function getValue() 135 { 136 return $this->value; 137 } 138 139 /** 140 * Returns the access token as a string. 141 * 142 * @return string 143 */ 144 public function __toString() 145 { 146 return $this->getValue(); 147 } 148 149 /** 150 * Setter for expires_at. 151 * 152 * @param int $timeStamp 153 */ 154 protected function setExpiresAtFromTimeStamp($timeStamp) 155 { 156 $dt = new \DateTime(); 157 $dt->setTimestamp($timeStamp); 158 $this->expiresAt = $dt; 159 } 160} 161