2020-07-22 01:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
declare(strict_types=1);
|
2021-10-10 09:26:18 +01:00
|
|
|
|
2020-07-22 01:58:25 +00:00
|
|
|
// {{{ 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;
|
|
|
|
|
2021-10-21 14:45:18 +01:00
|
|
|
use App\Core\Router\Router;
|
2021-09-06 23:47:28 +01:00
|
|
|
use App\Entity\LocalUser;
|
2021-11-14 23:15:24 +00:00
|
|
|
use App\Util\Common;
|
2021-10-21 14:45:18 +01:00
|
|
|
use App\Util\Exception\NoSuchActorException;
|
2021-11-11 12:30:42 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-11-16 14:48:18 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-11-14 23:15:24 +00:00
|
|
|
use App\Util\Nickname;
|
2021-10-21 14:45:18 +01:00
|
|
|
use Stringable;
|
2020-07-22 01:58:25 +00:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
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;
|
2021-10-21 14:45:18 +01:00
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
2020-07-22 01:58:25 +00:00
|
|
|
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;
|
2021-11-16 14:48:18 +00:00
|
|
|
use Symfony\Component\Security\Guard\AuthenticatorInterface;
|
2020-07-22 01:58:25 +00:00
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
2021-11-16 14:48:18 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2020-07-22 01:58:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
class Authenticator extends AbstractFormLoginAuthenticator implements AuthenticatorInterface
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
|
|
|
use TargetPathTrait;
|
|
|
|
|
2021-10-10 17:41:30 +01:00
|
|
|
public const LOGIN_ROUTE = 'security_login';
|
2020-07-22 01:58:25 +00:00
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
private CsrfTokenManagerInterface $csrfTokenManager;
|
2020-07-22 01:58:25 +00:00
|
|
|
|
2021-11-01 19:43:23 +00:00
|
|
|
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:43:23 +00:00
|
|
|
/**
|
2021-11-16 14:48:18 +00:00
|
|
|
* @param Request $request
|
2021-11-01 19:43:23 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function supports(Request $request): bool
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
|
|
|
return self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->isMethod('POST');
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:43:23 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function getCredentials(Request $request): array
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
2021-10-10 17:41:30 +01:00
|
|
|
return [
|
2021-10-21 14:45:18 +01:00
|
|
|
'nickname_or_email' => $request->request->get('nickname_or_email'),
|
2021-11-16 14:48:18 +00:00
|
|
|
'password' => $request->request->get('password'),
|
|
|
|
'csrf_token' => $request->request->get('_csrf_token'),
|
2020-07-22 01:58:25 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:45:18 +01:00
|
|
|
/**
|
|
|
|
* Get a user given credentials and a CSRF token
|
2021-11-01 19:43:23 +00:00
|
|
|
*
|
|
|
|
* @param array<string, string> $credentials result of self::getCredentials
|
2021-11-16 14:48:18 +00:00
|
|
|
* @param UserProviderInterface $userProvider
|
2021-11-01 19:43:23 +00:00
|
|
|
* @return ?LocalUser
|
2021-11-16 14:48:18 +00:00
|
|
|
* @throws NoSuchActorException
|
|
|
|
* @throws ServerException
|
2021-10-21 14:45:18 +01:00
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider): ?LocalUser
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
|
|
|
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
|
|
|
|
if (!$this->csrfTokenManager->isTokenValid($token)) {
|
|
|
|
throw new InvalidCsrfTokenException();
|
|
|
|
}
|
2021-11-01 19:43:23 +00:00
|
|
|
$user = null;
|
2020-08-14 00:18:31 +00:00
|
|
|
try {
|
2021-11-14 23:15:24 +00:00
|
|
|
if (Common::isValidEmail($credentials['nickname_or_email'])) {
|
2021-10-10 17:41:30 +01:00
|
|
|
$user = LocalUser::getByEmail($credentials['nickname_or_email']);
|
2021-11-14 23:15:24 +00:00
|
|
|
} elseif (Nickname::isValid($credentials['nickname_or_email'])) {
|
2021-11-11 12:30:42 +00:00
|
|
|
$user = LocalUser::getByNickname($credentials['nickname_or_email']);
|
2021-10-10 17:41:30 +01:00
|
|
|
}
|
2021-11-16 14:48:18 +00:00
|
|
|
if (is_null($user)) {
|
2021-10-10 17:41:30 +01:00
|
|
|
throw new NoSuchActorException('No such local user.');
|
|
|
|
}
|
|
|
|
$credentials['nickname'] = $user->getNickname();
|
2021-11-16 14:48:18 +00:00
|
|
|
} catch (NoSuchActorException|NotFoundException) {
|
2020-07-22 01:58:25 +00:00
|
|
|
throw new CustomUserMessageAuthenticationException(
|
2021-10-10 09:26:18 +01:00
|
|
|
_m('Invalid login credentials.'),
|
|
|
|
);
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-10-10 09:35:36 +01:00
|
|
|
/**
|
2021-11-01 19:43:23 +00:00
|
|
|
* @param array<string, string> $credentials result of self::getCredentials
|
2021-11-16 14:48:18 +00:00
|
|
|
* @param LocalUser $user
|
|
|
|
* @return bool
|
|
|
|
* @throws ServerException
|
2021-10-10 09:35:36 +01:00
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function checkCredentials($credentials, $user): bool
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:45:18 +01:00
|
|
|
/**
|
|
|
|
* After a successful login, redirect user to the path saved in their session or to the root of the website
|
|
|
|
*/
|
2021-11-16 14:48:18 +00:00
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): RedirectResponse
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
2021-10-21 14:45:18 +01:00
|
|
|
$nickname = $token->getUser();
|
|
|
|
if ($nickname instanceof Stringable) {
|
2021-11-16 14:48:18 +00:00
|
|
|
$nickname = (string)$nickname;
|
2021-10-21 14:45:18 +01:00
|
|
|
} elseif ($nickname instanceof UserInterface) {
|
2021-11-16 14:48:18 +00:00
|
|
|
$nickname = $nickname->getUserIdentifier();
|
2021-10-21 14:45:18 +01:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:41:30 +01:00
|
|
|
$request->getSession()->set(
|
|
|
|
Security::LAST_USERNAME,
|
2021-10-21 14:45:18 +01:00
|
|
|
$nickname,
|
2021-10-10 17:41:30 +01:00
|
|
|
);
|
2021-10-21 14:45:18 +01:00
|
|
|
|
2020-07-22 01:58:25 +00:00
|
|
|
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
|
|
|
|
return new RedirectResponse($targetPath);
|
|
|
|
}
|
|
|
|
|
2021-10-24 15:31:28 +01:00
|
|
|
return new RedirectResponse(Router::url('main_all'));
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:48:18 +00:00
|
|
|
protected function getLoginUrl(): string
|
2020-07-22 01:58:25 +00:00
|
|
|
{
|
2021-10-21 14:45:18 +01:00
|
|
|
return Router::url(self::LOGIN_ROUTE);
|
2020-07-22 01:58:25 +00:00
|
|
|
}
|
|
|
|
}
|