Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Throw error if 2FA is not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
binal-7span committed Nov 8, 2019
1 parent 23cfac4 commit 5b4b21a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
class TFAEnforcedException extends UnauthorizedException
{
const ERROR_CODE = 113;
const ERROR_MESSAGE = "2FA enforced but not activated for user";

public function __construct()
{
parent::__construct('2FA enforced but not activated for user');
parent::__construct(ERROR_MESSAGE);
}
}
5 changes: 1 addition & 4 deletions src/core/Directus/Authentication/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,14 @@ public function getUserProvider()
*
* @return string
*/
public function generateAuthToken(UserInterface $user, $needs2FA = false)
public function generateAuthToken(UserInterface $user)
{
$payload = [
'id' => (int) $user->getId(),
// 'group' => $user->getGroupId(),
'exp' => $this->getNewExpirationTime()
];

if ($needs2FA == true) {
$payload['needs2FA'] = true;
}

return $this->generateToken(JWTUtils::TYPE_AUTH, $payload);
}
Expand Down
23 changes: 17 additions & 6 deletions src/core/Directus/Services/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Directus\Authentication\Exception\InvalidResetPasswordTokenException;
use Directus\Authentication\Exception\UserNotFoundException;
use Directus\Authentication\Exception\UserWithEmailNotFoundException;
use Directus\Authentication\Exception\TFAEnforcedException;
use Directus\Authentication\Sso\OneSocialProvider;
use Directus\Authentication\Provider;
use Directus\Authentication\Sso\Social;
Expand Down Expand Up @@ -73,16 +74,26 @@ public function loginWithCredentials($email, $password, $otp=null, $mode = null)
break;
case DirectusUserSessionsTableGateway::TOKEN_JWT :
default :
$needs2FA = $tfa_enforced && $user->get2FASecret() == null;
$token = $this->generateAuthToken($user,$needs2FA);
$token = $this->generateAuthToken($user);
$user = $user->toArray();
$responseData = [
'token' => $token,
'user' => $user->toArray()
'user' => $user
];

}
return [
'data' => $responseData
];
$responseObject['data'] = $responseData;

if(!is_null($user)){
$needs2FA = $tfa_enforced && $user['2fa_secret'] == null;
if($needs2FA){
$responseObject['error'] = [
'code' => TFAEnforcedException::ERROR_CODE,
'message' => TFAEnforcedException::ERROR_MESSAGE
];
}
}
return $responseObject;
}

/**
Expand Down

0 comments on commit 5b4b21a

Please sign in to comment.