login consent app sql

This commit is contained in:
2022-05-03 08:54:45 +02:00
parent e7253acfd8
commit f9a6535906
1652 changed files with 187600 additions and 45 deletions

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink\Exception;
use Symfony\Component\Security\Core\Signature\Exception\ExpiredSignatureException;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class ExpiredLoginLinkException extends ExpiredSignatureException implements InvalidLoginLinkExceptionInterface
{
}

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink\Exception;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
/**
* Thrown when a login link is invalid.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class InvalidLoginLinkAuthenticationException extends AuthenticationException
{
/**
* {@inheritdoc}
*/
public function getMessageKey()
{
return 'Invalid or expired login link.';
}
}

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink\Exception;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class InvalidLoginLinkException extends \RuntimeException implements InvalidLoginLinkExceptionInterface
{
}

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink\Exception;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
interface InvalidLoginLinkExceptionInterface extends \Throwable
{
}

View File

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class LoginLinkDetails
{
private $url;
private $expiresAt;
public function __construct(string $url, \DateTimeImmutable $expiresAt)
{
$this->url = $url;
$this->expiresAt = $expiresAt;
}
public function getUrl(): string
{
return $this->url;
}
public function getExpiresAt(): \DateTimeImmutable
{
return $this->expiresAt;
}
public function __toString()
{
return $this->url;
}
}

View File

@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Signature\Exception\ExpiredSignatureException;
use Symfony\Component\Security\Core\Signature\Exception\InvalidSignatureException;
use Symfony\Component\Security\Core\Signature\SignatureHasher;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\LoginLink\Exception\ExpiredLoginLinkException;
use Symfony\Component\Security\Http\LoginLink\Exception\InvalidLoginLinkException;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
final class LoginLinkHandler implements LoginLinkHandlerInterface
{
private $urlGenerator;
private $userProvider;
private $options;
private $signatureHashUtil;
public function __construct(UrlGeneratorInterface $urlGenerator, UserProviderInterface $userProvider, SignatureHasher $signatureHashUtil, array $options)
{
$this->urlGenerator = $urlGenerator;
$this->userProvider = $userProvider;
$this->signatureHashUtil = $signatureHashUtil;
$this->options = array_merge([
'route_name' => null,
'lifetime' => 600,
], $options);
}
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails
{
$expires = time() + $this->options['lifetime'];
$expiresAt = new \DateTimeImmutable('@'.$expires);
$parameters = [
// @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
'user' => method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(),
'expires' => $expires,
'hash' => $this->signatureHashUtil->computeSignatureHash($user, $expires),
];
if ($request) {
$currentRequestContext = $this->urlGenerator->getContext();
$this->urlGenerator->setContext(
(new RequestContext())
->fromRequest($request)
->setParameter('_locale', $request->getLocale())
);
}
try {
$url = $this->urlGenerator->generate(
$this->options['route_name'],
$parameters,
UrlGeneratorInterface::ABSOLUTE_URL
);
} finally {
if ($request) {
$this->urlGenerator->setContext($currentRequestContext);
}
}
return new LoginLinkDetails($url, $expiresAt);
}
public function consumeLoginLink(Request $request): UserInterface
{
$userIdentifier = $request->get('user');
try {
// @deprecated since Symfony 5.3, change to $this->userProvider->loadUserByIdentifier() in 6.0
if (method_exists($this->userProvider, 'loadUserByIdentifier')) {
$user = $this->userProvider->loadUserByIdentifier($userIdentifier);
} else {
trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "loadUserByIdentifier()" in user provider "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.', get_debug_type($this->userProvider));
$user = $this->userProvider->loadUserByUsername($userIdentifier);
}
} catch (UserNotFoundException $exception) {
throw new InvalidLoginLinkException('User not found.', 0, $exception);
}
$hash = $request->get('hash');
$expires = $request->get('expires');
try {
$this->signatureHashUtil->verifySignatureHash($user, $expires, $hash);
} catch (ExpiredSignatureException $e) {
throw new ExpiredLoginLinkException(ucfirst(str_ireplace('signature', 'login link', $e->getMessage())), 0, $e);
} catch (InvalidSignatureException $e) {
throw new InvalidLoginLinkException(ucfirst(str_ireplace('signature', 'login link', $e->getMessage())), 0, $e);
}
return $user;
}
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* A class that is able to create and handle "magic" login links.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
interface LoginLinkHandlerInterface
{
/**
* Generate a link that can be used to authenticate as the given user.
*/
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails;
/**
* Validates if this request contains a login link and returns the associated User.
*
* Throw InvalidLoginLinkExceptionInterface if the link is invalid.
*/
public function consumeLoginLink(Request $request): UserInterface;
}

View File

@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\LoginLink;
use Symfony\Bridge\Twig\Mime\NotificationEmail;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
/**
* Use this notification to ease sending login link
* emails/SMS using the Notifier component.
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
class LoginLinkNotification extends Notification implements EmailNotificationInterface, SmsNotificationInterface
{
private $loginLinkDetails;
public function __construct(LoginLinkDetails $loginLinkDetails, string $subject, array $channels = [])
{
parent::__construct($subject, $channels);
$this->loginLinkDetails = $loginLinkDetails;
}
public function asEmailMessage(EmailRecipientInterface $recipient, string $transport = null): ?EmailMessage
{
if (!class_exists(NotificationEmail::class)) {
throw new \LogicException(sprintf('The "%s" method requires "symfony/twig-bridge:>4.4".', __METHOD__));
}
$email = NotificationEmail::asPublicEmail()
->to($recipient->getEmail())
->subject($this->getSubject())
->content($this->getContent() ?: $this->getDefaultContent('button below'))
->action('Sign in', $this->loginLinkDetails->getUrl())
;
return new EmailMessage($email);
}
public function asSmsMessage(SmsRecipientInterface $recipient, string $transport = null): ?SmsMessage
{
return new SmsMessage($recipient->getPhone(), $this->getDefaultContent('link').' '.$this->loginLinkDetails->getUrl());
}
private function getDefaultContent(string $target): string
{
$duration = $this->loginLinkDetails->getExpiresAt()->getTimestamp() - time();
$durationString = floor($duration / 60).' minute'.($duration > 60 ? 's' : '');
if (($hours = $duration / 3600) >= 1) {
$durationString = floor($hours).' hour'.($hours >= 2 ? 's' : '');
}
return sprintf('Click on the %s to confirm you want to sign in. This link will expire in %s.', $target, $durationString);
}
}