Skip to content
Open
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
6 changes: 4 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ jobs:
strategy:
matrix:
php-version:
- "8.4"
- "8.3"
- "8.2"
- "8.1"
- "8.0"

steps:
- uses: actions/checkout@v4
Expand All @@ -32,5 +33,6 @@ jobs:
with:
folder: php
project: ${{ github.event.repository.name }}
secrets: inherit
secrets:
DOC_TOKEN: ${{ secrets.DOC_TOKEN }}

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
composer.lock
vendor
/.phpunit.result.cache
phpunit.coverage.xml
phpunit.report.xml
*.bak
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"byjg/jwt-wrapper": "4.9.*"
"php": ">=8.1 <8.5",
"byjg/jwt-wrapper": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*|^9.6"
"phpunit/phpunit": "^10|^11",
"vimeo/psalm": "^5.9|^6.12"
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm"
},
"license": "MIT"
}
27 changes: 17 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnPhpunitDeprecations="true"
failOnWarning="true"
failOnNotice="true"
failOnDeprecation="true"
failOnPhpunitDeprecation="true"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">

<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<filter>
<whitelist>
<source>
<include>
<directory>./src</directory>
</whitelist>
</filter>
</include>
</source>

<testsuites>
<testsuite name="Test Suite">
Expand Down
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
cacheDirectory="/tmp/psalm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">

<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
</psalm>
28 changes: 17 additions & 11 deletions src/JwtSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace ByJG\Session;

use ByJG\Util\JwtWrapper;
use ByJG\Util\JwtWrapperException;
use ByJG\JwtWrapper\JwtWrapper;
use ByJG\JwtWrapper\JwtWrapperException;
use Exception;
use SessionHandlerInterface;

Expand Down Expand Up @@ -64,6 +64,7 @@ protected function replaceSessionHandler(): void
* </p>
* @since 5.4.0
*/
#[\Override]
public function close(): bool
{
return true;
Expand All @@ -80,14 +81,15 @@ public function close(): bool
* </p>
* @since 5.4.0
*/
#[\Override]
public function destroy(string $id): bool
{
if (!headers_sent()) {
setcookie(
self::COOKIE_PREFIX . $this->sessionConfig->getSessionContext(),
"",
(time()-3000),
$this->sessionConfig->getCookiePath() ?? "",
$this->sessionConfig->getCookiePath(),
$this->sessionConfig->getCookieDomain() ?? "",
);
}
Expand All @@ -99,19 +101,20 @@ public function destroy(string $id): bool
* Cleanup old sessions
*
* @link http://php.net/manual/en/sessionhandlerinterface.gc.php
*
* @param int $max_lifetime <p>
* Sessions that have not updated for
* the last maxlifetime seconds will be removed.
* </p>
* @return int|false <p>
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
* </p>
*
* @return int|false <p> The return value (usually TRUE on success, FALSE on failure). Note this value is returned internally to PHP for processing. </p>
*
* @since 5.4.0
*/
#[\Override]
public function gc(int $max_lifetime): int|false
{
return true;
return 1;
}

/**
Expand All @@ -126,6 +129,7 @@ public function gc(int $max_lifetime): int|false
* </p>
* @since 5.4.0
*/
#[\Override]
public function open(string $path, string $name): bool
{
return true;
Expand All @@ -143,6 +147,7 @@ public function open(string $path, string $name): bool
* </p>
* @since 5.4.0
*/
#[\Override]
public function read(string $id): string
{
try {
Expand Down Expand Up @@ -184,21 +189,22 @@ public function read(string $id): string
* @throws JwtWrapperException
* @since 5.4.0
*/
#[\Override]
public function write(string $id, string $data): bool
{
$jwt = new JwtWrapper(
$this->sessionConfig->getServerName(),
$this->sessionConfig->getKey()
);
$session_data = $jwt->createJwtData($data, $this->sessionConfig->getTimeoutMinutes() * 60);
$session_data = $jwt->createJwtData(['data' => $data], $this->sessionConfig->getTimeoutMinutes() * 60, 0, null);
$token = $jwt->generateToken($session_data);

if (!headers_sent()) {
setcookie(
self::COOKIE_PREFIX . $this->sessionConfig->getSessionContext(),
$token,
(time()+$this->sessionConfig->getTimeoutMinutes()*60) ,
$this->sessionConfig->getCookiePath() ?? "",
$this->sessionConfig->getCookiePath(),
$this->sessionConfig->getCookieDomain() ?? "",
false,
true
Expand Down Expand Up @@ -236,7 +242,7 @@ public function unSerializeSessionData($session_data): array
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session_data, $offset));
$data = @unserialize(substr($session_data, $offset), ['allowed_classes' => true]);
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
Expand Down
12 changes: 6 additions & 6 deletions src/SessionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ByJG\Session;

use ByJG\Util\JwtKeyInterface;
use ByJG\Util\JwtKeySecret;
use ByJG\Util\JwtRsaKey;
use ByJG\JwtWrapper\JwtKeyInterface;
use ByJG\JwtWrapper\JwtHashHmacSecret;
use ByJG\JwtWrapper\JwtOpenSSLKey;

class SessionConfig
{
Expand Down Expand Up @@ -53,13 +53,13 @@ public function withCookie($domain, $path = "/"): static

public function withSecret($secret): static
{
$this->jwtKey = new JwtKeySecret($secret);
$this->jwtKey = new JwtHashHmacSecret($secret);
return $this;
}

public function withRsaSecret($private, $public): static
{
$this->jwtKey = new JwtRsaKey($private, $public);
$this->jwtKey = new JwtOpenSSLKey($private, $public);
return $this;
}

Expand Down
25 changes: 6 additions & 19 deletions tests/JwtSessionTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

use ByJG\JwtWrapper\JwtWrapperException;
use ByJG\Session\JwtSession;
use ByJG\Session\JwtSessionException;
use ByJG\Session\SessionConfig;
use ByJG\Util\JwtWrapperException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

ob_start();
define("SETCOOKIE_FORTEST", "TESTCASE");
Expand Down Expand Up @@ -57,7 +58,7 @@ public function testClose()
$this->assertTrue($this->object->close());
}

public function dataProvider(): array
public static function dataProvider(): array
{
$obj = new stdClass();
$obj->prop1 = "value1";
Expand Down Expand Up @@ -119,35 +120,21 @@ public function dataProvider(): array
];
}

/**
* @dataProvider dataProvider
* @param $input
* @param $expected
*/
#[DataProvider('dataProvider')]
public function testSerializeSessionData($input, $expected)
{
$result = $this->object->serializeSessionData($input);
$this->assertEquals($expected, $result);
}

/**
* @dataProvider dataProvider
* @param $expected
* @param $input
* @throws Exception
*/
#[DataProvider('dataProvider')]
public function testUnserializeData($expected, $input)
{
$result = $this->object->unSerializeSessionData($input);
$this->assertEquals($expected, $result);
}

/**
* @dataProvider dataProvider
* @param $object
* @param $serialize
* @throws JwtWrapperException
*/
#[DataProvider('dataProvider')]
public function testReadWrite($object, $serialize)
{
$this->object->write("SESSID", $serialize);
Expand Down