Skip to content

Commit

Permalink
PHP 8.2+
Browse files Browse the repository at this point in the history
Updated all dependencies
  • Loading branch information
jasny committed Aug 30, 2024
1 parent 0d90f89 commit 459003a
Show file tree
Hide file tree
Showing 31 changed files with 214 additions and 207 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ jobs:
fail-fast: false
matrix:
include:
- php: 8.1
- php: 8.2
composer: '--prefer-lowest'
desc: "Lowest versions"
- php: 8.1
- php: 8.2
coverage: '--coverage-clover /tmp/clover.xml'
- php: 8.3
Expand Down Expand Up @@ -47,4 +46,3 @@ jobs:
if: ${{ matrix.coverage }}
with:
cli-args: "--format=php-clover build/logs/clover.xml --revision=${{ github.event.pull_request.head.sha || github.sha }}"

15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": ">=8.1.0",
"php": ">=8.2.0",
"improved/iterable": "^0.1.4",
"jasny/immutable": "^2.1",
"nesbot/carbon": "^2.27",
Expand All @@ -29,17 +29,18 @@
"psr/log": "^1.1"
},
"conflict": {
"hashids/hashids": "< 2.0",
"lcobucci/jwt": "< 3.4"
"hashids/hashids": "< 4.1",
"lcobucci/jwt": "< 4.0"
},
"require-dev": {
"ext-bcmath": "*",
"phpunit/phpunit": "^11.3",
"hashids/hashids": "^4.1 | ^5.0",
"jasny/phpunit-extension": "^0.5.1",
"lcobucci/clock": "^3.2",
"lcobucci/jwt": "^4.0 | ^5.0",
"phpstan/phpstan": "^1.12.0",
"squizlabs/php_codesniffer": "^3.10",
"hashids/hashids": "^2.0 | ^3.0 | ^4.0",
"lcobucci/jwt": "^3.4 | ^4.0"
"phpunit/phpunit": "^11.3",
"squizlabs/php_codesniffer": "^3.10"
},
"config": {
"optimize-autoloader": true,
Expand Down
5 changes: 0 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage>
<report>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<testsuites>
<testsuite name="tests">
<directory>tests/</directory>
Expand Down
60 changes: 31 additions & 29 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Jasny\Auth;

use Closure;
use DateTimeImmutable;
use DateTimeInterface;
use Jasny\Auth\AuthzInterface as Authz;
use Jasny\Auth\Confirmation\ConfirmationInterface as Confirmation;
use Jasny\Auth\Confirmation\NoConfirmation;
Expand All @@ -14,9 +17,11 @@
use Jasny\Auth\User\PartiallyLoggedIn;
use Jasny\Auth\UserInterface as User;
use Jasny\Immutable;
use LogicException;
use Psr\EventDispatcher\EventDispatcherInterface as EventDispatcher;
use Psr\Log\LoggerInterface as Logger;
use Psr\Log\NullLogger;
use RuntimeException;

/**
* Authentication and authorization.
Expand All @@ -35,7 +40,7 @@ class Auth implements Authz
/**
* Time when logged in.
*/
protected ?\DateTimeInterface $timestamp = null;
protected ?DateTimeInterface $timestamp = null;

protected Session $session;
protected Storage $storage;
Expand All @@ -47,8 +52,8 @@ class Auth implements Authz
/** Allow service to be re-initialized */
protected bool $forMultipleRequests = false;

/** @var \Closure&callable(User $user, string $code):bool */
protected \Closure $verifyMfa;
/** @var Closure&callable(User $user, string $code):bool */
protected Closure $verifyMfa;

/**
* Auth constructor.
Expand All @@ -70,23 +75,23 @@ public function __construct(Authz $authz, Storage $storage, ?Confirmation $confi
*
* @return static
*/
public function forMultipleRequests(): self
public function forMultipleRequests(): static
{
return $this->withProperty('forMultipleRequests', true);
}

/**
* Get a copy with an event dispatcher.
*/
public function withEventDispatcher(EventDispatcher $dispatcher): self
public function withEventDispatcher(EventDispatcher $dispatcher): static
{
return $this->withProperty('dispatcher', $dispatcher);
}

/**
* Get a copy with a logger.
*/
public function withLogger(Logger $logger): self
public function withLogger(Logger $logger): static
{
return $this->withProperty('logger', $logger);
}
Expand All @@ -100,14 +105,14 @@ public function getLogger(): Logger
}

/**
* Get a copy of the service with Multi Factor Authentication (MFA) support.
* Get a copy of the service with Multi-Factor Authentication (MFA) support.
*
* @param callable $verify Callback to verify MFA.
* @return static
*/
public function withMfa(callable $verify): self
{
return $this->withProperty('verifyMfa', \Closure::fromCallable($verify));
return $this->withProperty('verifyMfa', $verify(...));
}


Expand All @@ -118,7 +123,7 @@ public function initialize(?Session $session = null): void
{
if ($this->isInitialized()) {
if (!$this->forMultipleRequests) {
throw new \LogicException("Auth service is already initialized");
throw new LogicException("Auth service is already initialized");
}

$this->authz = $this->authz()->forUser(null)->inContextOf(null);
Expand All @@ -133,7 +138,7 @@ public function initialize(?Session $session = null): void
/**
* Get user and context from session, loading objects from storage.
*
* @return array{user:User|null,context:Context|null,timestamp:\DateTimeInterface|null}
* @return array{user:User|null,context:Context|null,timestamp:DateTimeInterface|null}
*/
protected function getInfoFromSession(): array
{
Expand All @@ -145,7 +150,7 @@ protected function getInfoFromSession(): array
if ($uid === null || $uid instanceof User) {
$user = $uid;
} else {
if (substr($uid, 0, 9) === '#partial:') {
if (str_starts_with($uid, '#partial:')) {
$partial = true;
$uid = substr($uid, 9);
}
Expand Down Expand Up @@ -185,12 +190,12 @@ public function isInitialized(): bool
/**
* Throw an exception if the service hasn't been initialized yet.
*
* @throws \LogicException
* @throws LogicException
*/
protected function assertInitialized(): void
{
if (!$this->isInitialized()) {
throw new \LogicException("Auth needs to be initialized before use");
throw new LogicException("Auth needs to be initialized before use");
}
}

Expand All @@ -217,7 +222,7 @@ final public function isLoggedIn(): bool

/**
* Check if the current user is partially logged in.
* Typically this means MFA verification is required.
* Typically, this means MFA verification is required.
*/
final public function isPartiallyLoggedIn(): bool
{
Expand Down Expand Up @@ -274,7 +279,7 @@ final public function context(): ?Context
/**
* Get the login timestamp.
*/
public function time(): ?\DateTimeInterface
public function time(): ?DateTimeInterface
{
return $this->timestamp;
}
Expand All @@ -290,7 +295,7 @@ public function loginAs(User $user): void
$this->assertInitialized();

if ($this->authz->isLoggedIn()) {
throw new \LogicException("Already logged in");
throw new LogicException("Already logged in");
}

if ($user->requiresMfa()) {
Expand All @@ -311,7 +316,7 @@ public function login(string $username, string $password): void
$this->assertInitialized();

if ($this->authz->isLoggedIn()) {
throw new \LogicException("Already logged in");
throw new LogicException("Already logged in");
}

$user = $this->storage->fetchUserByUsername($username);
Expand Down Expand Up @@ -339,8 +344,8 @@ public function login(string $username, string $password): void
*/
private function loginUser(User $user): void
{
$event = new Event\Login($this, $user);
$this->dispatcher->dispatch($event);
/** @var Event\Login $event */
$event = $this->dispatcher->dispatch(new Event\Login($this, $user));

if ($event->isCancelled()) {
if ($this->isPartiallyLoggedIn()) {
Expand All @@ -361,7 +366,7 @@ private function loginUser(User $user): void
$this->authz = $this->authz->inContextOf($context);
}

$this->timestamp = new \DateTimeImmutable();
$this->timestamp = new DateTimeImmutable();
$this->updateSession();

$this->logger->info("Login successful", ['user' => $user->getAuthId()]);
Expand All @@ -374,8 +379,8 @@ private function loginUser(User $user): void
*/
private function partialLoginUser(User $user): void
{
$event = new Event\PartialLogin($this, $user);
$this->dispatcher->dispatch($event);
/** @var Event\PartialLogin $event */
$event = $this->dispatcher->dispatch(new Event\PartialLogin($this, $user));

if ($event->isCancelled()) {
$this->logger->info("Login failed: " . $event->getCancellationReason(), ['user' => $user->getAuthId()]);
Expand All @@ -384,7 +389,7 @@ private function partialLoginUser(User $user): void

// Beware; the `authz` property may have been changed via the partial login event.
$this->authz = $this->authz->forUser(new PartiallyLoggedIn($user));
$this->timestamp = new \DateTimeImmutable();
$this->timestamp = new DateTimeImmutable();
$this->updateSession();

$this->logger->info("Partial login", ['user' => $user->getAuthId()]);
Expand All @@ -398,7 +403,7 @@ public function mfa(string $code): void
$this->assertInitialized();

if ($this->isLoggedOut()) {
throw new \RuntimeException("Unable to perform MFA verification: No user (partially) logged in");
throw new RuntimeException("Unable to perform MFA verification: No user (partially) logged in");
}

$authzUser = $this->user();
Expand Down Expand Up @@ -462,7 +467,7 @@ public function setContext(?Context $context): void
*
* @return $this
*/
public function recalc(): self
public function recalc(): static
{
$this->authz = $this->authz->recalc();
$this->updateSession();
Expand All @@ -486,13 +491,12 @@ protected function updateSession(): void
$context = $this->authz->context();

$uid = $user->getAuthId();
$cid = $context !== null ? $context->getAuthId() : null;
$cid = $context?->getAuthId();
$checksum = $user->getAuthChecksum();

$this->session->persist($uid, $cid, $checksum, $this->timestamp);
}


/**
* Return read-only service for authorization of the current user and context.
*/
Expand Down Expand Up @@ -525,7 +529,6 @@ final public function outOfContext(): Authz
return $this->inContextOf(null);
}


/**
* Get service to create or validate confirmation token.
*/
Expand All @@ -537,7 +540,6 @@ public function confirm(string $subject): Confirmation
->withSubject($subject);
}


/**
* Create an event dispatcher as null object.
* @codeCoverageIgnore
Expand Down
4 changes: 3 additions & 1 deletion src/AuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Jasny\Auth;

use RuntimeException;

/**
* Authentication exception.
*/
class AuthException extends \RuntimeException
class AuthException extends RuntimeException
{
}
Loading

0 comments on commit 459003a

Please sign in to comment.