Skip to content

Commit 037cbd1

Browse files
committed
Added ULtimateSessionLoader for easy library instantiation. Updated unit tests and docs.
1 parent 16c698d commit 037cbd1

17 files changed

+1551
-77
lines changed

README.md

+110-28
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,52 @@
66

77
This library provides a simple drop-in replacement to PHP's out-of-the-box
88
session functionality to provide more robust and secure session handling by
9-
enforcing recommend php.ini session settings and providing optional session
9+
enforcing recommended php.ini session settings and providing optional session
1010
data encryption, with client-side decryption key storage (in cookie).
1111
Encryption is provided via use of the excellent [defuse/php-encryption](https://github.com/defuse/php-encryption) library.
1212

13-
The primary components of the library are:
13+
**The primary components of the library are:**
1414

1515
[UltimateSessionHandler](docs/MikeBrant-UltimateSessions-UltimateSessionHandler.md)
1616

17-
A class which extends PHP's `SessionHandler` class with added encryption
18-
capability. Class instantiation automatically sets php.ini session settings
19-
to their preferred values and sets the class as session save handler via
17+
A class which extends PHP's `SessionHandler` class with added encryption
18+
capability. Class instantiation automatically sets php.ini session settings
19+
to their preferred values and sets the class as session save handler via
2020
`set_session_save_handler()`.
2121

22-
[AbstractUltimateSessionHandler](docs/MikeBrant-UltimateSessions-AbstractUltimateSessionHandler.md)
23-
24-
An abstract class made available for those wanting to implement their own
25-
session handler that does not extend from SessionHandler. This would be an
26-
atypical situation, but it is here if needed.
27-
28-
(Planned for future) UltimateSessionManager - A class to provide
29-
object-oriented access to
30-
typical PHP session handling functions along with timestamp based session
31-
security.
22+
[UltimateSessionManager](doc/MikeBrant-UltimateSessions-UltimateSessionManager.md)
23+
24+
This class is an object-oriented wrapper around common PHP session management
25+
functions along with features geared at enhancing security
26+
and proper session management behaviors including:
27+
- timestamp-based management for session data expiry;
28+
- automated session ID regeneration at configurable time- and count-based
29+
intervals;
30+
- client fingerprinting based on request header properties
31+
- logging of data for certain use cases where there are session accesses
32+
against expired data or accesses with mis-matched fingerprints, which may
33+
need further investigation. Only data within session metadata is logged.
34+
An optional PSR-3 compliant logger to be used for logging in lieu of
35+
default logging via `error_log()`.
36+
37+
This class allows for setting of callback around session ID change events
38+
(ID regeneration or ID forwarding from expired session). For example, in
39+
recommended library configuration, this callback is used to trigger
40+
encryption key cookie regeneration using
41+
`UltimateSessionHandler::changeKeyCookieSessionId()`.
42+
43+
For cases where custom session garbage collection is implemented
44+
(something that should strongly be considered for production-level
45+
applications), this class offers setting of an optional callback that
46+
is passed session ID and data expiry timestamp that can be used to mark
47+
those session ID's as eligible for garbage collection after given
48+
timestamp (by touching files, updating database field, etc.).
49+
50+
[UltimateSessionLoader](doc/)
51+
52+
A simple static interface provided to allow for one-line instantiation of
53+
the UltimateSession library using recommended settings, with a few minimal
54+
configuration options.
3255

3356
Requires:
3457
- PHP 5.6+
@@ -53,31 +76,90 @@ This library is developed against PHP 7.1 and tested via Travis CI against:
5376

5477
**Usage**
5578

56-
Usage is intended to be dead simple. Just instantiate
57-
UltimateSessoinConfig passing optional parameters:
58-
- `$useEncryption` (boolean, default `false`)
59-
- `$keyCookiePrefix` (string, default `ULTSESS_`)
79+
Usage is intended to be dead simple. Using
80+
`UltimateSessionLoader::initialize()` you can
81+
set up with few lines of code. The parameters for this method are:
82+
83+
- `$useEncryption` (boolean, default `false`) Flag to determine if encryption
84+
is applied to session data.
85+
- `$logger` (Psr\Log\LoggerInterface, default null) A PSR-3 compliant logger
86+
can be attached to UltimateSessionManager to allow for logging of
87+
non-standard session access events.
6088

61-
Then pass this config to a new UltimateSessionHandler instance
62-
and start your session.
89+
Sample code usage:
6390

6491
```php
6592

6693
use \MikeBrant\UltimateSessions;
6794

6895
try {
69-
$sessionConfig = UltimateSessionConfig::getInstance(
70-
$useEncryption,
71-
$keyCookiePrefix)
72-
;
73-
new UltimateSessionHandler($sessionConfig);
96+
$sessionManager = UltimateSessionLoader::initialize(
97+
$useEncryption,
98+
$logger
99+
);
74100
} catch (\Exception $e) {
75101
// handle as needed (or don't try-catch at all)
76102
}
77-
session_start();
103+
// replacement for session_start()
104+
$result = $sessionManager->startSession();
105+
106+
// Make sure to investigate result of startSession()
107+
// as false represents a case where an invalid session was attempted
108+
// to be started
109+
if($result === false) {
110+
// perhaps exit execution
111+
// at a minimum drop all user authentication/state
112+
}
113+
114+
// set session data as normal
115+
$_SESSION['data'] = 'value;
116+
117+
// manually regenerate session Id
118+
// as typically done around authentication or privelege level change
119+
$sessionManager->regenerateId();
120+
121+
// get session ID
122+
$sessionId = $sessionManager->getSessionId();
123+
124+
// close session to write, replacement for session_write_commit()
125+
$sessionManager->commitSession();
126+
127+
// destroy session. Not commonly used, by calling code, but can be
128+
// used as necessary to unset $_SESSION and destroy session as done
129+
// with session_destroy()
130+
$sessionManager->destroySession();
78131
```
79132

80-
Recommended reading related to PHP session security:
133+
There are more configuration options available beyond this Please look at
134+
full library documentation at link above.
135+
136+
Note that while the `UltimateSessionHandler` and `UltimateSessionManager` are
137+
designed to work together, each of these classes could be used independently
138+
of each other. The `UltimateSessionHandler` class should also be considered
139+
as a class designed to be extended upon should one want to implement custom
140+
session storage (i.e. in database, memory store, etc.) above and beyond
141+
one of the built-in PHP session stores which which this class can work as-is.
142+
143+
**A note on session garbage collection:**
144+
145+
146+
This library does not perform session garbage collection or override default
147+
`SassionHandler::gc()` behavior at all. As such, this class would work with
148+
PHP's built in session stores and not modify their basic behavior at all. If one were to extend `UltimateSessionHandler` to implement a custom data
149+
store it is **highly** recommended that you not rely on PHP's default probability-based garbage collection scheme (and thus not override `gc()`
150+
method at all). It is preferable to use a schedule-based garbage collection
151+
mechanism as a means to perform final deletion of session stores. To this
152+
end, the `UltimateSessionManager` class constructor accepts a callback
153+
which can be used as a hook for you to update your session data store with
154+
a timestamp after which the individual data for the session can be removed.
155+
This hook can actually be used even in cases where you want to override
156+
`gc()` in a probability-based deletion scheme (the probability-based
157+
approach triggers `gc()` session handler). You would just need your
158+
overridden `gc()` method to take advantage of the timestamps that have
159+
been set on the data store to determine eligibility of the session data
160+
for deletion.
161+
162+
**Recommended reading related to PHP session security:**
81163

82164
[PHP Session Lifecycle](https://gist.github.com/mindplay-dk/623bdd50c1b4c0553cd3)
83165

docs/MikeBrant-UltimateSessions-AbstractUltimateSessionHandler.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Class AbstractUltimateSessionHandler
55

66
This is abstract base class for extension for cases where one does not
77
want to directly inherit from PHP's \SessionHandler class as is done in
8-
UltimateSessionConfig, but still want to leverage the functionality
8+
UltimateSessionHandlerConfig, but still want to leverage the functionality
99
provided in UltimateSessionHandlerTrait which is composed into both classes.
1010

1111
For most use cases, it would likely be preferable to extend
@@ -43,7 +43,7 @@ Properties
4343

4444
### $config
4545

46-
protected \MikeBrant\UltimateSessions\UltimateSessionConfig $config = null
46+
protected \MikeBrant\UltimateSessions\UltimateSessionHandlerConfig $config = null
4747

4848
Object storing session handler configuration values as derived from an
4949
UltimateSessionHandleConfig object and needed for classes inheriting
@@ -72,7 +72,7 @@ Methods
7272

7373
### __construct
7474

75-
\MikeBrant\UltimateSessions\UltimateSessionHandlerInterface MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::__construct(\MikeBrant\UltimateSessions\UltimateSessionConfig $config)
75+
\MikeBrant\UltimateSessions\UltimateSessionHandlerInterface MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::__construct(\MikeBrant\UltimateSessions\UltimateSessionHandlerConfig $config)
7676

7777
UltimateSessionHandlerInterface constructor.
7878

@@ -83,7 +83,7 @@ UltimateSessionHandlerInterface constructor.
8383

8484

8585
#### Arguments
86-
* $config **[MikeBrant\UltimateSessions\UltimateSessionConfig](MikeBrant-UltimateSessions-UltimateSessionConfig.md)**
86+
* $config **[MikeBrant\UltimateSessions\UltimateSessionHandlerConfig](MikeBrant-UltimateSessions-UltimateSessionHandlerConfig.md)**
8787

8888

8989

@@ -265,6 +265,23 @@ This method is implemented in UltimateSessionHandlerTrait.
265265

266266

267267

268+
### changeKeyCookieSessionId
269+
270+
mixed MikeBrant\UltimateSessions\AbstractUltimateSessionHandler::changeKeyCookieSessionId($oldSessionId, $newSessionId)
271+
272+
Method to associate encryption key cookie to a new session id. Thi
273+
274+
275+
276+
* Visibility: **public**
277+
278+
279+
#### Arguments
280+
* $oldSessionId **mixed**
281+
* $newSessionId **mixed**
282+
283+
284+
268285
### encrypt
269286

270287
string MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::encrypt(string $sessionId, string $sessionData)

docs/MikeBrant-UltimateSessions-UltimateSessionHandler.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ MikeBrant\UltimateSessions\UltimateSessionHandler
33

44
Class UltimateSessionHandler
55

6-
This class extends PHP's \SessionHandler implementation to use
7-
UltimateSession functionality exposed through UltimateSessionHandlerTrait.
8-
This class provides optional encryption of data stored in session.
6+
A class which extends PHP's `SessionHandler` class with added encryption
7+
capability. Class instantiation automatically sets php.ini session settings
8+
to their preferred values and sets the class as session save handler via
9+
`set_session_save_handler()`.
910

1011
See article on \SessionHandler call pattern from PHP internals at
1112
https://gist.github.com/mindplay-dk/623bdd50c1b4c0553cd3
@@ -28,7 +29,7 @@ Properties
2829

2930
### $config
3031

31-
protected \MikeBrant\UltimateSessions\UltimateSessionConfig $config = null
32+
protected \MikeBrant\UltimateSessions\UltimateSessionHandlerConfig $config = null
3233

3334
Object storing session handler configuration values as derived from an
3435
UltimateSessionHandleConfig object and needed for classes inheriting
@@ -57,7 +58,7 @@ Methods
5758

5859
### __construct
5960

60-
\MikeBrant\UltimateSessions\UltimateSessionHandlerInterface MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::__construct(\MikeBrant\UltimateSessions\UltimateSessionConfig $config)
61+
\MikeBrant\UltimateSessions\UltimateSessionHandlerInterface MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::__construct(\MikeBrant\UltimateSessions\UltimateSessionHandlerConfig $config)
6162

6263
UltimateSessionHandlerInterface constructor.
6364

@@ -68,7 +69,7 @@ UltimateSessionHandlerInterface constructor.
6869

6970

7071
#### Arguments
71-
* $config **[MikeBrant\UltimateSessions\UltimateSessionConfig](MikeBrant-UltimateSessions-UltimateSessionConfig.md)**
72+
* $config **[MikeBrant\UltimateSessions\UltimateSessionHandlerConfig](MikeBrant-UltimateSessions-UltimateSessionHandlerConfig.md)**
7273

7374

7475

@@ -182,6 +183,23 @@ This method is implemented in UltimateSessionHandlerTrait.
182183

183184

184185

186+
### changeKeyCookieSessionId
187+
188+
mixed MikeBrant\UltimateSessions\UltimateSessionHandler::changeKeyCookieSessionId($oldSessionId, $newSessionId)
189+
190+
Method to associate encryption key cookie to a new session id. Thi
191+
192+
193+
194+
* Visibility: **public**
195+
196+
197+
#### Arguments
198+
* $oldSessionId **mixed**
199+
* $newSessionId **mixed**
200+
201+
202+
185203
### encrypt
186204

187205
string MikeBrant\UltimateSessions\UltimateSessionHandlerInterface::encrypt(string $sessionId, string $sessionData)

0 commit comments

Comments
 (0)