Skip to content

Commit

Permalink
Scope一覧はScopeManagerから取得する
Browse files Browse the repository at this point in the history
  • Loading branch information
kiy0taka committed Aug 31, 2023
1 parent c913e89 commit 0d57fcd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
30 changes: 10 additions & 20 deletions Form/Type/Admin/ClientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
use Eccube\Form\FormEvent;
use Eccube\Form\Type\AbstractType;
use Eccube\Validator\Constraints as Assert;
use GraphQL\Type\Definition\ObjectType;
use League\Bundle\OAuth2ServerBundle\OAuth2Grants;
use Plugin\Api42\GraphQL\Types;
use Plugin\Api42\Service\ScopeManager;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

Expand All @@ -31,7 +30,8 @@ class ClientType extends AbstractType
* @var EccubeConfig
*/
protected $eccubeConfig;
private Types $types;

private ScopeManager $scopeManager;

/**
* ClientType constructor.
Expand All @@ -40,10 +40,10 @@ class ClientType extends AbstractType
*/
public function __construct(
EccubeConfig $eccubeConfig,
Types $types
ScopeManager $scopeManager
) {
$this->eccubeConfig = $eccubeConfig;
$this->types = $types;
$this->scopeManager = $scopeManager;
}

/**
Expand All @@ -53,21 +53,11 @@ public function __construct(
*/
public function buildForm(FormBuilder $builder, array $options)
{
$allTypes = array_filter($this->types->getAll(), function (ObjectType $type) {
return !empty($type->getFields());
});
asort($allTypes);
$scopes = array_reduce(
$allTypes,
function ($acc, $type) {
$read = 'read:'.$type->name;
$write = 'write:'.$type->name;
$acc[$read] = $read;
$acc[$write] = $write;

return $acc;
},
[]);
$scopes = array_reduce($this->scopeManager->getScopes(), function ($acc, $val) {
$scope = (string) $val;
$acc[$scope] = $scope;
return $acc;
}, []);

$builder
->add('identifier', TextType::class, [
Expand Down
5 changes: 5 additions & 0 deletions Service/ScopeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public function save(Scope $scope): void
{
// NOP
}

public function getScopes(): array
{
return $this->scopes;
}
}
12 changes: 6 additions & 6 deletions Tests/Web/Admin/OAuth2Bundle/AuthorizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testRoutingAdminOauth2Authorize_ログインしている場合
'client_id' => $Client->getIdentifier(),
'redirect_uri' => (string) current($Client->getRedirectUris()),
'response_type' => 'code',
'scope' => 'read',
'scope' => 'read:Product',
'state' => 'xxx',
]
)
Expand All @@ -69,7 +69,7 @@ public function testRoutingAdminOauth2Authorize_権限移譲を許可()
'client_id' => $Client->getIdentifier(),
'redirect_uri' => (string) current($Client->getRedirectUris()),
'response_type' => 'code',
'scope' => 'read',
'scope' => 'read:Product',
'state' => 'xxx',
]
);
Expand All @@ -82,7 +82,7 @@ public function testRoutingAdminOauth2Authorize_権限移譲を許可()
'client_secret' => $Client->getSecret(),
'redirect_uri' => current($Client->getRedirectUris()),
'response_type' => 'code',
'scope' => 'read',
'scope' => 'read:Product',
'state' => 'xxx',
'approve' => '',
Constant::TOKEN_NAME => 'dummy',
Expand Down Expand Up @@ -114,7 +114,7 @@ public function testRoutingAdminOauth2Authorize_権限移譲を許可しない()
'client_id' => $Client->getIdentifier(),
'redirect_uri' => (string) current($Client->getRedirectUris()),
'response_type' => 'code',
'scope' => 'read',
'scope' => 'read:Product',
'state' => 'xxx',
]
);
Expand All @@ -127,7 +127,7 @@ public function testRoutingAdminOauth2Authorize_権限移譲を許可しない()
'client_secret' => $Client->getSecret(),
'redirect_uri' => current($Client->getRedirectUris()),
'response_type' => 'code',
'scope' => 'read',
'scope' => 'read:Product',
'state' => 'xxx',
'deny' => '',
Constant::TOKEN_NAME => 'dummy',
Expand Down Expand Up @@ -187,7 +187,7 @@ private function createOAuth2Client(): Client
$client_secret = hash('sha256', random_bytes(32));
$Client = new Client('', $client_id, $client_secret);
$Client
->setScopes(new Scope('read'))
->setScopes(new Scope('read:Product'))
->setRedirectUris(new RedirectUri('http://127.0.0.1:8000/'))
->setGrants(
new Grant(OAuth2Grants::AUTHORIZATION_CODE),
Expand Down
6 changes: 3 additions & 3 deletions Tests/Web/OAuth2Bundle/TokenControllerWithROPCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testGetInstance()
'client_id' => $this->OAuth2Client->getIdentifier(),
'username' => $this->Customer->getEmail(),
'password' => 'password',
'scope' => 'read write'
'scope' => 'read:Product write:Product'
]
);

Expand All @@ -73,8 +73,8 @@ protected function createOAuth2Client(): Client
$Client = new Client('', $client_id, null); // public client
$Client
->setScopes(
new Scope('read'),
new Scope('write')
new Scope('read:Product'),
new Scope('write:Product')
)
->setRedirectUris(new RedirectUri('http://127.0.0.1:8000/'))
->setGrants(
Expand Down

0 comments on commit 0d57fcd

Please sign in to comment.