Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue "Get Current Session ID process from Post Purchase Extension Token" #279

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
- PATCH: Fix the issue "Get Current Session ID process from Post Purchase Extension Token"

## v5.0.0 - 2023-05-10

Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "shopify/shopify-api",
"name": "jonaswebdev/shopify-api",
jonaswebdev marked this conversation as resolved.
Show resolved Hide resolved
"description": "Shopify API Library for PHP",
"license": "MIT",
"type": "library",
Expand All @@ -18,6 +18,10 @@
{
"name": "Shopify Inc.",
"email": "[email protected]"
},
{
"name": "Jonas Rosado",
jonaswebdev marked this conversation as resolved.
Show resolved Hide resolved
"email": "[email protected]"
}
],
"require": {
Expand Down
11 changes: 10 additions & 1 deletion src/Auth/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Shopify\Exception\CookieSetException;
use Shopify\Exception\HttpRequestException;
use Shopify\Exception\InvalidArgumentException;
use Shopify\Exception\InvalidJwtPayloadException;
use Shopify\Exception\InvalidOAuthException;
use Shopify\Exception\MissingArgumentException;
use Shopify\Exception\OAuthSessionNotFoundException;
Expand Down Expand Up @@ -232,7 +233,15 @@ public static function getCurrentSessionId(array $rawHeaders, array $cookies, bo
}

$jwtPayload = Utils::decodeSessionToken($matches[1]);
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['dest']);

if (!empty($jwtPayload['dest'])) {
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['dest']);
} elseif (!empty($jwtPayload['input_data']->shop->domain)) {
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['input_data']->shop->domain);
} else {
throw new InvalidJwtPayloadException('Missing shop value in JWT payload');
}

if ($isOnline) {
$currentSessionId = self::getJwtSessionId($shop, $jwtPayload['sub']);
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/InvalidJwtPayloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Shopify\Exception;

class InvalidJwtPayloadException extends \Exception
{
}
57 changes: 57 additions & 0 deletions tests/Auth/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopify\Exception\CookieSetException;
use Shopify\Exception\HttpRequestException;
use Shopify\Exception\InvalidArgumentException;
use Shopify\Exception\InvalidJwtPayloadException;
use Shopify\Exception\InvalidOAuthException;
use Shopify\Exception\MissingArgumentException;
use Shopify\Exception\OAuthSessionNotFoundException;
Expand Down Expand Up @@ -672,4 +673,60 @@ private function encodeJwtPayload(): string
];
return JWT::encode($payload, Context::$API_SECRET_KEY, 'HS256');
}


private function encodeJwtPayloadFromPostPurchaseExtension(): string
{
$shop = new stdClass();
$shop->domain = "https://exampleshop.myshopify.com";
$inputData = new stdClass();
$inputData->shop = $shop;

$payload = [
"iss" => "https://exampleshop.myshopify.com/admin",
"sub" => "42",
"input_data" => $inputData,
"iat" => 1591764998,
];

return JWT::encode($payload, Context::$API_SECRET_KEY, 'HS256');
}

private function encodeInvalidJwtPayloadFromPostPurchaseExtension(): string
{
$payload = [
"iss" => "https://exampleshop.myshopify.com/admin",
"sub" => "42",
"iat" => 1591764998,
];

return JWT::encode($payload, Context::$API_SECRET_KEY, 'HS256');
}


public function testGetCurrentSessionIdFromPostPurchaseTokenForOnlineShop()
{
$token = $this->encodeJwtPayloadFromPostPurchaseExtension();

$currentSessionId = OAuth::getCurrentSessionId(['Authorization' => "Bearer $token"], [], true);
$this->assertEquals('exampleshop.myshopify.com_42', $currentSessionId);
}

public function testGetCurrentSessionIdFromPostPurchaseTokenForOfflineShop()
{
$token = $this->encodeJwtPayloadFromPostPurchaseExtension();

$currentSessionId = OAuth::getCurrentSessionId(['Authorization' => "Bearer $token"], [], false);
$this->assertEquals('offline_exampleshop.myshopify.com', $currentSessionId);
}

public function testGetCurrentSessionIdFromPostPurchaseTokenInvalidJwtPayloadException()
{
$token = $this->encodeInvalidJwtPayloadFromPostPurchaseExtension();

$this->expectException(InvalidJwtPayloadException::class);
$this->expectExceptionMessage('Missing shop value in JWT payload');
$currentSessionId = OAuth::getCurrentSessionId(['Authorization' => "Bearer $token"], [], true);
}

}