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'); + } + } +}