Skip to content

Commit

Permalink
fix: impossible to login after incorrect otp entry when turnstile act…
Browse files Browse the repository at this point in the history
…ive (#26)

* fix: impossible to login after incorrect otp entry when turnstile active

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
imorland and StyleCIBot authored Feb 6, 2024
1 parent 9dcc2bc commit b8d70f8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"name": "fas fa-shield-alt",
"color": "#fff",
"backgroundColor": "#0072e3"
}
},
"optional-dependencies": [
"blomstra/turnstile"
]
},
"flarum-cli": {
"modules": {
Expand Down Expand Up @@ -93,6 +96,7 @@
"fof/oauth": "*",
"flarum/phpstan": "^1.8",
"blomstra/gdpr": "@beta",
"sycho/flarum-private-facade": "^0.1.16"
"sycho/flarum-private-facade": "^0.1.16",
"blomstra/turnstile": "*"
}
}
14 changes: 11 additions & 3 deletions src/Api/Controller/CreateTwoFactorTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ class CreateTwoFactorTokenController implements RequestHandlerInterface
{
use TwoFactorAuthenticationTrait;

public function __construct(protected TotpInterface $totp, protected UserRepository $users, protected BusDispatcher $bus, protected EventDispatcher $events)
{
public function __construct(
protected TotpInterface $totp,
protected UserRepository $users,
protected BusDispatcher $bus,
protected EventDispatcher $events
) {
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -50,9 +54,13 @@ public function handle(ServerRequestInterface $request): ResponseInterface
if ($this->twoFactorActive($user)) {
$token = $this->retrieveTwoFactorTokenFrom(Arr::get($body, 'twoFactorToken'));

if (! $this->isTokenActive($token, $user)) {
if (! $token) {
throw new ValidationException(['twoFactorToken' => 'two_factor_required']);
}

if (! $this->isTokenActive($token, $user)) {
throw new ValidationException(['twoFactorToken' => 'two_factor_incorrect']);
}
}

if (Arr::get($body, 'remember')) {
Expand Down
33 changes: 23 additions & 10 deletions src/Api/Controller/TwoFactorLogInController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,45 @@

namespace IanM\TwoFactor\Api\Controller;

use Flarum\Api\Client;
use Flarum\Extension\ExtensionManager;
use Flarum\Forum\Controller\LogInController;
use Flarum\Forum\LogInValidator;
use Flarum\Http\AccessToken;
use Flarum\Http\RememberAccessToken;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
use Flarum\User\Event\LoggedIn;
use Flarum\User\UserRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;

class TwoFactorLogInController extends LogInController
{
public function __construct(
UserRepository $users,
Client $apiClient,
SessionAuthenticator $authenticator,
Dispatcher $events,
Rememberer $rememberer,
LogInValidator $validator,
protected ExtensionManager $extensions
) {
parent::__construct($users, $apiClient, $authenticator, $events, $rememberer, $validator);
}

public function handle(Request $request): ResponseInterface
{
$body = $request->getParsedBody();
$identification = Arr::get($body, 'identification');
$password = Arr::get($body, 'password');
$remember = Arr::get($body, 'remember');
$twoFactorToken = Arr::get($body, 'twoFactorToken');

$this->validator->assertValid($body);
if (! $this->extensions->isEnabled('blomstra-turnstile') && empty(Arr::get($body, 'twoFactorToken'))) {
$this->validator->assertValid($body);
}

$response = $this->apiClient->withParentRequest($request)
->withBody([
'identification' => $identification,
'password' => $password,
'remember' => $remember,
'twoFactorToken' => $twoFactorToken])
->withBody($body)
->post('/token');

if ($response->getStatusCode() === 200) {
Expand Down

0 comments on commit b8d70f8

Please sign in to comment.