Skip to content

Commit

Permalink
Applied doctrine/coding-standard:^10 ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Aug 29, 2022
1 parent 981ea8f commit 49ca12e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 121 deletions.
4 changes: 2 additions & 2 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
$sessionMiddleware = new SessionMiddleware(
Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText('c9UA8QKLSmDEn4DhNeJIad/4JugZd/HvrjyKrS0jOes=') // // signature key (important: change this to your own)
InMemory::plainText('c9UA8QKLSmDEn4DhNeJIad/4JugZd/HvrjyKrS0jOes='), // // signature key (important: change this to your own)
),
SetCookie::create('an-example-cookie-name')
->withSecure(false) // false on purpose, unless you have https locally
->withHttpOnly(true)
->withPath('/'),
1200, // 20 minutes
new SystemClock(new DateTimeZone(date_default_timezone_get()))
new SystemClock(new DateTimeZone(date_default_timezone_get())),
);

$myMiddleware = new class implements RequestHandlerInterface {
Expand Down
53 changes: 20 additions & 33 deletions src/Storageless/Http/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,17 @@ final class SessionMiddleware implements MiddlewareInterface

private Configuration $config;

private int $expirationTime;

private int $refreshTime;

private SetCookie $defaultCookie;

private Clock $clock;

public function __construct(
Configuration $configuration,
SetCookie $defaultCookie,
int $expirationTime,
Clock $clock,
int $refreshTime = self::DEFAULT_REFRESH_TIME
private int $expirationTime,
private Clock $clock,
private int $refreshTime = self::DEFAULT_REFRESH_TIME,
) {
$this->config = $configuration;
$this->defaultCookie = clone $defaultCookie;
$this->expirationTime = $expirationTime;
$this->clock = $clock;
$this->refreshTime = $refreshTime;
$this->config = $configuration;
$this->defaultCookie = clone $defaultCookie;
}

/**
Expand All @@ -88,11 +79,11 @@ public static function fromSymmetricKeyDefaults(Signer\Key $symmetricKey, int $e
return new self(
Configuration::forSymmetricSigner(
new Signer\Hmac\Sha256(),
$symmetricKey
$symmetricKey,
),
self::buildDefaultCookie(),
$expirationTime,
new SystemClock(new DateTimeZone(date_default_timezone_get()))
new SystemClock(new DateTimeZone(date_default_timezone_get())),
);
}

Expand All @@ -103,17 +94,17 @@ public static function fromSymmetricKeyDefaults(Signer\Key $symmetricKey, int $e
public static function fromRsaAsymmetricKeyDefaults(
Signer\Key $privateRsaKey,
Signer\Key $publicRsaKey,
int $expirationTime
int $expirationTime,
): self {
return new self(
Configuration::forAsymmetricSigner(
new Signer\Rsa\Sha256(),
$privateRsaKey,
$publicRsaKey
$publicRsaKey,
),
self::buildDefaultCookie(),
$expirationTime,
new SystemClock(new DateTimeZone(date_default_timezone_get()))
new SystemClock(new DateTimeZone(date_default_timezone_get())),
);
}

Expand Down Expand Up @@ -142,14 +133,14 @@ public function process(Request $request, RequestHandlerInterface $handler): Res
return $this->appendToken(
$sessionContainer,
$handler->handle($request->withAttribute(self::SESSION_ATTRIBUTE, $sessionContainer)),
$token
$token,
);
}

/**
* Extract the token from the given request object
*/
private function parseToken(Request $request): ?UnencryptedToken
private function parseToken(Request $request): UnencryptedToken|null
{
/** @var array<string, string> $cookies */
$cookies = $request->getCookieParams();
Expand Down Expand Up @@ -181,20 +172,18 @@ private function parseToken(Request $request): ?UnencryptedToken
return $token;
}

/**
* @throws OutOfBoundsException
*/
private function extractSessionContainer(?UnencryptedToken $token): SessionInterface
/** @throws OutOfBoundsException */
private function extractSessionContainer(UnencryptedToken|null $token): SessionInterface
{
if (! $token) {
return DefaultSessionData::newEmptySession();
}

try {
return DefaultSessionData::fromDecodedTokenData(
(object) $token->claims()->get(self::SESSION_CLAIM, new stdClass())
(object) $token->claims()->get(self::SESSION_CLAIM, new stdClass()),
);
} catch (BadMethodCallException $invalidToken) {
} catch (BadMethodCallException) {
return DefaultSessionData::newEmptySession();
}
}
Expand All @@ -203,7 +192,7 @@ private function extractSessionContainer(?UnencryptedToken $token): SessionInter
* @throws BadMethodCallException
* @throws InvalidArgumentException
*/
private function appendToken(SessionInterface $sessionContainer, Response $response, ?Token $token): Response
private function appendToken(SessionInterface $sessionContainer, Response $response, Token|null $token): Response
{
$sessionContainerChanged = $sessionContainer->hasChanged();

Expand All @@ -218,15 +207,13 @@ private function appendToken(SessionInterface $sessionContainer, Response $respo
return $response;
}

private function shouldTokenBeRefreshed(?Token $token): bool
private function shouldTokenBeRefreshed(Token|null $token): bool
{
return $token !== null
&& $token->hasBeenIssuedBefore($this->clock->now()->sub(new DateInterval(sprintf('PT%sS', $this->refreshTime))));
}

/**
* @throws BadMethodCallException
*/
/** @throws BadMethodCallException */
private function getTokenCookie(SessionInterface $sessionContainer): SetCookie
{
$now = $this->clock->now();
Expand All @@ -241,7 +228,7 @@ private function getTokenCookie(SessionInterface $sessionContainer): SetCookie
->expiresAt($expiresAt)
->withClaim(self::SESSION_CLAIM, $sessionContainer)
->getToken($this->config->signer(), $this->config->signingKey())
->toString()
->toString(),
)
->withExpires($expiresAt);
}
Expand Down
18 changes: 4 additions & 14 deletions src/Storageless/Session/DefaultSessionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@ final class DefaultSessionData implements SessionInterface
{
private const DEFAULT_JSON_DECODE_DEPTH = 512;

/** @var array<string, int|bool|string|float|mixed[]|null> */
private array $data;

/** @var array<string, int|bool|string|float|mixed[]|null> */
private array $originalData;

/**
* @param array<string, int|bool|string|float|mixed[]|null> $data
* @param array<string, int|bool|string|float|mixed[]|null> $originalData
*/
private function __construct(
array $data,
array $originalData
private array $data,
private array $originalData,
) {
$this->data = $data;
$this->originalData = $originalData;
}

public static function fromDecodedTokenData(object $data): self
Expand All @@ -59,9 +51,7 @@ public static function fromDecodedTokenData(object $data): self
return new self($arrayShapedData, $arrayShapedData);
}

/**
* @param array<int|bool|string|float|mixed[]|object|JsonSerializable|null> $data
*/
/** @param array<int|bool|string|float|mixed[]|object|JsonSerializable|null> $data */
public static function fromTokenData(array $data): self
{
$instance = new self([], []);
Expand Down Expand Up @@ -147,7 +137,7 @@ private static function convertValueToScalar(int|bool|string|float|array|object|
json_encode($value, JSON_PRESERVE_ZERO_FRACTION | JSON_THROW_ON_ERROR),
true,
self::DEFAULT_JSON_DECODE_DEPTH,
JSON_THROW_ON_ERROR
JSON_THROW_ON_ERROR,
);

return $decoded;
Expand Down
2 changes: 1 addition & 1 deletion src/Storageless/Session/LazySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class LazySession implements SessionInterface
{
/** @internal do not access directly: use {@see LazySession::getRealSession} instead */
private ?SessionInterface $realSession = null;
private SessionInterface|null $realSession = null;

/**
* @var callable
Expand Down
Loading

0 comments on commit 49ca12e

Please sign in to comment.