Skip to content

Add an option to return the auth request url #14

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ public static function verifyShopifyRequest()
*
* @param string|string[] $scopes Scopes required by app
* @param string $redirectUrl
* @param bool $autoRedirect
*
* @throws SdkException if required configuration is not provided in $config
*
* @return void
* @return string
*/
public static function createAuthRequest($scopes, $redirectUrl = null)
public static function createAuthRequest($scopes, $redirectUrl = null, $autoRedirect = false)
{
$config = ShopifySDK::$config;

Expand All @@ -98,20 +99,25 @@ public static function createAuthRequest($scopes, $redirectUrl = null)
throw new SdkException("SharedSecret is required for getting access token. Please check SDK configuration!");
}

//If redirect url is the same as this url, then need to check for access token when redirected back from shopify
// If redirect url is the same as this url, then need to check for access token when redirected back from shopify
if(isset($_GET['code'])) {
return self::getAccessToken($config);
} else {
$redirectUrl = self::getCurrentUrl();
return self::getAccessToken();
}

$redirectUrl = self::getCurrentUrl();
}

if (is_array($scopes)) {
$scopes = join(',', $scopes);
}
$authUrl = $config['AdminUrl'] . 'oauth/authorize?client_id=' . $config['ApiKey'] . '&redirect_uri=' . $redirectUrl . "&scope=$scopes";

header("Location: $authUrl");
if (!$autoRedirect) {
header("Location: $authUrl");
exit;
}

return $authUrl;
}

/**
Expand Down
78 changes: 78 additions & 0 deletions tests/AuthHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* ______ __ __
* / ____/___ ____ / /__________ / /
* / / / __ \/ __ \/ __/ ___/ __ \/ /
* / /___/ /_/ / / / / /_/ / / /_/ / /
* \______________/_/\__/_/ \____/_/
* / | / / /_
* / /| | / / __/
* / ___ |/ / /_
* /_/ _|||_/\__/ __ __
* / __ \___ / /__ / /____
* / / / / _ \/ / _ \/ __/ _ \
* / /_/ / __/ / __/ /_/ __/
* /_____/\___/_/\___/\__/\___/
*
*/

namespace PHPShopify;


use PHPShopify\Exception\SdkException;

class AuthHelperTest extends \PHPUnit_Framework_TestCase
{
public function createAuthRequestThrowsSdkExceptionProvider()
{
return [
'no config' => [
'config' => [],
'message' => 'ShopUrl and ApiKey are required for authentication request. Please check SDK configuration!',
],
'only ShopUrl' => [
'config' => ['ShopUrl' => ''],
'message' => 'ShopUrl and ApiKey are required for authentication request. Please check SDK configuration!',
],
'ShopUrl and ApiKey' => [
'config' => ['ShopUrl' => '', 'ApiKey' => ''],
'message' => 'SharedSecret is required for getting access token. Please check SDK configuration!',
],
];
}

/**
* @dataProvider createAuthRequestThrowsSdkExceptionProvider
* @param $config
* @param $message
* @throws SdkException
*/
public function testCreateAuthRequestThrowsSdkException($config, $message)
{
$this->expectException(SdkException::class);
$this->expectExceptionMessage($message);

ShopifySDK::$config = $config;

AuthHelper::createAuthRequest('read_orders');
}

public function testReturnsTheRedirectUrl()
{
$_SERVER['HTTP_HOST'] = 'myshopifyapp.com';
$_SERVER['REQUEST_URI'] = '/test';

ShopifySDK::$config = [
'ShopUrl' => '',
'ApiKey' => '',
'SharedSecret' => '',
'AdminUrl' => 'https://test.myshopify.com/admin/',
];

$url = AuthHelper::createAuthRequest('read_orders', 'https://myshopifyapp.com/app/', true);

$expected = 'https://test.myshopify.com/admin/oauth/authorize?client_id=';
$expected .= '&redirect_uri=https://myshopifyapp.com/app/&scope=read_orders';
$this->assertEquals($expected, $url);
}
}