-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from itk-dev/develop
OpenId Connect Symfony Bundle
- Loading branch information
Showing
27 changed files
with
875 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace ItkDev\OpenIdConnectBundle\Command; | ||
|
||
use ItkDev\OpenIdConnectBundle\Exception\CacheException; | ||
use ItkDev\OpenIdConnectBundle\Util\CliLoginHelper; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
|
||
class UserLoginCommand extends Command | ||
{ | ||
protected static $defaultName = 'itk-dev:openid-connect:login'; | ||
protected static $defaultDescription = 'Get login url for user'; | ||
|
||
/** | ||
* @var UrlGeneratorInterface | ||
*/ | ||
private $urlGenerator; | ||
|
||
/** | ||
* @var CliLoginHelper | ||
*/ | ||
private $cliLoginHelper; | ||
|
||
/** | ||
* @var UserProviderInterface | ||
*/ | ||
private $userProvider; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $cliLoginRedirectRoute; | ||
|
||
/** | ||
* UserLoginCommand constructor. | ||
* | ||
* @param CliLoginHelper $cliLoginHelper | ||
* @param string $cliLoginRedirectRoute | ||
* @param UrlGeneratorInterface $urlGenerator | ||
* @param UserProviderInterface $userProvider | ||
*/ | ||
public function __construct(CliLoginHelper $cliLoginHelper, string $cliLoginRedirectRoute, UrlGeneratorInterface $urlGenerator, UserProviderInterface $userProvider) | ||
{ | ||
$this->cliLoginHelper = $cliLoginHelper; | ||
$this->cliLoginRedirectRoute = $cliLoginRedirectRoute; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->userProvider = $userProvider; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription(self::$defaultDescription) | ||
->addArgument('username', InputArgument::REQUIRED, 'Username'); | ||
} | ||
|
||
/** | ||
* Executes the CLI login url generation. | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return int | ||
* @throws CacheException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$username = $input->getArgument('username'); | ||
|
||
if (!is_string($username)) { | ||
$io->error('Username is not type string'); | ||
return Command::FAILURE; | ||
} | ||
// Check if username is registered in User database | ||
try { | ||
$this->userProvider->loadUserByUsername($username); | ||
} catch (UsernameNotFoundException $e) { | ||
$io->error('User does not exist'); | ||
return Command::FAILURE; | ||
} | ||
|
||
// Create token via CliLoginHelper | ||
$token = $this->cliLoginHelper->createToken($username); | ||
|
||
//Generate absolute url for login | ||
$loginPage = $this->urlGenerator->generate($this->cliLoginRedirectRoute, [ | ||
'loginToken' => $token, | ||
], UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$io->writeln($loginPage); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.