-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Middleware to Verify Scopes and reauthenticate if required (#187
- Loading branch information
1 parent
444ef60
commit 5a68ef8
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Osiset\ShopifyApp\Http\Middleware; | ||
|
||
use Closure; | ||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel; | ||
use Osiset\ShopifyApp\Util; | ||
|
||
class VerifyScopes | ||
{ | ||
/** | ||
* Checks if a shop has all required access scopes. | ||
* If a required access scope is missing, it will redirect the app | ||
* for re-authentication | ||
* | ||
* @param Request $request The request object. | ||
* @param Closure $next The next action. | ||
* | ||
* @throws Exception | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
/** @var $shop IShopModel */ | ||
$shop = auth()->user(); | ||
$scopesResponse = $shop->api()->rest('GET', '/admin/oauth/access_scopes.json'); | ||
if ($scopesResponse && $scopesResponse['errors']) { | ||
return $next($request); | ||
} | ||
$scopes = json_decode(json_encode($scopesResponse['body']['access_scopes']), false); | ||
$scopes = array_map(static function ($scope) { | ||
return $scope->handle; | ||
}, $scopes); | ||
|
||
$requiredScopes = explode(',', Util::getShopifyConfig('api_scopes')); | ||
$missingScopes = array_diff($requiredScopes, $scopes); | ||
if (count($missingScopes) === 0) { | ||
return $next($request); | ||
} | ||
|
||
return redirect()->route( | ||
Util::getShopifyConfig('route_names.authenticate'), | ||
[ | ||
'shop' => $shop->getDomain()->toNative(), | ||
'host' => $request->get('host'), | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Osiset\ShopifyApp\Test\Http\Middleware; | ||
|
||
use Illuminate\Auth\AuthManager; | ||
use Illuminate\Http\Request; | ||
use Osiset\ShopifyApp\Http\Middleware\VerifyScopes as VerifyScopesMiddleware; | ||
use Osiset\ShopifyApp\Test\Stubs\Api as ApiStub; | ||
use Osiset\ShopifyApp\Test\TestCase; | ||
|
||
class VerifyScopesTest extends TestCase | ||
{ | ||
/** | ||
* @var AuthManager | ||
*/ | ||
protected $auth; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->auth = $this->app->make(AuthManager::class); | ||
} | ||
|
||
public function testMissingScopes(): void | ||
{ | ||
// Setup API stub | ||
$this->setApiStub(); | ||
ApiStub::stubResponses(['access_scopes']); | ||
|
||
$this->app['config']->set('shopify-app.api_scopes', 'read_products,write_products,read_orders'); | ||
|
||
$shop = factory($this->model)->create(); | ||
$this->auth->login($shop); | ||
|
||
$request = Request::create('/', 'GET', ['shop' => $shop->getDomain()->toNative()]); | ||
|
||
// Run the middleware | ||
$middleware = new VerifyScopesMiddleware(); | ||
$result = $middleware->handle($request, function () { | ||
}); | ||
|
||
//this line needs to assert if proper redirect was made | ||
$this->assertEquals(302, $result->getStatusCode()); | ||
} | ||
|
||
public function testMatchingScopes(): void | ||
{ | ||
// Setup API stub | ||
$this->setApiStub(); | ||
ApiStub::stubResponses(['access_scopes']); | ||
|
||
$this->app['config']->set('shopify-app.api_scopes', 'read_products,write_products'); | ||
|
||
$shop = factory($this->model)->create(); | ||
$this->auth->login($shop); | ||
|
||
$request = Request::create('/', 'GET', ['shop' => $shop->getDomain()->toNative()]); | ||
|
||
// Run the middleware | ||
$middleware = new VerifyScopesMiddleware(); | ||
$result = $middleware->handle($request, function () { | ||
}); | ||
|
||
//this line needs to assert if proper redirect was made | ||
$this->assertEquals($result, null); | ||
} | ||
|
||
public function testScopeApiFailure(): void | ||
{ | ||
// Setup API stub | ||
$this->setApiStub(); | ||
ApiStub::stubResponses(['access_scopes_error']); | ||
|
||
$this->app['config']->set('shopify-app.api_scopes', 'read_products,write_products'); | ||
|
||
$shop = factory($this->model)->create(); | ||
$this->auth->login($shop); | ||
|
||
$request = Request::create('/', 'GET', ['shop' => $shop->getDomain()->toNative()]); | ||
|
||
// Run the middleware | ||
$middleware = new VerifyScopesMiddleware(); | ||
$result = $middleware->handle($request, function () { | ||
}); | ||
|
||
//this line needs to assert if proper redirect was made | ||
$this->assertEquals($result, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"access_scopes": [ | ||
{ | ||
"handle": "read_products" | ||
}, | ||
{ | ||
"handle": "write_products" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Could not get access copes" | ||
} | ||
] | ||
} |