Skip to content

Commit 036edd8

Browse files
committed
fix: improve UX on OTP request
Change-Id: I5aa9e7be727dd52eba4f3debef1e9400698679df
1 parent a686682 commit 036edd8

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

app/Services/OAuth2/TokenService.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
**/
1414

1515
use App\Http\Utils\IUserIPHelperProvider;
16+
use App\Jobs\AddUserAction;
1617
use App\libs\Auth\Models\IGroupSlugs;
1718
use App\libs\OAuth2\Repositories\IOAuth2OTPRepository;
1819
use App\Models\OAuth2\Factories\OTPFactory;
@@ -73,6 +74,7 @@
7374
use Utils\Db\ITransactionService;
7475
use Utils\Exceptions\ConfigurationException;
7576
use Utils\Exceptions\UnacquiredLockException;
77+
use Utils\IPHelper;
7678
use utils\json_types\JsonValue;
7779
use utils\json_types\NumericDate;
7880
use utils\json_types\StringOrURI;
@@ -1587,6 +1589,21 @@ public function createOTPFromRequest(OAuth2PasswordlessAuthenticationRequest $re
15871589
$this->otp_repository->add($otp);
15881590
}
15891591

1592+
$user = $this->auth_service->getUserByUsername($otp->getUserName());
1593+
if(!is_null($user)){
1594+
Log::debug
1595+
(
1596+
sprintf
1597+
(
1598+
"TokenService::createOTPFromRequest requested OTP for existent user %s (%s)",
1599+
$user->getEmail(),
1600+
$user->getId()
1601+
)
1602+
);
1603+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), "Requested OTP");
1604+
if(!$user->isActive())
1605+
throw new ValidationException("User is not active.");
1606+
}
15901607
return $otp;
15911608
});
15921609

@@ -1610,10 +1627,26 @@ public function createOTPFromRequest(OAuth2PasswordlessAuthenticationRequest $re
16101627
* @throws Exception
16111628
*/
16121629
public function createOTPFromPayload(array $payload, ?Client $client):OAuth2OTP{
1630+
16131631
$otp = $this->tx_service->transaction(function() use($payload, $client){
16141632

16151633
$otp = OTPFactory::buildFromPayload($payload, $this->identifier_generator, $client);
16161634

1635+
$user = $this->auth_service->getUserByUsername($otp->getUserName());
1636+
if(!is_null($user)){
1637+
Log::debug
1638+
(
1639+
sprintf
1640+
(
1641+
"TokenService::createOTPFromPayload requested OTP for existent user %s (%s)",
1642+
$user->getEmail(),
1643+
$user->getId()
1644+
)
1645+
);
1646+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), "Requested OTP");
1647+
if(!$user->isActive())
1648+
throw new ValidationException("User is not active.");
1649+
}
16171650
if(is_null($client)){
16181651
$this->otp_repository->add($otp);
16191652
}

app/libs/OAuth2/GrantTypes/PasswordlessGrantType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Exception;
1616
use Illuminate\Support\Facades\Auth;
17+
use models\exceptions\ValidationException;
1718
use Models\OAuth2\Client;
1819
use Models\OAuth2\OAuth2OTP;
1920
use OAuth2\Exceptions\InvalidApplicationType;
@@ -325,6 +326,13 @@ public function handle(OAuth2Request $request)
325326
$this->memento_service->forget();
326327
return new OAuth2DirectErrorResponse($ex->getError(), $ex->getMessage());
327328
}
329+
catch(ValidationException $ex){
330+
$this->log_service->warning($ex);
331+
// clear save data ...
332+
$this->auth_service->clearUserAuthorizationResponse();
333+
$this->memento_service->forget();
334+
return new OAuth2DirectErrorResponse($ex->getMessage());
335+
}
328336
catch (Exception $ex) {
329337
$this->log_service->error($ex);
330338
// clear save data ...

resources/js/login/login.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,12 @@ class LoginPage extends React.Component {
444444
});
445445
}, (error) => {
446446
let {response, status, message} = error;
447+
if(status == 412){
448+
const {message, errors} = response.body;
449+
Swal(message, errors[0], 'error')
450+
return;
451+
}
452+
447453
Swal('Oops...', 'Something went wrong!', 'error')
448454
});
449455
return false;

0 commit comments

Comments
 (0)