Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Call to function property_exists\\(\\) with CodeIgniter\\\\Shield\\\\Config\\\\Auth and \'userProvider\' will always evaluate to true\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Auth.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Auth.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in a ternary operator condition, CodeIgniter\\\\Shield\\\\Entities\\\\User\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Auth.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated function random_string\\(\\)\\:
The type \'basic\', \'md5\', and \'sha1\' are deprecated\\. They are not cryptographically secure\\.$#',
Expand Down
53 changes: 34 additions & 19 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\UserModel;

/**
* Facade for Authentication
*
* @method Result attempt(array $credentials)
* @method Result check(array $credentials)
* @method bool checkAction(string $token, string $type) [Session]
Expand All @@ -30,7 +33,8 @@ class Auth
*/
public const SHIELD_VERSION = '1.0.0-beta.7';

protected Authentication $authenticate;
protected AuthConfig $config;
protected ?Authentication $authenticate = null;

/**
* The Authenticator alias to use for this request.
Expand All @@ -39,9 +43,21 @@ class Auth

protected ?UserModel $userProvider = null;

public function __construct(Authentication $authenticate)
public function __construct(AuthConfig $config)
{
$this->authenticate = $authenticate->setProvider($this->getProvider());
$this->config = $config;
}

protected function ensureAuthentication(): void
{
if ($this->authenticate !== null) {
return;
}

$authenticate = new Authentication($this->config);
$authenticate->setProvider($this->getProvider());

$this->authenticate = $authenticate;
}

/**
Expand All @@ -51,7 +67,7 @@ public function __construct(Authentication $authenticate)
*/
public function setAuthenticator(?string $alias = null): self
{
if (! empty($alias)) {
if ($alias !== null) {
$this->alias = $alias;
}

Expand All @@ -63,6 +79,8 @@ public function setAuthenticator(?string $alias = null): self
*/
public function getAuthenticator(): AuthenticatorInterface
{
$this->ensureAuthentication();

return $this->authenticate
->factory($this->alias);
}
Expand All @@ -84,13 +102,15 @@ public function user(): ?User
*/
public function id()
{
return ($user = $this->user())
? $user->id
: null;
$user = $this->user();

return ($user !== null) ? $user->id : null;
}

public function authenticate(array $credentials): Result
{
$this->ensureAuthentication();

return $this->authenticate
->factory($this->alias)
->attempt($credentials);
Expand Down Expand Up @@ -133,22 +153,15 @@ public function getProvider(): UserModel
return $this->userProvider;
}

/** @var \CodeIgniter\Shield\Config\Auth $config */
$config = config('Auth');

if (! property_exists($config, 'userProvider')) {
throw AuthenticationException::forUnknownUserProvider();
}

$className = $config->userProvider;
$className = $this->config->userProvider;
$this->userProvider = new $className();

return $this->userProvider;
}

/**
* Provide magic function-access to Authenticators to save use
* from repeating code here, and to allow them have their
* from repeating code here, and to allow them to have their
* own, additional, features on top of the required ones,
* like "remember-me" functionality.
*
Expand All @@ -158,10 +171,12 @@ public function getProvider(): UserModel
*/
public function __call(string $method, array $args)
{
$authenticate = $this->authenticate->factory($this->alias);
$this->ensureAuthentication();

$authenticator = $this->authenticate->factory($this->alias);

if (method_exists($authenticate, $method)) {
return $authenticate->{$method}(...$args);
if (method_exists($authenticator, $method)) {
return $authenticator->{$method}(...$args);
}
}
}
14 changes: 7 additions & 7 deletions src/Authentication/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use CodeIgniter\Shield\Models\UserModel;

/**
* Factory for Authenticators.
*/
class Authentication
{
/**
Expand All @@ -26,13 +29,10 @@ public function __construct(AuthConfig $config)
}

/**
* Returns an instance of the specified Authenticator.
* Creates and returns the shared instance of the specified Authenticator.
*
* You can pass 'default' as the Authenticator and it
* will return an instance of the first Authenticator specified
* in the Auth config file.
*
* @param string|null $alias Authenticator alias
* @param string|null $alias Authenticator alias. Passing `null` returns the
* default authenticator.
*
* @throws AuthenticationException
*/
Expand Down Expand Up @@ -61,7 +61,7 @@ public function factory(?string $alias = null): AuthenticatorInterface
}

/**
* Sets the User provider to use
* Sets the User Provider to use.
*
* @return $this
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use CodeIgniter\Config\BaseService;
use CodeIgniter\Shield\Auth;
use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\JWTManager;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth as AuthConfig;

class Services extends BaseService
{
Expand All @@ -21,9 +21,10 @@ public static function auth(bool $getShared = true): Auth
return self::getSharedInstance('auth');
}

/** @var AuthConfig $config */
$config = config('Auth');

return new Auth(new Authentication($config));
return new Auth($config);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Helpers/auth_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ function auth(?string $alias = null): Auth

if (! function_exists('user_id')) {
/**
* Returns the ID for the current logged in user.
* Note: For \CodeIgniter\Shield\Entities\User this will always return an int.
* Returns the ID for the current logged-in user.
*
* @return int|string|null
*/
Expand Down