Skip to content

Commit

Permalink
Merge pull request #109 from pixelant/105_token_expires
Browse files Browse the repository at this point in the history
[BUGFIX] Add check for if long lived Facebook token is ''expired''
  • Loading branch information
MattiasNilsson authored Jan 12, 2022
2 parents e1cec7d + c974cda commit 3f53936
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
45 changes: 18 additions & 27 deletions Classes/Domain/Model/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use League\OAuth2\Client\Provider\Exception\FacebookProviderException;
use League\OAuth2\Client\Provider\Facebook;
use League\OAuth2\Client\Token\AccessToken;
use Pixelant\PxaSocialFeed\Feed\Source\FacebookSource;
Expand Down Expand Up @@ -284,15 +285,15 @@ public function isValidFacebookAccessToken(): bool
if (empty($this->accessToken)) {
$isValid = false;
} else {
$token = new AccessToken([
'access_token' => $this->getAccessToken()
]);
$hasExpired = $this->getFb(
$this->getAppId(),
$this->getAppSecret()
)->getLongLivedAccessToken($token)->hasExpired();

if ($hasExpired === true) {
try {
$token = new AccessToken([
'access_token' => $this->getAccessToken(),
]);
$this->getFb(
$this->getAppId(),
$this->getAppSecret()
)->getLongLivedAccessToken($token);
} catch (FacebookProviderException $exception) {
$isValid = false;
}
}
Expand Down Expand Up @@ -327,24 +328,14 @@ public function getFacebookAccessTokenValidPeriod(string $format = '%R%a'): stri
*/
public function getFacebookAccessTokenMetadataExpirationDate(): ?\DateTime
{
$expireAt = new \DateTime();
$token = new AccessToken([
'access_token' => $this->getAccessToken(),
'expires' => $expireAt->getTimestamp()
]);

$accessToken = $this->getFb($this->getAppId(), $this->getAppSecret())->getLongLivedAccessToken($token);
$expireAt->setTimestamp($accessToken->getExpires());

if ($expireAt === 0) {
$dataAccessExpiresAt = 1734694626000;
if ($dataAccessExpiresAt > 0) {
try {
return (new \DateTime())->setTimestamp($dataAccessExpiresAt);
} catch (\Exception $exception) {
return null;
}
}
try {
$expireAt = new \DateTime('+60 days');
$token = new AccessToken([
'access_token' => $this->getAccessToken()
]);
$this->getFb($this->getAppId(), $this->getAppSecret())->getLongLivedAccessToken($token);
} catch (FacebookProviderException $exception) {
return null;
}

return $expireAt;
Expand Down
11 changes: 11 additions & 0 deletions Classes/ViewHelpers/FacebookLoginUrlViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public static function renderStatic(
static::removeVariables($variableProvider, $loginUrlAs, $redirectUrlAs);

$variableProvider->add($redirectUrlAs, $redirectUrl);

if (strpos($url, 'redirect_uri=&') !== false) {
$urlStructure = explode('redirect_uri=&', $url);
$url = sprintf(
'%sredirect_uri=%s&%s',
$urlStructure[0],
urlencode($redirectUrl),
$urlStructure[1]
);
}

$variableProvider->add($loginUrlAs, $url);
$content = $renderChildrenClosure();

Expand Down

0 comments on commit 3f53936

Please sign in to comment.