2020-07-22 01:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace App\Security;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
2021-10-10 17:41:30 +01:00
|
|
|
use App\Util\Exception\NoSuchActorException;
|
|
|
|
use App\Util\Exception\NotFoundException;
|
2020-07-22 01:58:25 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2021-09-06 23:47:28 +01:00
|
|
|
use App\Entity\LocalUser;
|
2020-07-22 01:58:25 +00:00
|
|
|
use App\Entity\User;
|
|
|
|
use App\Util\Nickname;
|
2020-08-14 00:18:31 +00:00
|
|
|
use Exception;
|
2020-07-22 01:58:25 +00:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
|
|
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken;
|
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
|
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User authenticator
|
|
|
|
*
|
|
|
|
* @category Authentication
|
|
|
|
* @package GNUsocial
|
|
|
|
*
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-07-22 01:58:25 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class Authenticator extends AbstractFormLoginAuthenticator
|
|
|
|
{
|
|
|
|
use TargetPathTrait;
|
|
|
|
|
2021-10-10 17:41:30 +01:00
|
|
|
public const LOGIN_ROUTE = 'security_login';
|
2020-07-22 01:58:25 +00:00
|
|
|
|
|
|
|
private $urlGenerator;
|
|
|
|
private $csrfTokenManager;
|
|
|
|
|
2020-07-25 02:07:46 +00:00
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager)
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supports(Request $request)
|
|
|
|
{
|
|
|
|
return self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->isMethod('POST');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCredentials(Request $request)
|
|
|
|
{
|
2021-10-10 17:41:30 +01:00
|
|
|
return [
|
|
|
|
'nickname_or_email' => $request->request->get('nickname_or_email'),
|
2020-07-22 01:58:25 +00:00
|
|
|
'password' => $request->request->get('password'),
|
|
|
|
'csrf_token' => $request->request->get('_csrf_token'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider)
|
|
|
|
{
|
|
|
|
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
|
|
|
|
if (!$this->csrfTokenManager->isTokenValid($token)) {
|
|
|
|
throw new InvalidCsrfTokenException();
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:18:31 +00:00
|
|
|
try {
|
2021-10-10 17:41:30 +01:00
|
|
|
if (filter_var($credentials['nickname_or_email'], FILTER_VALIDATE_EMAIL) !== false) {
|
|
|
|
$user = LocalUser::getByEmail($credentials['nickname_or_email']);
|
|
|
|
} else {
|
|
|
|
$user = LocalUser::getWithPK(['nickname' => Nickname::normalize($credentials['nickname_or_email'], check_already_used: false)]);
|
|
|
|
}
|
|
|
|
if ($user === null) {
|
|
|
|
throw new NoSuchActorException('No such local user.');
|
|
|
|
}
|
|
|
|
$credentials['nickname'] = $user->getNickname();
|
|
|
|
} catch (Exception) {
|
2020-07-22 01:58:25 +00:00
|
|
|
throw new CustomUserMessageAuthenticationException(
|
2021-09-15 14:47:29 +01:00
|
|
|
_m('Invalid login credentials.'));
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
/**
|
|
|
|
* @param LocalUser $user
|
|
|
|
* @param mixed $credentials
|
|
|
|
*/
|
|
|
|
public function checkCredentials($credentials, $user)
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
2020-07-22 11:45:03 +00:00
|
|
|
if (!$user->checkPassword($credentials['password'])) {
|
|
|
|
throw new CustomUserMessageAuthenticationException(_m('Invalid login credentials.'));
|
|
|
|
} else {
|
|
|
|
return true;
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
|
|
|
|
{
|
2021-10-10 17:41:30 +01:00
|
|
|
$request->getSession()->set(
|
|
|
|
Security::LAST_USERNAME,
|
|
|
|
$token->getUser()->getNickname()
|
|
|
|
);
|
2020-07-22 01:58:25 +00:00
|
|
|
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
|
|
|
|
return new RedirectResponse($targetPath);
|
|
|
|
}
|
|
|
|
|
2020-07-22 11:45:03 +00:00
|
|
|
return new RedirectResponse($this->urlGenerator->generate('main_all'));
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLoginUrl()
|
|
|
|
{
|
|
|
|
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
|
|
|
|
}
|
|
|
|
}
|