Skip to content
Merged

6.0 #20

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
35 changes: 32 additions & 3 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,44 @@ jobs:
strategy:
matrix:
php-version:
- "8.5"
- "8.4"
- "8.3"
- "8.2"
- "8.1"

steps:
- uses: actions/checkout@v5
- run: composer install
- run: ./vendor/bin/phpunit
- run: composer test

Psalm:
name: Psalm Static Analyzer
runs-on: ubuntu-latest
permissions:
# for github/codeql-action/upload-sarif to upload SARIF results
security-events: write
container:
image: byjg/php:8.4-cli
options: --user root --privileged

steps:
- name: Git checkout
uses: actions/checkout@v4

- name: Composer
run: composer install

- name: Psalm
# Note: Ignoring error code 2, which just signals that some
# flaws were found, not that Psalm itself failed to run.
run: ./vendor/bin/psalm
--show-info=true
--report=psalm-results.sarif || [ $? = 2 ]

- name: Upload Analysis results to GitHub
uses: github/codeql-action/upload-sarif@v4
if: github.ref == 'refs/heads/master'
with:
sarif_file: psalm-results.sarif

Documentation:
if: github.ref == 'refs/heads/master'
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# JwtSession
# JWT Session Handler

[![Build Status](https://github.com/byjg/jwt-session/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/jwt-session/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/jwt-session/)
[![GitHub license](https://img.shields.io/github/license/byjg/jwt-session.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![Sponsor](https://img.shields.io/badge/Sponsor-%23ea4aaa?logo=githubsponsors&logoColor=white&labelColor=0d1117)](https://github.com/sponsors/byjg)
[![Build Status](https://github.com/byjg/jwt-session/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/jwt-session/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/jwt-session/)
[![GitHub license](https://img.shields.io/github/license/byjg/jwt-session.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/jwt-session.svg)](https://github.com/byjg/jwt-session/releases/)

JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN.
The implementation following the SessionHandlerInterface.
A PHP session replacement that stores session data in JWT tokens instead of the filesystem. This implementation follows the SessionHandlerInterface standard, enabling stateless sessions without the need for dedicated session servers like Redis or Memcached. Perfect for distributed applications and microservices architectures.

# How to use:

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/jwt-session",
"description": "JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN. The implementation following the SessionHandlerInterface.",
"description": "A PHP session replacement that stores session data in JWT tokens instead of the filesystem. This implementation follows the SessionHandlerInterface standard, enabling stateless sessions without the need for dedicated session servers like Redis or Memcached. Perfect for distributed applications and microservices architectures.",
"autoload": {
"psr-4": {
"ByJG\\Session\\": "src/"
Expand All @@ -9,16 +9,16 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1 <8.5",
"php": ">=8.3 <8.6",
"byjg/jwt-wrapper": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^10|^11",
"vimeo/psalm": "^5.9|^6.12"
"phpunit/phpunit": "^10.5|^11.5",
"vimeo/psalm": "^5.9|^6.13"
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm"
"psalm": "vendor/bin/psalm --threads=1"
},
"license": "MIT"
}
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
errorLevel="3"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
Expand Down
17 changes: 14 additions & 3 deletions src/JwtSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Exception;
use SessionHandlerInterface;

class JwtSession implements SessionHandlerInterface

Check warning on line 10 in src/JwtSession.php

View workflow job for this annotation

GitHub Actions / Psalm Static Analyzer

ClassMustBeFinal

src/JwtSession.php:10:7: ClassMustBeFinal: Class ByJG\Session\JwtSession is never extended and is not part of the public API, and thus must be made final. (see https://psalm.dev/361)
{
const COOKIE_PREFIX = "AUTH_BEARER_";
const string COOKIE_PREFIX = "AUTH_BEARER_";

/**
* @var SessionConfig
Expand All @@ -22,7 +22,7 @@
* @param $sessionConfig
* @throws JwtSessionException
*/
public function __construct($sessionConfig)

Check warning on line 25 in src/JwtSession.php

View workflow job for this annotation

GitHub Actions / Psalm Static Analyzer

MissingParamType

src/JwtSession.php:25:33: MissingParamType: Parameter $sessionConfig has no provided type (see https://psalm.dev/154)
{
ini_set("session.use_cookies", 0);

Expand Down Expand Up @@ -152,9 +152,13 @@
{
try {
if (isset($_COOKIE[self::COOKIE_PREFIX . $this->sessionConfig->getSessionContext()])) {
$key = $this->sessionConfig->getKey();
if ($key === null) {
return '';
}
$jwt = new JwtWrapper(
$this->sessionConfig->getServerName(),
$this->sessionConfig->getKey()
$key
);
$data = $jwt->extractData($_COOKIE[self::COOKIE_PREFIX . $this->sessionConfig->getSessionContext()]);

Expand Down Expand Up @@ -192,9 +196,13 @@
#[\Override]
public function write(string $id, string $data): bool
{
$key = $this->sessionConfig->getKey();
if ($key === null) {
return false;
}
$jwt = new JwtWrapper(
$this->sessionConfig->getServerName(),
$this->sessionConfig->getKey()
$key
);
$session_data = $jwt->createJwtData(['data' => $data], $this->sessionConfig->getTimeoutMinutes() * 60, 0, null);
$token = $jwt->generateToken($session_data);
Expand All @@ -217,7 +225,7 @@
return true;
}

public function serializeSessionData($array): string

Check warning on line 228 in src/JwtSession.php

View workflow job for this annotation

GitHub Actions / Psalm Static Analyzer

MissingParamType

src/JwtSession.php:228:42: MissingParamType: Parameter $array has no provided type (see https://psalm.dev/154)
{
$result = '';
foreach ($array as $key => $value) {
Expand All @@ -232,13 +240,16 @@
* @return array
* @throws JwtSessionException
*/
public function unSerializeSessionData($session_data): array

Check warning on line 243 in src/JwtSession.php

View workflow job for this annotation

GitHub Actions / Psalm Static Analyzer

MissingParamType

src/JwtSession.php:243:44: MissingParamType: Parameter $session_data has no provided type (see https://psalm.dev/154)
{
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
if (!str_contains(substr($session_data, $offset), "|")) throw new JwtSessionException("invalid data, remaining: " . substr($session_data, $offset));
$pos = strpos($session_data, "|", $offset);
if ($pos === false) {
throw new JwtSessionException("invalid data, pipe not found");
}
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/JwtSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JwtSessionTest extends TestCase
*/
protected SessionConfig $sessionConfig;

const SESSION_ID = "sessionid";
const string SESSION_ID = "sessionid";

/**
* @throws JwtSessionException
Expand Down
Loading