Skip to content

Commit

Permalink
Iam: cli commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerai committed Mar 3, 2024
1 parent 9b4ce82 commit 5ce4f3e
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
116 changes: 116 additions & 0 deletions _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php declare(strict_types=1);

/*
* This file is part of the medicalmundi/marketplace-accounts
*
* @copyright (c) 2023 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-accounts/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-accounts/blob/main/LICENSE MIT
*/

namespace IdentityAccess\AdapterForCli;

use App\Entity\OAuth2ClientProfile;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\Client;
use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'app:oauth:create-for-marketplace',
description: 'Create oAuth client for stage.marketplace website',
)]
class CreateMarketplaceOauthClientCommand extends Command
{
public function __construct(
private readonly ClientManagerInterface $clientManager,
private readonly EntityManagerInterface $em,
) {
parent::__construct();
}

protected function configure(): void
{
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

try {
$this->checkDefaultOauthClientOrCreate($io);
} catch (Exception $exception) {
$io->error($exception->getMessage());
return Command::FAILURE;
}

return Command::SUCCESS;
}

private function createOauthClientForMarketplaceEngine()

Check failure on line 64 in _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

MissingReturnType

_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php:64:22: MissingReturnType: Method IdentityAccess\AdapterForCli\CreateMarketplaceOauthClientCommand::createOauthClientForMarketplaceEngine does not have a return type, expecting void (see https://psalm.dev/050)
{
$clientName = 'Marketplace Engine Client';
$clientId = 'marketplace-engine';
$clientSecret = 'marketplace';
$clientDescription = 'Marketplace website';
$scopes = ['email'];
$grantTypes = ['authorization_code', 'refresh_token'];
$redirectUris = ['https://marketplace.oe-modules.com/connect/oemodules/check'];

$oAuthClient = $this
->buildOauthClient(
$clientName,
$clientId,
$clientSecret,
$redirectUris,
$grantTypes,
$scopes,
$clientDescription
);

$this->clientManager->save($oAuthClient);

// Create Client Profile
$oAuth2ClientProfile = new OAuth2ClientProfile();
$oAuth2ClientProfile->setClient($oAuthClient)

Check failure on line 89 in _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

ArgumentTypeCoercion

_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php:89:41: ArgumentTypeCoercion: Argument 1 of App\Entity\OAuth2ClientProfile::setClient expects League\Bundle\OAuth2ServerBundle\Model\Client, but parent type League\Bundle\OAuth2ServerBundle\Model\AbstractClient provided (see https://psalm.dev/193)
->setName($clientName)
->setDescription($clientDescription);
$this->em->persist($oAuth2ClientProfile);
$this->em->flush();
}

private function buildOauthClient(string $name, string $identifier, string $secret, array $redirectUriStrings, array $grantStrings, array $scopeStrings, string $clientDescription): AbstractClient

Check failure on line 96 in _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

MoreSpecificReturnType

_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php:96:186: MoreSpecificReturnType: The declared return type 'League\Bundle\OAuth2ServerBundle\Model\AbstractClient' for IdentityAccess\AdapterForCli\CreateMarketplaceOauthClientCommand::buildOauthClient is more specific than the inferred return type 'League\Bundle\OAuth2ServerBundle\Model\ClientInterface' (see https://psalm.dev/070)
{
$client = new Client($name, $identifier, $secret);
$client->setActive(true);
$client->setAllowPlainTextPkce(false);

return $client

Check failure on line 102 in _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

LessSpecificReturnStatement

_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php:102:16: LessSpecificReturnStatement: The type 'League\Bundle\OAuth2ServerBundle\Model\ClientInterface' is more general than the declared return type 'League\Bundle\OAuth2ServerBundle\Model\AbstractClient' for IdentityAccess\AdapterForCli\CreateMarketplaceOauthClientCommand::buildOauthClient (see https://psalm.dev/129)
->setRedirectUris(...array_map(static fn (string $redirectUri): RedirectUri => new RedirectUri($redirectUri), $redirectUriStrings))
->setGrants(...array_map(static fn (string $grant): Grant => new Grant($grant), $grantStrings))
->setScopes(...array_map(static fn (string $scope): Scope => new Scope($scope), $scopeStrings))
;
}

private function checkDefaultOauthClientOrCreate(SymfonyStyle $io): void
{
if (null === $this->clientManager->find('marketplace-engine')) {
$this->createOauthClientForMarketplaceEngine();
$io->success('Oauth Client with identifier \'marketplace-engine\' was created');
}
}
}
116 changes: 116 additions & 0 deletions _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php declare(strict_types=1);

/*
* This file is part of the medicalmundi/marketplace-accounts
*
* @copyright (c) 2023 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-accounts/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-accounts/blob/main/LICENSE MIT
*/

namespace IdentityAccess\AdapterForCli;

use App\Entity\OAuth2ClientProfile;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\Client;
use League\Bundle\OAuth2ServerBundle\ValueObject\Grant;
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'app:oauth:create-for-stage-marketplace',
description: 'Create oAuth client for stage.marketplace website',
)]
class CreateStageMarketplaceOauthClientCommand extends Command
{
public function __construct(
private readonly ClientManagerInterface $clientManager,
private readonly EntityManagerInterface $em,
) {
parent::__construct();
}

protected function configure(): void
{
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

try {
$this->checkDefaultOauthClientOrCreate($io);
} catch (Exception $exception) {
$io->error($exception->getMessage());
return Command::FAILURE;
}

return Command::SUCCESS;
}

private function createOauthClientForMarketplaceEngine()

Check failure on line 64 in _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

MissingReturnType

_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php:64:22: MissingReturnType: Method IdentityAccess\AdapterForCli\CreateStageMarketplaceOauthClientCommand::createOauthClientForMarketplaceEngine does not have a return type, expecting void (see https://psalm.dev/050)
{
$clientName = 'Stage Marketplace Engine Client';
$clientId = 'stage-marketplace-engine';
$clientSecret = 'stage-marketplace';
$clientDescription = 'Stage Marketplace website';
$scopes = ['email'];
$grantTypes = ['authorization_code', 'refresh_token'];
$redirectUris = ['https://stage.marketplace.oe-modules.com/connect/oemodules/check'];

$oAuthClient = $this
->buildOauthClient(
$clientName,
$clientId,
$clientSecret,
$redirectUris,
$grantTypes,
$scopes,
$clientDescription
);

$this->clientManager->save($oAuthClient);

// Create Client Profile
$oAuth2ClientProfile = new OAuth2ClientProfile();
$oAuth2ClientProfile->setClient($oAuthClient)

Check failure on line 89 in _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

ArgumentTypeCoercion

_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php:89:41: ArgumentTypeCoercion: Argument 1 of App\Entity\OAuth2ClientProfile::setClient expects League\Bundle\OAuth2ServerBundle\Model\Client, but parent type League\Bundle\OAuth2ServerBundle\Model\AbstractClient provided (see https://psalm.dev/193)
->setName($clientName)
->setDescription($clientDescription);
$this->em->persist($oAuth2ClientProfile);
$this->em->flush();
}

private function buildOauthClient(string $name, string $identifier, string $secret, array $redirectUriStrings, array $grantStrings, array $scopeStrings, string $clientDescription): AbstractClient

Check failure on line 96 in _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

MoreSpecificReturnType

_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php:96:186: MoreSpecificReturnType: The declared return type 'League\Bundle\OAuth2ServerBundle\Model\AbstractClient' for IdentityAccess\AdapterForCli\CreateStageMarketplaceOauthClientCommand::buildOauthClient is more specific than the inferred return type 'League\Bundle\OAuth2ServerBundle\Model\ClientInterface' (see https://psalm.dev/070)
{
$client = new Client($name, $identifier, $secret);
$client->setActive(true);
$client->setAllowPlainTextPkce(false);

return $client

Check failure on line 102 in _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

LessSpecificReturnStatement

_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php:102:16: LessSpecificReturnStatement: The type 'League\Bundle\OAuth2ServerBundle\Model\ClientInterface' is more general than the declared return type 'League\Bundle\OAuth2ServerBundle\Model\AbstractClient' for IdentityAccess\AdapterForCli\CreateStageMarketplaceOauthClientCommand::buildOauthClient (see https://psalm.dev/129)
->setRedirectUris(...array_map(static fn (string $redirectUri): RedirectUri => new RedirectUri($redirectUri), $redirectUriStrings))
->setGrants(...array_map(static fn (string $grant): Grant => new Grant($grant), $grantStrings))
->setScopes(...array_map(static fn (string $scope): Scope => new Scope($scope), $scopeStrings))
;
}

private function checkDefaultOauthClientOrCreate(SymfonyStyle $io): void
{
if (null === $this->clientManager->find('stage-marketplace-engine')) {
$this->createOauthClientForMarketplaceEngine();
$io->success('Oauth Client with identifier \'stage-marketplace-engine\' was created');
}
}
}

0 comments on commit 5ce4f3e

Please sign in to comment.