Skip to content

Commit

Permalink
Ignore scope mismatches when loading offline session for shop
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdierich committed Jul 18, 2024
1 parent 61fc7b6 commit 12900fb
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Lib/ShopifySessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Codelayer\LaravelShopifyIntegration\Lib;

use DateTime;
use Exception;
use Shopify\Auth\OAuth;
use Shopify\Auth\Session;
use Shopify\Utils;
use Shopify\Context;

class ShopifySessionHandler
{
public function getSessionForShopOrThrow(string $shop): Session
{
$session = Utils::loadOfflineSession($shop);
$session = $this->loadOfflineSession($shop);

if ($session === null) {
throw new Exception(
Expand All @@ -20,4 +22,24 @@ public function getSessionForShopOrThrow(string $shop): Session

return $session;
}

public function sessionIsValid(Session $session): bool
{
return $session->getAccessToken() &&
(! $session->getExpires() || ($session->getExpires() > new DateTime()));
}

private function loadOfflineSession(string $shop, bool $includeExpired = false): ?Session
{
Context::throwIfUninitialized();

$sessionId = OAuth::getOfflineSessionId($shop);
$session = Context::$SESSION_STORAGE->loadSession($sessionId);

if ($session && ! $includeExpired && ! $this->sessionIsValid($session)) {
return null;
}

return $session;
}
}

0 comments on commit 12900fb

Please sign in to comment.