Use Backend API authorization scopes
Edit on GitHubThis guide describes how to add scopes to the resource and custom route for the Backend API application.
Let’s say you have a module named ModuleBackendApi
with GET
and POST
methods, where you want to add scopes. To add scopes, follow these steps:
-
Set up a resource for the Backend API application.
-
To implement
ScopeDefinitionPluginInterface
and set up the scopes, adjustModuleResource
:
Pyz\Glue\ModuleBackendApi\Plugin\ModuleResource.php
<?php
namespace Pyz\Glue\ModuleBackendApi\Plugin;
use Spryker\Glue\GlueApplication\Plugin\GlueApplication\AbstractResourcePlugin;
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceInterface;
use Spryker\Glue\OauthExtension\Dependency\Plugin\ScopeDefinitionPluginInterface;
class ModuleResource extends AbstractResourcePlugin implements ResourceInterface, ScopeDefinitionPluginInterface
{
public function getScopes(): array
{
return [
'get' => 'storefront:module:read',
'post' => 'storefront:module:write',
];
}
}
- To implement
ScopeRouteProviderPluginInterface
and set up the scopes, adjustModuleBarRouteProviderPlugin
:
Pyz\Glue\ModuleBackendApi\Plugin\ModuleBarRouteProviderPlugin.php
<?php
namespace Pyz\Glue\ModuleBackendApi\Plugin;
use Pyz\Glue\ModuleBackendApi\Controller\ModuleBarController;
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\RouteProviderPluginInterface;
use Spryker\Glue\Kernel\Backend\AbstractPlugin;
use Spryker\Glue\OauthExtension\Dependency\Plugin\ScopeRouteProviderPluginInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ModuleBarRouteProviderPlugin extends AbstractPlugin implements RouteProviderPluginInterface, ScopeRouteProviderPluginInterface
{
public function addRoutes(RouteCollection $routeCollection): RouteCollection
{
$getRoute = (new Route('/module/bar'))
->setDefaults([
'_controller' => [ModuleBarController::class, 'getCollectionAction'],
'_resourceName' => 'moduleBar',
'_method'=> 'get'
])
->setMethods(Request::METHOD_GET);
$getRoute->addDefaults(['scope' => 'backend:modulebar:read']);
$routeCollection->add('moduleBarGetCollection', $getRoute);
return $routeCollection;
}
}
- Regenerate the scopes cache file:
vendor/bin/console oauth:scope-collection-file:generate
Verification
-
Ensure that when accessing
https://glue-backend.mysprykershop.com/module/bar
without an access token, you receive the403
response with the messageUnauthorized request
. -
Ensure that you can authenticate as a user:
- Send the request:
POST /token/ HTTP/1.1 Host: glue-backend.mysprykershop.com Content-Type: application/x-www-form-urlencoded Accept: application/json Content-Length: 117 grant_type=password&username={user_username}&password={user_password}&scope=backend%3module%3read%20backend%3modulebar%3read
- Check that the output contains the 201 response with a valid token.
- Enter a valid access token to access
https://glue-backend.mysprykershop.com/module/bar
.
Thank you!
For submitting the form