|
| 1 | +<?php |
| 2 | +namespace Yeebase\TwoFactorAuthentication\Security\Authentication\Provider; |
| 3 | + |
| 4 | +use Neos\Flow\Annotations as Flow; |
| 5 | +use Neos\Flow\Core\Bootstrap; |
| 6 | +use Neos\Flow\Http\HttpRequestHandlerInterface; |
| 7 | +use Neos\Flow\Persistence\PersistenceManagerInterface; |
| 8 | +use Neos\Flow\Security\Account; |
| 9 | +use Neos\Flow\Security\AccountRepository; |
| 10 | +use Neos\Flow\Security\Authentication\EntryPoint\WebRedirect; |
| 11 | +use Neos\Flow\Security\Authentication\Provider\AbstractProvider; |
| 12 | +use Neos\Flow\Security\Authentication\TokenInterface; |
| 13 | +use Neos\Flow\Security\Context; |
| 14 | +use Neos\Flow\Security\Cryptography\HashService; |
| 15 | +use Neos\Flow\Security\Exception\UnsupportedAuthenticationTokenException; |
| 16 | +use Yeebase\TwoFactorAuthentication\Security\Authentication\Token\TwoFactorUsernamePasswordToken; |
| 17 | +use Yeebase\TwoFactorAuthentication\Service\TwoFactorAuthenticationService; |
| 18 | + |
| 19 | +/** |
| 20 | + * An authentication provider that adds an additional layer of security by validating a Two-Factor-Authentication token. |
| 21 | + */ |
| 22 | +class TwoFactorAuthenticationProvider extends AbstractProvider |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var TwoFactorAuthenticationService |
| 26 | + * @Flow\Inject |
| 27 | + */ |
| 28 | + protected $twoFactorAuthenticationService; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var AccountRepository |
| 32 | + * @Flow\Inject |
| 33 | + */ |
| 34 | + protected $accountRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Context |
| 38 | + * @Flow\Inject |
| 39 | + */ |
| 40 | + protected $securityContext; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var HashService |
| 44 | + * @Flow\Inject |
| 45 | + */ |
| 46 | + protected $hashService; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var PersistenceManagerInterface |
| 50 | + * @Flow\Inject |
| 51 | + */ |
| 52 | + protected $persistenceManager; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var Bootstrap |
| 56 | + * @Flow\Inject |
| 57 | + */ |
| 58 | + protected $bootstrap; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var array |
| 62 | + * @Flow\InjectConfiguration("authenticationEntryPoint") |
| 63 | + */ |
| 64 | + protected $entryPointConfiguration; |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the class names of the tokens this provider can authenticate. |
| 68 | + * |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + public function getTokenClassNames() |
| 72 | + { |
| 73 | + return [TwoFactorUsernamePasswordToken::class]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks the given token for validity and sets the token authentication status |
| 78 | + * accordingly (success, wrong credentials or no credentials given). |
| 79 | + * |
| 80 | + * @param TokenInterface $authenticationToken The token to be authenticated |
| 81 | + * @return void |
| 82 | + * @throws UnsupportedAuthenticationTokenException |
| 83 | + */ |
| 84 | + public function authenticate(TokenInterface $authenticationToken) |
| 85 | + { |
| 86 | + if (! in_array(get_class($authenticationToken), $this->getTokenClassNames())) { |
| 87 | + throw new UnsupportedAuthenticationTokenException('This provider cannot authenticate the given token.', 1217339840); |
| 88 | + } |
| 89 | + |
| 90 | + $alreadyAuthenticated = false; |
| 91 | + if ($authenticationToken->getAuthenticationStatus() !== TokenInterface::AUTHENTICATION_SUCCESSFUL) { |
| 92 | + $authenticationToken->setAuthenticationStatus(TokenInterface::NO_CREDENTIALS_GIVEN); |
| 93 | + } else { |
| 94 | + $alreadyAuthenticated = true; |
| 95 | + } |
| 96 | + |
| 97 | + // Username-Password-Authentication |
| 98 | + |
| 99 | + $credentials = $authenticationToken->getCredentials(); |
| 100 | + if (!is_array($credentials) || !isset($credentials[TwoFactorUsernamePasswordToken::CREDENTIALS_USERNAME]) || !isset($credentials[TwoFactorUsernamePasswordToken::CREDENTIALS_PASSWORD])) { |
| 101 | + // no username/password credentials given -> authentication failed |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + $authenticationToken->setAuthenticationStatus(TokenInterface::WRONG_CREDENTIALS); |
| 106 | + |
| 107 | + $account = $this->retrieveAccountForToken($authenticationToken); |
| 108 | + $givenPassword = $credentials[TwoFactorUsernamePasswordToken::CREDENTIALS_PASSWORD]; |
| 109 | + |
| 110 | + if ($account === null) { |
| 111 | + // account for username not found -> authentication failed |
| 112 | + $this->hashService->validatePassword($givenPassword, 'bcrypt=>$2a$14$DummySaltToPreventTim,.ingAttacksOnThisProvider'); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + $existingPasswordHash = $this->twoFactorAuthenticationService->getPasswordCredentialsSource($account); |
| 117 | + if (! $this->hashService->validatePassword($givenPassword, $existingPasswordHash)) { |
| 118 | + // invalid password for given username -> authentication failed |
| 119 | + $account->authenticationAttempted(TokenInterface::WRONG_CREDENTIALS); |
| 120 | + $this->accountRepository->update($account); |
| 121 | + $this->persistenceManager->whitelistObject($account); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (! $this->twoFactorAuthenticationService->hasTwoFactorAuthenticationEnabled($account) || $alreadyAuthenticated) { |
| 126 | + // Two-Factor-Authentication is disabled or has been completed already -> authentication succeeded |
| 127 | + $account->authenticationAttempted(TokenInterface::AUTHENTICATION_SUCCESSFUL); |
| 128 | + $authenticationToken->setAuthenticationStatus(TokenInterface::AUTHENTICATION_SUCCESSFUL); |
| 129 | + $authenticationToken->setAccount($account); |
| 130 | + $this->accountRepository->update($account); |
| 131 | + $this->persistenceManager->whitelistObject($account); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // Authentication of Two-Factor-Authentication secret |
| 136 | + |
| 137 | + $authenticationToken->setAuthenticationStatus(TokenInterface::NO_CREDENTIALS_GIVEN); |
| 138 | + |
| 139 | + $twoFactorSecret = $authenticationToken->getCredentials()[TwoFactorUsernamePasswordToken::CREDENTIALS_TWO_FACTOR_SECRET]; |
| 140 | + if (empty($twoFactorSecret)) { |
| 141 | + // No secret given yet -> forward to insertSecret action |
| 142 | + $this->configureRedirectToInsertSecretAction(); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + $authenticationToken->setAuthenticationStatus(TokenInterface::WRONG_CREDENTIALS); |
| 147 | + |
| 148 | + if ($this->twoFactorAuthenticationService->validateSecret($twoFactorSecret, $account)) { |
| 149 | + // Secret evaluated correctly -> authentication succeeded |
| 150 | + $account->authenticationAttempted(TokenInterface::AUTHENTICATION_SUCCESSFUL); |
| 151 | + $authenticationToken->setAuthenticationStatus(TokenInterface::AUTHENTICATION_SUCCESSFUL); |
| 152 | + $authenticationToken->setAccount($account); |
| 153 | + } else { |
| 154 | + // Invalid secret -> authentication failed |
| 155 | + $account->authenticationAttempted(TokenInterface::WRONG_CREDENTIALS); |
| 156 | + } |
| 157 | + $this->accountRepository->update($account); |
| 158 | + $this->persistenceManager->whitelistObject($account); |
| 159 | + } |
| 160 | + |
| 161 | + protected function retrieveAccountForToken(TokenInterface $authenticationToken): ?Account |
| 162 | + { |
| 163 | + $account = null; |
| 164 | + |
| 165 | + $username = $authenticationToken->getCredentials()[TwoFactorUsernamePasswordToken::CREDENTIALS_USERNAME]; |
| 166 | + $providerName = $this->name; |
| 167 | + $accountRepository = $this->accountRepository; |
| 168 | + $this->securityContext->withoutAuthorizationChecks(function () use ($username, $providerName, $accountRepository, &$account) { |
| 169 | + $account = $accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($username, $providerName); |
| 170 | + }); |
| 171 | + |
| 172 | + return $account; |
| 173 | + } |
| 174 | + |
| 175 | + protected function configureRedirectToInsertSecretAction() |
| 176 | + { |
| 177 | + /* @var HttpRequestHandlerInterface $requestHandler */ |
| 178 | + $requestHandler = $this->bootstrap->getActiveRequestHandler(); |
| 179 | + $request = $requestHandler->getHttpRequest(); |
| 180 | + $response = $requestHandler->getHttpResponse(); |
| 181 | + |
| 182 | + $webRedirect = new WebRedirect(); |
| 183 | + $webRedirect->setOptions(['routeValues' => [ |
| 184 | + '@package' => $this->entryPointConfiguration['package'], |
| 185 | + '@controller' => $this->entryPointConfiguration['controller'], |
| 186 | + '@action' => $this->entryPointConfiguration['action'], |
| 187 | + '@format' => 'html' |
| 188 | + ]]); |
| 189 | + |
| 190 | + $webRedirect->startAuthentication($request, $response); |
| 191 | + } |
| 192 | +} |
0 commit comments