|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the medicalmundi/marketplace-accounts |
| 5 | + * |
| 6 | + * @copyright (c) 2023 MedicalMundi |
| 7 | + * |
| 8 | + * This software consists of voluntary contributions made by many individuals |
| 9 | + * {@link https://github.com/medicalmundi/marketplace-accounts/graphs/contributors developer} and is licensed under the MIT license. |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + * @license https://github.com/MedicalMundi/marketplace-accounts/blob/main/LICENSE MIT |
| 14 | + */ |
| 15 | + |
| 16 | +namespace IdentityAccess\AdapterForCli; |
| 17 | + |
| 18 | +use App\Entity\OAuth2ClientProfile; |
| 19 | +use Doctrine\ORM\EntityManagerInterface; |
| 20 | +use Exception; |
| 21 | +use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; |
| 22 | +use League\Bundle\OAuth2ServerBundle\Model\AbstractClient; |
| 23 | +use League\Bundle\OAuth2ServerBundle\Model\Client; |
| 24 | +use League\Bundle\OAuth2ServerBundle\ValueObject\Grant; |
| 25 | +use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri; |
| 26 | +use League\Bundle\OAuth2ServerBundle\ValueObject\Scope; |
| 27 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 28 | +use Symfony\Component\Console\Command\Command; |
| 29 | +use Symfony\Component\Console\Input\InputInterface; |
| 30 | +use Symfony\Component\Console\Output\OutputInterface; |
| 31 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 32 | + |
| 33 | +#[AsCommand( |
| 34 | + name: 'app:oauth:create-for-stage-marketplace', |
| 35 | + description: 'Create oAuth client for stage.marketplace website', |
| 36 | +)] |
| 37 | +class CreateStageMarketplaceOauthClientCommand extends Command |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private readonly ClientManagerInterface $clientManager, |
| 41 | + private readonly EntityManagerInterface $em, |
| 42 | + ) { |
| 43 | + parent::__construct(); |
| 44 | + } |
| 45 | + |
| 46 | + protected function configure(): void |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 51 | + { |
| 52 | + $io = new SymfonyStyle($input, $output); |
| 53 | + |
| 54 | + try { |
| 55 | + $this->checkDefaultOauthClientOrCreate($io); |
| 56 | + } catch (Exception $exception) { |
| 57 | + $io->error($exception->getMessage()); |
| 58 | + return Command::FAILURE; |
| 59 | + } |
| 60 | + |
| 61 | + return Command::SUCCESS; |
| 62 | + } |
| 63 | + |
| 64 | + private function createOauthClientForMarketplaceEngine() |
| 65 | + { |
| 66 | + $clientName = 'Stage Marketplace Engine Client'; |
| 67 | + $clientId = 'stage-marketplace-engine'; |
| 68 | + $clientSecret = 'stage-marketplace'; |
| 69 | + $clientDescription = 'Stage Marketplace website'; |
| 70 | + $scopes = ['email']; |
| 71 | + $grantTypes = ['authorization_code', 'refresh_token']; |
| 72 | + $redirectUris = ['https://stage.marketplace.oe-modules.com/connect/oemodules/check']; |
| 73 | + |
| 74 | + $oAuthClient = $this |
| 75 | + ->buildOauthClient( |
| 76 | + $clientName, |
| 77 | + $clientId, |
| 78 | + $clientSecret, |
| 79 | + $redirectUris, |
| 80 | + $grantTypes, |
| 81 | + $scopes, |
| 82 | + $clientDescription |
| 83 | + ); |
| 84 | + |
| 85 | + $this->clientManager->save($oAuthClient); |
| 86 | + |
| 87 | + // Create Client Profile |
| 88 | + $oAuth2ClientProfile = new OAuth2ClientProfile(); |
| 89 | + $oAuth2ClientProfile->setClient($oAuthClient) |
| 90 | + ->setName($clientName) |
| 91 | + ->setDescription($clientDescription); |
| 92 | + $this->em->persist($oAuth2ClientProfile); |
| 93 | + $this->em->flush(); |
| 94 | + } |
| 95 | + |
| 96 | + private function buildOauthClient(string $name, string $identifier, string $secret, array $redirectUriStrings, array $grantStrings, array $scopeStrings, string $clientDescription): AbstractClient |
| 97 | + { |
| 98 | + $client = new Client($name, $identifier, $secret); |
| 99 | + $client->setActive(true); |
| 100 | + $client->setAllowPlainTextPkce(false); |
| 101 | + |
| 102 | + return $client |
| 103 | + ->setRedirectUris(...array_map(static fn (string $redirectUri): RedirectUri => new RedirectUri($redirectUri), $redirectUriStrings)) |
| 104 | + ->setGrants(...array_map(static fn (string $grant): Grant => new Grant($grant), $grantStrings)) |
| 105 | + ->setScopes(...array_map(static fn (string $scope): Scope => new Scope($scope), $scopeStrings)) |
| 106 | + ; |
| 107 | + } |
| 108 | + |
| 109 | + private function checkDefaultOauthClientOrCreate(SymfonyStyle $io): void |
| 110 | + { |
| 111 | + if (null === $this->clientManager->find('stage-marketplace-engine')) { |
| 112 | + $this->createOauthClientForMarketplaceEngine(); |
| 113 | + $io->success('Oauth Client with identifier \'stage-marketplace-engine\' was created'); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments