Skip to content

Commit cadf6d7

Browse files
authored
Merge pull request #9 from byjg/1.0.3
Some Updates
2 parents 7f5780c + 704a774 commit cadf6d7

File tree

3 files changed

+90
-13
lines changed

3 files changed

+90
-13
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# JwtSession
22

3+
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg.com-brightgreen.svg)](http://opensource.byjg.com)
34
[![Build Status](https://travis-ci.org/byjg/jwt-session.svg?branch=master)](https://travis-ci.org/byjg/jwt-session)
45
[![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)
56

67
JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN.
78
The implementation following the SessionHandlerInterface.
89

9-
## How to use:
10+
# How to use:
1011

1112
Before the session_start() use the command:
1213

@@ -18,7 +19,7 @@ session_set_save_handler($handler, true);
1819

1920
Now, all your `$_SESSION` variable will be saved directly to a JWT Token!!
2021

21-
## Motivation
22+
# Motivation
2223

2324
The default PHP Session does not work in different servers using round robin or other algorithms.
2425
This occurs because PHP Session are saved by default in the file system.
@@ -39,15 +40,15 @@ The JWT Token cannot be changed, but it can be read.
3940
This implementation save the JWT into a client cookie.
4041
Because of this _**do not** store in the JWT Token sensible data like passwords_.
4142

42-
## Install
43+
# Install
4344

4445
```
4546
composer require "byjg/jwt-session=1.0.*"
4647
```
4748

48-
## Customizations
49+
# Customizations
4950

50-
### Setting the validity of JWT Token
51+
## Setting the validity of JWT Token
5152

5253
```php
5354
<?php
@@ -56,31 +57,33 @@ $handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret ke
5657
session_set_save_handler($handler, true);
5758
```
5859

59-
### Setting the different Session Contexts
60+
## Setting the different Session Contexts
6061

6162
```php
6263
<?php
6364
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key', 20, 'MYCONTEXT');
6465
session_set_save_handler($handler, true);
6566
```
6667

67-
### Create the handler and replace the session handler
68+
## Create the handler and replace the session handler
6869

6970
```php
7071
<?php
7172
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key');
7273
$handler->replaceSessionHandler(true);
7374
```
7475

75-
### Create the handler and replace the session handler, specifying cookie domain valid for all subdomains of mydomain.com
76+
## Create the handler and replace the session handler, specifying cookie domain valid for all subdomains of mydomain.com
7677

7778
```php
7879
<?php
7980
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key', null, null, '.mydomain.com');
8081
$handler->replaceSessionHandler(true);
8182
```
8283

83-
### How it works
84+
## How it works
8485

8586
We store a cookie named AUTH_BEARER_<context name> with the session name. The PHPSESSID cookie is still created because
86-
PHP create it by default but we do not use it;
87+
PHP create it by default but we do not use it;
88+
89+

_config.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: jwt-session
2+
3+
project:
4+
version: 1.0.0
5+
download_url: https://github.com/byjg/jwt-session/releases
6+
7+
license:
8+
software: MIT
9+
software_url: https://opensource.org/licenses/MIT
10+
11+
docs: MIT
12+
docs_url: https://opensource.org/licenses/MIT
13+
14+
git_edit_address: https://github.com/byjg/jwt-session/blob/master/
15+
16+
links:
17+
header:
18+
- title: GitHub
19+
url: https://github.com/byjg/jwt-session
20+
- title: ByJG
21+
url: https://opensource.byjg.com/
22+
footer:
23+
- title: GitHub
24+
url: https://github.com/byjg/jwt-session
25+
- title: Issues
26+
url: https://github.com/byjg/jwt-session/issues
27+
28+
ui:
29+
header:
30+
color1: "#080331"
31+
color2: "#0033cc"
32+
trianglify: true
33+
34+
social:
35+
github:
36+
user: byjg
37+
repo: jwt-session
38+
twitter:
39+
enabled: false
40+
via:
41+
hash: opensourcebyjg
42+
account:
43+
facebook:
44+
enabled: false
45+
profileUrl:
46+
47+
analytics:
48+
google: UA-130014324-1
49+
50+
# Build settings
51+
markdown: kramdown
52+
remote_theme: allejo/jekyll-docs-theme
53+

src/JwtSession.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@ class JwtSession implements SessionHandlerInterface
1919

2020
protected $cookieDomain;
2121

22+
protected $path = "/";
23+
2224
/**
2325
* JwtSession constructor.
2426
*
2527
* @param $serverName
2628
* @param $secretKey
2729
* @param int $timeOutMinutes
2830
*/
29-
public function __construct($serverName, $secretKey, $timeOutMinutes = null, $sessionContext = null, $cookieDomain = null)
31+
public function __construct($serverName, $secretKey, $timeOutMinutes = null, $sessionContext = null, $cookieDomain = null, $path = "/")
3032
{
3133
$this->serverName = $serverName;
3234
$this->secretKey = $secretKey;
3335
$this->timeOutMinutes = $timeOutMinutes ?: 20;
3436
$this->suffix = $sessionContext ?: 'default';
3537
$this->cookieDomain = $cookieDomain;
38+
$this->path = "/";
3639
}
3740

41+
/**
42+
* @param bool $startSession
43+
* @throws JwtSessionException
44+
*/
3845
public function replaceSessionHandler($startSession = true)
3946
{
4047
if (session_status() != PHP_SESSION_NONE) {
@@ -78,7 +85,13 @@ public function close()
7885
public function destroy($session_id)
7986
{
8087
if (!headers_sent()) {
81-
setcookie(self::COOKIE_PREFIX . $this->suffix, null);
88+
setcookie(
89+
self::COOKIE_PREFIX . $this->suffix,
90+
null,
91+
(time()-3000),
92+
$this->path,
93+
$this->cookieDomain
94+
);
8295
}
8396

8497
return true;
@@ -172,7 +185,15 @@ public function write($session_id, $session_data)
172185
$token = $jwt->generateToken($data);
173186

174187
if (!headers_sent()) {
175-
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, '/', $this->cookieDomain);
188+
setcookie(
189+
self::COOKIE_PREFIX . $this->suffix,
190+
$token,
191+
(time()+$this->timeOutMinutes*60) ,
192+
$this->path,
193+
$this->cookieDomain,
194+
false,
195+
true
196+
);
176197
if (defined("SETCOOKIE_FORTEST")) {
177198
$_COOKIE[self::COOKIE_PREFIX . $this->suffix] = $token;
178199
}

0 commit comments

Comments
 (0)