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

Commit

Permalink
Merge branch 'develop' into webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkvanzanten committed Nov 12, 2019
2 parents f51c51a + 41fd0b6 commit be83925
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 58 deletions.
7 changes: 7 additions & 0 deletions migrations/db/schemas/20180220023248_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public function change()
'default' => null
]);

$table->addColumn('2fa_secret', 'string', [
'limit' => 100,
'encoding' => 'utf8',
'null' => true,
'default' => null
]);

$table->addIndex('email', [
'unique' => true,
'name' => 'idx_users_email'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function change()
]);

$table->addColumn('token', 'string', [
'limit' => 255,
'limit' => 520,
'encoding' => 'utf8',
'null' => true,
'default' => null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function change()
]);

$table->addColumn('token', 'string', [
'limit' => 255,
'limit' => 520,
'encoding' => 'utf8',
'null' => true,
'default' => null
Expand Down
2 changes: 1 addition & 1 deletion public/extensions/core/auth/facebook/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/extensions/core/auth/github/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/extensions/core/auth/google/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/extensions/core/auth/okta/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/extensions/core/auth/twitter/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __invoke(Request $request, Response $response, callable $next)

try {
$user = $this->authenticate($request);

$hookEmitter = $this->container->get('hook_emitter');
if (!$user && !$publicRoleId) {
$exception = new UserNotAuthenticatedException();
Expand Down Expand Up @@ -110,7 +110,7 @@ public function __invoke(Request $request, Response $response, callable $next)
$hookEmitter->run('auth.fail', [$exception]);
throw $exception;
}

// TODO: Adding an user should auto set its ID and GROUP
// TODO: User data should be casted to its data type
// TODO: Make sure that the group is not empty
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function authenticate(Request $request)

$user = $authService->authenticateWithToken($authToken, $request->getAttribute('ignore_origin'));
}

return $user;
}

Expand Down Expand Up @@ -226,8 +226,11 @@ protected function targetIsUserEdit(Request $request, int $id) {

if ($num_elements > 3
&&$target_array[$num_elements - 3] == 'users'
&& $target_array[$num_elements - 2] == strval($id)
&& $target_array[$num_elements - 1] == 'activate2FA') {
&& (
$target_array[$num_elements - 2] == strval($id) ||
$target_array[$num_elements - 2] == 'me'
)
&& $target_array[$num_elements - 1] == 'activate_2fa') {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Directus\Authentication\Exception;

use Directus\Exception\UnauthorizedException;

class SsoNotAllowedException extends UnauthorizedException
{
const ERROR_CODE = 115;

public function __construct()
{
parent::__construct('SSO not allowed with 2FA enabled');
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class UnknownProjectException extends ErrorException
{
const ERROR_CODE = 22;
const ERROR_CODE = 24;

public function __construct($project, $previous = null)
{
Expand Down
31 changes: 20 additions & 11 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 All @@ -95,7 +106,7 @@ public function findOrCreateStaticToken(&$user)
{
$user = $user->toArray();
if(empty($user['token'])){
$token = StringUtils::randomString(6,false);
$token = StringUtils::randomString(24,false);
$userTable = $this->createTableGateway(SchemaManager::COLLECTION_USERS, false);
$Update = new Update(SchemaManager::COLLECTION_USERS);
$Update->set(['token' => $token]);
Expand Down Expand Up @@ -342,16 +353,14 @@ public function authenticateWithSsoRequestToken($token)
*
* @param UserInterface $user
*
* @param bool $needs2FA Whether the user needs 2FA
*
* @return string
*/
public function generateAuthToken(UserInterface $user, bool $needs2FA = false)
public function generateAuthToken(UserInterface $user)
{
/** @var Provider $auth */
$auth = $this->container->get('auth');

return $auth->generateAuthToken($user, $needs2FA);
return $auth->generateAuthToken($user);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/Directus/Services/ProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function create(array $data)

$this->validate($data,[
'project' => 'required|string|regex:/^[0-9a-z_-]+$/i',

'private' => 'bool',
'force' => 'bool',
'existing' => 'bool',
'super_admin_token' => 'required',
Expand Down
4 changes: 2 additions & 2 deletions src/core/Directus/Services/UsersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ protected function enforceLastAdmin($id)
public function activate2FA($id, $tfa_secret, $otp)
{
$this->validate(
['tfa_secret' => $tfa_secret, 'otp' => $otp],
['tfa_secret' => 'required|string', 'otp' => 'required|string']
['2fa_secret' => $tfa_secret, 'otp' => $otp],
['2fa_secret' => 'required|string', 'otp' => 'required|string']
);

$ga = new Google2FA();
Expand Down
6 changes: 5 additions & 1 deletion src/core/Directus/Services/UtilsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function generate2FASecret()
{
$ga = new Google2FA();
$tfa_secret = $ga->generateSecretKey();
return ['2fa_secret' => $tfa_secret];
return [
'data' => [
'2fa_secret' => $tfa_secret
]
];
}
}
3 changes: 3 additions & 0 deletions src/core/Directus/Util/DateTimeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public static function nowInTimezone()
if(!is_null($projectName)){
$config = get_project_config($projectName);
return static::now($config->get('app.timezone'));
} else {
// If there's no project config (f.e. when creating projects), default to UTC
return static::now('UTC');
}
}

Expand Down
Loading

0 comments on commit be83925

Please sign in to comment.