Skip to content

Commit 7f5780c

Browse files
authored
Merge pull request #5 from byjg/issue-4
Issue 4 - Convert issues
2 parents b12db86 + 6117112 commit 7f5780c

File tree

7 files changed

+203
-5
lines changed

7 files changed

+203
-5
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: php
2+
3+
php:
4+
- "7.2"
5+
- "7.1"
6+
- "7.0"
7+
- "5.6"
8+
9+
install:
10+
- composer install
11+
12+
script:
13+
- vendor/bin/phpunit
14+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# JwtSession
22

3+
[![Build Status](https://travis-ci.org/byjg/jwt-session.svg?branch=master)](https://travis-ci.org/byjg/jwt-session)
34
[![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)
45

56
JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"php": ">=5.6.0",
1919
"byjg/jwt-wrapper": "1.0.*"
2020
},
21+
"require-dev": {
22+
"phpunit/phpunit": ">=5.7"
23+
},
2124
"license": "MIT"
2225
}

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
8+
<!-- see http://www.phpunit.de/wiki/Documentation -->
9+
<phpunit bootstrap="./vendor/autoload.php"
10+
colors="true"
11+
convertErrorsToExceptions="true"
12+
convertNoticesToExceptions="true"
13+
convertWarningsToExceptions="true"
14+
stopOnFailure="false">
15+
16+
<filter>
17+
<whitelist>
18+
<directory>./src</directory>
19+
</whitelist>
20+
</filter>
21+
22+
<testsuites>
23+
<testsuite name="Test Suite">
24+
<directory>./tests/</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
</phpunit>

src/JwtSession.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($serverName, $secretKey, $timeOutMinutes = null, $se
3838
public function replaceSessionHandler($startSession = true)
3939
{
4040
if (session_status() != PHP_SESSION_NONE) {
41-
throw new \Exception('Session already started!');
41+
throw new JwtSessionException('Session already started!');
4242
}
4343

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

142-
return $this->serializeSessionData($data->data);
142+
return $data->data;
143143
}
144144
return '';
145145
} catch (\Exception $ex) {
@@ -168,11 +168,14 @@ public function read($session_id)
168168
public function write($session_id, $session_data)
169169
{
170170
$jwt = new JwtWrapper($this->serverName, $this->secretKey);
171-
$data = $jwt->createJwtData($this->unSerializeSessionData($session_data), $this->timeOutMinutes * 60);
171+
$data = $jwt->createJwtData($session_data, $this->timeOutMinutes * 60);
172172
$token = $jwt->generateToken($data);
173173

174174
if (!headers_sent()) {
175175
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, '/', $this->cookieDomain);
176+
if (defined("SETCOOKIE_FORTEST")) {
177+
$_COOKIE[self::COOKIE_PREFIX . $this->suffix] = $token;
178+
}
176179
}
177180

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

208211
return $return_data;
209212
}
210-
}
213+
}

src/JwtSessionException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace ByJG\Session;
3+
4+
class JwtSessionException extends \Exception
5+
{
6+
7+
}

tests/JwtSessionTest.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
use ByJG\Session\JwtSession;
4+
5+
ob_start();
6+
define("SETCOOKIE_FORTEST", "TESTCASE");
7+
8+
class JwtSessionTest extends \PHPUnit\Framework\TestCase
9+
{
10+
/**
11+
* @var JwtSession
12+
*/
13+
protected $object;
14+
15+
const SESSION_ID = "sessionid";
16+
17+
protected function setUp()
18+
{
19+
$this->object = new JwtSession("example.com", "secretKey");
20+
}
21+
22+
protected function tearDown()
23+
{
24+
header_remove();
25+
$_COOKIE = [];
26+
$this->object = null;
27+
}
28+
29+
30+
public function testDestroy()
31+
{
32+
$this->assertTrue($this->object->destroy(self::SESSION_ID));
33+
}
34+
35+
public function testGc()
36+
{
37+
$this->assertTrue($this->object->gc(0));
38+
}
39+
40+
public function testClose()
41+
{
42+
$this->assertTrue($this->object->close());
43+
}
44+
45+
public function dataProvider()
46+
{
47+
$obj = new stdClass();
48+
$obj->prop1 = "value1";
49+
$obj->prop2 = "value2";
50+
51+
return
52+
[
53+
[
54+
[
55+
"text" => "simple string"
56+
],
57+
"text|s:13:\"simple string\";"
58+
],
59+
[
60+
[
61+
"text" => "simple string",
62+
"text2" => "another string",
63+
"number" => 74
64+
],
65+
"text|s:13:\"simple string\";text2|s:14:\"another string\";number|i:74;"
66+
],
67+
[
68+
[
69+
"text" => [ 1, 2, 3 ]
70+
],
71+
"text|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
72+
],
73+
[
74+
[
75+
"text" => [ "a" => 1, "b" => 2, "c" => 3 ]
76+
],
77+
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}"
78+
],
79+
[
80+
[
81+
"text" => [ "a" => 1, "b" => 2, "c" => 3 ],
82+
"single" => 2000
83+
],
84+
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}single|i:2000;"
85+
],
86+
[
87+
[
88+
"text" => $obj
89+
],
90+
"text|O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}"
91+
],
92+
[
93+
[
94+
"text" => [ "a" => $obj ]
95+
],
96+
"text|a:1:{s:1:\"a\";O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
97+
],
98+
[
99+
[
100+
"text" => [ $obj ]
101+
],
102+
"text|a:1:{i:0;O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
103+
]
104+
];
105+
}
106+
107+
/**
108+
* @dataProvider dataProvider
109+
* @param $input
110+
* @param $expected
111+
*/
112+
public function testSerializeSessionData($input, $expected)
113+
{
114+
$result = $this->object->serializeSessionData($input);
115+
$this->assertEquals($expected, $result);
116+
}
117+
118+
/**
119+
* @dataProvider dataProvider
120+
* @param $expected
121+
* @param $input
122+
* @throws Exception
123+
*/
124+
public function testUnserializeData($expected, $input)
125+
{
126+
$result = $this->object->unSerializeSessionData($input);
127+
$this->assertEquals($expected, $result);
128+
}
129+
130+
/**
131+
* @dataProvider dataProvider
132+
* @param $object
133+
* @param $serialize
134+
*/
135+
public function testReadWrite($object, $serialize)
136+
{
137+
$this->object->write("SESSID", $serialize);
138+
$result = $this->object->read("SESSID");
139+
$this->assertEquals($serialize, $result);
140+
}
141+
142+
}

0 commit comments

Comments
 (0)