From 0f33ba7e8c029e7996f6d2ca1617c22c02f3eba1 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 3 Feb 2025 15:40:24 -0500 Subject: [PATCH 1/3] Add api key finder class and related configurations --- src/ApiKeyFinder/CurrentApiKeyFinder.php | 28 +++++++++++++++++++ .../CurrentApiKeyFinderInterface.php | 8 ++++++ src/Util.php | 28 ++++++++++++------- src/resources/config/shopify-app.php | 24 ++++++++++++++++ 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 src/ApiKeyFinder/CurrentApiKeyFinder.php create mode 100644 src/Contracts/CurrentApiKeyFinderInterface.php diff --git a/src/ApiKeyFinder/CurrentApiKeyFinder.php b/src/ApiKeyFinder/CurrentApiKeyFinder.php new file mode 100644 index 00000000..ef01917c --- /dev/null +++ b/src/ApiKeyFinder/CurrentApiKeyFinder.php @@ -0,0 +1,28 @@ +toNative() : $shop; + $shopDomain = preg_replace('/[^A-Z0-9]/', '', strtoupper(explode('.', $shopDomain)[0])); + + // Try to get env defined for shop, fallback to config value + return env( + strtoupper($key) . "_" . $shopDomain, + Config::get($fullKey) + ); + } +} diff --git a/src/Contracts/CurrentApiKeyFinderInterface.php b/src/Contracts/CurrentApiKeyFinderInterface.php new file mode 100644 index 00000000..e43df18e --- /dev/null +++ b/src/Contracts/CurrentApiKeyFinderInterface.php @@ -0,0 +1,8 @@ + env('SCRIPTTAGS_JOB_CONNECTION', null), 'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_CONNECTION', null), ], + /* |-------------------------------------------------------------------------- | Config API Callback @@ -487,11 +488,34 @@ | A closure/callable is required. | The first argument will be the key string. | The second argument will be something to help identify the shop. + | + | This will break caching config values because Closures can not be serialized. + | Use config_api_class below instead. | */ 'config_api_callback' => null, + /* + |-------------------------------------------------------------------------- + | Config API Resolver Class + |-------------------------------------------------------------------------- + | + | This option can be used to modify what returns when `getConfig('api_*')` + | is used. A use-case for this is modifying the return of `api_secret` + | or something similar. + | + | A class name is required + | The class must implement Osiset\ShopifyApp\Contracts\CurrentApiKeyFinderInterface + | A default class is provided and can be uncommented below. + | + | config_api_callback will take priority for backwards-compatibility however, + | this option is the recommended way because closures can not be serialized. + | + */ + + // 'config_api_class' => Osiset\ShopifyApp\ApiKeyFinder\CurrentApiKeyFinder::class, + /* |-------------------------------------------------------------------------- | Customize Models and Table Name From dc0a68f6ad5e7d4150f8b9009095187b72e1d080 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 3 Feb 2025 16:09:25 -0500 Subject: [PATCH 2/3] Fix handing NullableShopDomain --- src/ApiKeyFinder/CurrentApiKeyFinder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ApiKeyFinder/CurrentApiKeyFinder.php b/src/ApiKeyFinder/CurrentApiKeyFinder.php index ef01917c..79dba3d7 100644 --- a/src/ApiKeyFinder/CurrentApiKeyFinder.php +++ b/src/ApiKeyFinder/CurrentApiKeyFinder.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Config; use Osiset\ShopifyApp\Contracts\CurrentApiKeyFinderInterface; +use Osiset\ShopifyApp\Objects\Values\NullableShopDomain; class CurrentApiKeyFinder implements CurrentApiKeyFinderInterface { @@ -16,7 +17,7 @@ public static function resolve(string $key, $shop = null): ?string } // Clean the shop domain - $shopDomain = $shop instanceof ShopDomainValue ? $shop->toNative() : $shop; + $shopDomain = $shop instanceof NullableShopDomain ? $shop->toNative() : $shop; $shopDomain = preg_replace('/[^A-Z0-9]/', '', strtoupper(explode('.', $shopDomain)[0])); // Try to get env defined for shop, fallback to config value From a3d8cdcc55fe88d1852d7e390ef3c192e39b6d50 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 4 Feb 2025 13:22:27 -0500 Subject: [PATCH 3/3] Update resolver to work with cached env --- src/ApiKeyFinder/CurrentApiKeyFinder.php | 23 ++++++++++++++--------- src/resources/config/shopify-app.php | 3 +++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/ApiKeyFinder/CurrentApiKeyFinder.php b/src/ApiKeyFinder/CurrentApiKeyFinder.php index 79dba3d7..cdab7f6e 100644 --- a/src/ApiKeyFinder/CurrentApiKeyFinder.php +++ b/src/ApiKeyFinder/CurrentApiKeyFinder.php @@ -8,22 +8,27 @@ class CurrentApiKeyFinder implements CurrentApiKeyFinderInterface { + private const SUPPORTED_KEYS = [ + 'api_key', + 'api_secret' + ]; + public static function resolve(string $key, $shop = null): ?string { $fullKey = "shopify-app.{$key}"; - if (! $shop) { + if (! $shop || ! in_array($key, self::SUPPORTED_KEYS)) { // No shop passed, return default return Config::get($fullKey); } - // Clean the shop domain $shopDomain = $shop instanceof NullableShopDomain ? $shop->toNative() : $shop; - $shopDomain = preg_replace('/[^A-Z0-9]/', '', strtoupper(explode('.', $shopDomain)[0])); + $shopDomain = explode('.', $shopDomain)[0]; + $searchKey = "shopify-app.config_api_shop_keys.{$key}_{$shopDomain}"; + + if (! Config::has($searchKey)) { + return Config::get($fullKey); + } - // Try to get env defined for shop, fallback to config value - return env( - strtoupper($key) . "_" . $shopDomain, - Config::get($fullKey) - ); + return Config::get($searchKey); } -} +} \ No newline at end of file diff --git a/src/resources/config/shopify-app.php b/src/resources/config/shopify-app.php index c5a53e0f..ac78fa55 100644 --- a/src/resources/config/shopify-app.php +++ b/src/resources/config/shopify-app.php @@ -515,6 +515,9 @@ */ // 'config_api_class' => Osiset\ShopifyApp\ApiKeyFinder\CurrentApiKeyFinder::class, + // 'config_api_shop_keys' => [ + // 'api_key_shop-name' => env('API_KEY_SHOPNAME', ''), + // ], /* |--------------------------------------------------------------------------