|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codelayer\LaravelShopifyIntegration\Lib; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Psr\Http\Client\ClientExceptionInterface; |
| 7 | +use Ramsey\Uuid\Uuid; |
| 8 | +use Shopify\Auth\AccessTokenResponse; |
| 9 | +use Shopify\Auth\OAuth; |
| 10 | +use Shopify\Auth\Session; |
| 11 | +use Shopify\Clients\Http; |
| 12 | +use Shopify\Context; |
| 13 | +use Shopify\Exception\HttpRequestException; |
| 14 | +use Shopify\Exception\SessionStorageException; |
| 15 | +use Shopify\Exception\UninitializedContextException; |
| 16 | +use Shopify\Utils; |
| 17 | + |
| 18 | +class ShopifyOAuth extends OAuth |
| 19 | +{ |
| 20 | + public static function authorizeFromRequest(Request $request): ?Session |
| 21 | + { |
| 22 | + $encodedSessionToken = self::getSessionTokenHeader($request) ?? self::getSessionTokenFromUrlParam($request); |
| 23 | + $decodedSessionToken = Utils::decodeSessionToken($encodedSessionToken); |
| 24 | + |
| 25 | + $dest = $decodedSessionToken['dest']; |
| 26 | + $shop = parse_url($dest, PHP_URL_HOST); |
| 27 | + |
| 28 | + $cleanShop = Utils::sanitizeShopDomain($shop); |
| 29 | + |
| 30 | + $session = Utils::loadOfflineSession($cleanShop); |
| 31 | + |
| 32 | + if (empty($session)) { |
| 33 | + $session = new Session( |
| 34 | + id: OAuth::getOfflineSessionId($cleanShop), |
| 35 | + shop: $cleanShop, |
| 36 | + isOnline: false, |
| 37 | + state: Uuid::uuid4()->toString() |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + $accessTokenResponse = ShopifyOAuth::exchangeToken( |
| 42 | + shop: $shop, |
| 43 | + sessionToken: $encodedSessionToken, |
| 44 | + requestedTokenType: 'urn:shopify:params:oauth:token-type:offline-access-token', |
| 45 | + ); |
| 46 | + |
| 47 | + $session->setAccessToken($accessTokenResponse->getAccessToken()); |
| 48 | + $session->setScope($accessTokenResponse->getScope()); |
| 49 | + |
| 50 | + $sessionStored = Context::$SESSION_STORAGE->storeSession($session); |
| 51 | + |
| 52 | + if (! $sessionStored) { |
| 53 | + throw new SessionStorageException( |
| 54 | + 'OAuth Session could not be saved. Please check your session storage functionality.' |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return $session; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * From https://github.com/Shopify/shopify-app-js/blob/ab752293284d344a5e3803271c25e4237e478565/packages/apps/shopify-api/lib/auth/oauth/token-exchange.ts#L27 |
| 63 | + * |
| 64 | + * @throws HttpRequestException |
| 65 | + * @throws \JsonException |
| 66 | + * @throws ClientExceptionInterface |
| 67 | + * @throws UninitializedContextException |
| 68 | + */ |
| 69 | + public static function exchangeToken(string $shop, string $sessionToken, string $requestedTokenType): AccessTokenResponse |
| 70 | + { |
| 71 | + Utils::decodeSessionToken($sessionToken); |
| 72 | + |
| 73 | + $body = [ |
| 74 | + 'client_id' => Context::$API_KEY, |
| 75 | + 'client_secret' => Context::$API_SECRET_KEY, |
| 76 | + 'grant_type' => 'urn:ietf:params:oauth:grant-type:token-exchange', |
| 77 | + 'subject_token' => $sessionToken, |
| 78 | + 'subject_token_type' => 'urn:ietf:params:oauth:token-type:id_token', |
| 79 | + 'requested_token_type' => $requestedTokenType, |
| 80 | + ]; |
| 81 | + |
| 82 | + $cleanShop = Utils::sanitizeShopDomain($shop); |
| 83 | + |
| 84 | + $client = new Http($cleanShop); |
| 85 | + $response = $client->post(path: '/admin/oauth/access_token', body: $body, headers: [ |
| 86 | + 'Content-Type' => 'application/json', |
| 87 | + 'Accept' => 'application/json', |
| 88 | + ]); |
| 89 | + |
| 90 | + if ($response->getStatusCode() !== 200) { |
| 91 | + throw new HttpRequestException("Failed to get access token: {$response->getDecodedBody()}"); |
| 92 | + } |
| 93 | + |
| 94 | + $responseBody = $response->getDecodedBody(); |
| 95 | + |
| 96 | + return new AccessTokenResponse( |
| 97 | + accessToken: $responseBody['access_token'], |
| 98 | + scope: $responseBody['scope'], |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + private static function getSessionTokenHeader(Request $request): ?string |
| 103 | + { |
| 104 | + $authorizationHeader = $request->header('authorization'); |
| 105 | + |
| 106 | + if (empty($authorizationHeader)) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + return str_replace('Bearer ', '', $authorizationHeader); |
| 111 | + } |
| 112 | + |
| 113 | + private static function getSessionTokenFromUrlParam(Request $request): ?string |
| 114 | + { |
| 115 | + return $request->get('id_token'); |
| 116 | + } |
| 117 | +} |
0 commit comments