Skip to content

Commit 12900fb

Browse files
committed
Ignore scope mismatches when loading offline session for shop
1 parent 61fc7b6 commit 12900fb

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Lib/ShopifySessionHandler.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace Codelayer\LaravelShopifyIntegration\Lib;
44

5+
use DateTime;
56
use Exception;
7+
use Shopify\Auth\OAuth;
68
use Shopify\Auth\Session;
7-
use Shopify\Utils;
9+
use Shopify\Context;
810

911
class ShopifySessionHandler
1012
{
1113
public function getSessionForShopOrThrow(string $shop): Session
1214
{
13-
$session = Utils::loadOfflineSession($shop);
15+
$session = $this->loadOfflineSession($shop);
1416

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

2123
return $session;
2224
}
25+
26+
public function sessionIsValid(Session $session): bool
27+
{
28+
return $session->getAccessToken() &&
29+
(! $session->getExpires() || ($session->getExpires() > new DateTime()));
30+
}
31+
32+
private function loadOfflineSession(string $shop, bool $includeExpired = false): ?Session
33+
{
34+
Context::throwIfUninitialized();
35+
36+
$sessionId = OAuth::getOfflineSessionId($shop);
37+
$session = Context::$SESSION_STORAGE->loadSession($sessionId);
38+
39+
if ($session && ! $includeExpired && ! $this->sessionIsValid($session)) {
40+
return null;
41+
}
42+
43+
return $session;
44+
}
2345
}

0 commit comments

Comments
 (0)