Skip to content

Commit

Permalink
Merge pull request #5 from byjg/issue-4
Browse files Browse the repository at this point in the history
Issue 4 - Convert issues
  • Loading branch information
byjg authored Aug 25, 2018
2 parents b12db86 + 6117112 commit 7f5780c
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- "7.2"
- "7.1"
- "7.0"
- "5.6"

install:
- composer install

script:
- vendor/bin/phpunit

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# JwtSession

[![Build Status](https://travis-ci.org/byjg/jwt-session.svg?branch=master)](https://travis-ci.org/byjg/jwt-session)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/jwt-session/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/jwt-session/?branch=master)

JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN.
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"php": ">=5.6.0",
"byjg/jwt-wrapper": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": ">=5.7"
},
"license": "MIT"
}
28 changes: 28 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">

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

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

</phpunit>
13 changes: 8 additions & 5 deletions src/JwtSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct($serverName, $secretKey, $timeOutMinutes = null, $se
public function replaceSessionHandler($startSession = true)
{
if (session_status() != PHP_SESSION_NONE) {
throw new \Exception('Session already started!');
throw new JwtSessionException('Session already started!');
}

session_set_save_handler($this, true);
Expand Down Expand Up @@ -139,7 +139,7 @@ public function read($session_id)
$jwt = new JwtWrapper($this->serverName, $this->secretKey);
$data = $jwt->extractData($_COOKIE[self::COOKIE_PREFIX . $this->suffix]);

return $this->serializeSessionData($data->data);
return $data->data;
}
return '';
} catch (\Exception $ex) {
Expand Down Expand Up @@ -168,11 +168,14 @@ public function read($session_id)
public function write($session_id, $session_data)
{
$jwt = new JwtWrapper($this->serverName, $this->secretKey);
$data = $jwt->createJwtData($this->unSerializeSessionData($session_data), $this->timeOutMinutes * 60);
$data = $jwt->createJwtData($session_data, $this->timeOutMinutes * 60);
$token = $jwt->generateToken($data);

if (!headers_sent()) {
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, '/', $this->cookieDomain);
if (defined("SETCOOKIE_FORTEST")) {
$_COOKIE[self::COOKIE_PREFIX . $this->suffix] = $token;
}
}

return true;
Expand All @@ -194,7 +197,7 @@ public function unSerializeSessionData($session_data)
$offset = 0;
while ($offset < strlen($session_data)) {
if (!strstr(substr($session_data, $offset), "|")) {
throw new \Exception("invalid data, remaining: " . substr($session_data, $offset));
throw new JwtSessionException("invalid data, remaining: " . substr($session_data, $offset));
}
$pos = strpos($session_data, "|", $offset);
$num = $pos - $offset;
Expand All @@ -207,4 +210,4 @@ public function unSerializeSessionData($session_data)

return $return_data;
}
}
}
7 changes: 7 additions & 0 deletions src/JwtSessionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ByJG\Session;

class JwtSessionException extends \Exception
{

}
142 changes: 142 additions & 0 deletions tests/JwtSessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

use ByJG\Session\JwtSession;

ob_start();
define("SETCOOKIE_FORTEST", "TESTCASE");

class JwtSessionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var JwtSession
*/
protected $object;

const SESSION_ID = "sessionid";

protected function setUp()
{
$this->object = new JwtSession("example.com", "secretKey");
}

protected function tearDown()
{
header_remove();
$_COOKIE = [];
$this->object = null;
}


public function testDestroy()
{
$this->assertTrue($this->object->destroy(self::SESSION_ID));
}

public function testGc()
{
$this->assertTrue($this->object->gc(0));
}

public function testClose()
{
$this->assertTrue($this->object->close());
}

public function dataProvider()
{
$obj = new stdClass();
$obj->prop1 = "value1";
$obj->prop2 = "value2";

return
[
[
[
"text" => "simple string"
],
"text|s:13:\"simple string\";"
],
[
[
"text" => "simple string",
"text2" => "another string",
"number" => 74
],
"text|s:13:\"simple string\";text2|s:14:\"another string\";number|i:74;"
],
[
[
"text" => [ 1, 2, 3 ]
],
"text|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
],
[
[
"text" => [ "a" => 1, "b" => 2, "c" => 3 ]
],
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}"
],
[
[
"text" => [ "a" => 1, "b" => 2, "c" => 3 ],
"single" => 2000
],
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}single|i:2000;"
],
[
[
"text" => $obj
],
"text|O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}"
],
[
[
"text" => [ "a" => $obj ]
],
"text|a:1:{s:1:\"a\";O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
],
[
[
"text" => [ $obj ]
],
"text|a:1:{i:0;O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
]
];
}

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

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

/**
* @dataProvider dataProvider
* @param $object
* @param $serialize
*/
public function testReadWrite($object, $serialize)
{
$this->object->write("SESSID", $serialize);
$result = $this->object->read("SESSID");
$this->assertEquals($serialize, $result);
}

}

0 comments on commit 7f5780c

Please sign in to comment.