From c3a23ced116da0d3172add83bfcced1c10eb0817 Mon Sep 17 00:00:00 2001 From: zero Date: Sun, 3 Mar 2024 18:58:05 +0100 Subject: [PATCH] auth-cli-commands (#25) * Iam: cli commands. * Fix SA ARK. --- .../CreateMarketplaceOauthClientCommand.php | 116 ++++++++++++++++++ ...eateStageMarketplaceOauthClientCommand.php | 116 ++++++++++++++++++ phparkitect-baseline.json | 10 ++ psalm-baseline.xml | 34 +++++ .../allowed_in_iam_adapters.php | 3 + 5 files changed, 279 insertions(+) create mode 100644 _iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php create mode 100644 _iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php diff --git a/_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php b/_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php new file mode 100644 index 0000000..f77a82d --- /dev/null +++ b/_iam/src/AdapterForCli/CreateMarketplaceOauthClientCommand.php @@ -0,0 +1,116 @@ +checkDefaultOauthClientOrCreate($io); + } catch (Exception $exception) { + $io->error($exception->getMessage()); + return Command::FAILURE; + } + + return Command::SUCCESS; + } + + private function createOauthClientForMarketplaceEngine() + { + $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) + ->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 + { + $client = new Client($name, $identifier, $secret); + $client->setActive(true); + $client->setAllowPlainTextPkce(false); + + return $client + ->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'); + } + } +} diff --git a/_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php b/_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php new file mode 100644 index 0000000..3622eec --- /dev/null +++ b/_iam/src/AdapterForCli/CreateStageMarketplaceOauthClientCommand.php @@ -0,0 +1,116 @@ +checkDefaultOauthClientOrCreate($io); + } catch (Exception $exception) { + $io->error($exception->getMessage()); + return Command::FAILURE; + } + + return Command::SUCCESS; + } + + private function createOauthClientForMarketplaceEngine() + { + $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) + ->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 + { + $client = new Client($name, $identifier, $secret); + $client->setActive(true); + $client->setAllowPlainTextPkce(false); + + return $client + ->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'); + } + } +} diff --git a/phparkitect-baseline.json b/phparkitect-baseline.json index 84ff6c2..e931d8c 100644 --- a/phparkitect-baseline.json +++ b/phparkitect-baseline.json @@ -20,6 +20,16 @@ "line": 27, "error": "depends on App\\Repository\\UserRepository, but should not depend on these namespaces: App\\Controller\\*, App\\Service\\*, App\\Repository\\* because of component architecture" }, + { + "fqcn": "IdentityAccess\\AdapterForCli\\CreateMarketplaceOauthClientCommand", + "line": 88, + "error": "depends on App\\Entity\\OAuth2ClientProfile, but should not depend on classes outside namespace IdentityAccess\\Core because or namespaces in whitelist we want isolate our identity access Adapters from ever growing dependencies." + }, + { + "fqcn": "IdentityAccess\\AdapterForCli\\CreateStageMarketplaceOauthClientCommand", + "line": 88, + "error": "depends on App\\Entity\\OAuth2ClientProfile, but should not depend on classes outside namespace IdentityAccess\\Core because or namespaces in whitelist we want isolate our identity access Adapters from ever growing dependencies." + }, { "fqcn": "IdentityAccess\\AdapterForReadingAccounts\\AccountsDataProvider", "line": 26, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2cda8c7..8beee67 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,39 @@ + + + $oAuthClient + + + 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))]]> + + + createOauthClientForMarketplaceEngine + + + AbstractClient + + + + + $oAuthClient + + + 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))]]> + + + createOauthClientForMarketplaceEngine + + + AbstractClient + + getData()]]> diff --git a/tools/phparkitect/VendorDependencies/allowed_in_iam_adapters.php b/tools/phparkitect/VendorDependencies/allowed_in_iam_adapters.php index fa634eb..cd0857f 100644 --- a/tools/phparkitect/VendorDependencies/allowed_in_iam_adapters.php +++ b/tools/phparkitect/VendorDependencies/allowed_in_iam_adapters.php @@ -9,11 +9,14 @@ 'League\Bundle\OAuth2ServerBundle', 'Doctrine\DBAL\Connection', + 'Doctrine\ORM\EntityManagerInterface', 'Symfony\Bundle\FrameworkBundle\Controller\AbstractController', 'Symfony\Component\HttpFoundation\Request', 'Symfony\Component\HttpFoundation\Response', + 'Symfony\Component\Console', + 'Symfony\Component\Form', 'Symfony\Component\OptionsResolver\OptionsResolver',