Skip to content

Commit 9023e08

Browse files
committed
Bugfixes in JWT session and added additional documentation
1 parent e0059ee commit 9023e08

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

scripts/jwt-session.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ function jwt_decode($jwt, $secretKey=null, $currentTime=null, $algorithms=null){
167167

168168
// Check if token can already be used (if set)
169169
$leeway = isset($_ENV['JWT_LEEWAY_SEC']) ? intval($_ENV['JWT_LEEWAY_SEC']) : 0;
170-
if(isset($payload->nbf) && $payload->nbf > ($timestamp + $leeway)) return false;
170+
if(isset($payload->nbf) && $payload->nbf > ($currentTime + $leeway)) return false;
171171

172172
// Check that token has been created before 'now'
173-
if(isset($payload->iat) && $payload->iat > ($timestamp + $leeway)) return false;
173+
if(isset($payload->iat) && $payload->iat > ($currentTime + $leeway)) return false;
174174

175175
// Check if this token has expired.
176-
if(isset($payload->exp) && ($timestamp - $leeway) >= $payload->exp) return false;
176+
if(isset($payload->exp) && ($currentTime - $leeway) >= $payload->exp) return false;
177177

178178
return $payload;
179179
}
@@ -222,9 +222,13 @@ function jwt_sign($msg, $secretKey=null, $alg='HS256'){
222222

223223

224224
/** Creates a JWT
225-
* @param Array $payload JSON object into which custom data can be stored. Spezial functional values are:
226-
* "nbf": <UtcSec> to defined that token is valid after a (future) timestamp
227-
* "
225+
* @param Array $payload JSON object into which custom data can be stored. Typical (functional) values are:
226+
* "nbf": <UtcSec> defines that JWT is only valid after a (future) timestamp
227+
* "iat": <UtcSec> defines creation date of JWT
228+
* "exp": <UtcSec> defines timestamp at which JWT becomes invalid
229+
* "iss": <String> Issuer/Claim that issued the JWT
230+
* "sub": <String> Subject of the JWT
231+
* "aud": <String> Audience identifier the recipients that the JWT is intended for
228232
* @param String $secretKey Private key to sign JWT (if null then $_ENV['JWT_SECRET_KEY'])
229233
* @param String $alg Algorithm that should be used for signing (optional)
230234
* @param String $keyId ID of the key (optional)
@@ -243,18 +247,26 @@ function jwt_encode($payload, $secretKey=null, $alg='HS256', $keyId=null, $head=
243247

244248
/** Loads the user session from a cookie
245249
* @param String $cookieName Name of the cookie in which the session is stored (default 'jwt')
246-
* @param String $secretKey Private key to verify integrity of JWT (if null then $_ENV['JWT_SECRET_KEY'])
247-
* @param Int $currentTime UTC timestamp in seconds (optional, can be used for unit tests)
250+
* @param String $secretKey Private key to verify integrity of session data (if null then $_ENV['JWT_SECRET_KEY'])
251+
* @param Int $currentTime Current UTC time seconds (optional, can be used for unit tests)
252+
* @param Array $algorithms Map of allowed algorithms (optional, if null or empty then JWT_SUPPORTED_ALGORITHMS will be used)
248253
* @return Array containing loaded session values or empty array if no valid session
249254
*/
250-
function jwt_session_load($cookieName="jwt", $secretKey=null, $currentTime=null){
255+
function jwt_session_load($cookieName="jwt", $secretKey=null, $currentTime=null, $algorithms=array()){
251256
if(!isset($_COOKIE[$cookieName])) return array();
252-
return jwt_decode($_COOKIE[$cookieName]);
257+
return jwt_decode($_COOKIE[$cookieName], $secretKey, $currentTime, $algorithms);
253258
}
254259

255260

256261
/** Stores/updates the session in a cookie
257-
* @param Object $jsonObj JSON object containing the custom data that should be stored in the session (if null then $_SESSION will be used)
262+
* @param Object $jsonObj JSON object containing the custom data that should be stored in the session (if null then $_SESSION will be used).
263+
* Typical (functional) values are:
264+
* "nbf": <UtcSec> defines that JWT is only valid after a (future) timestamp
265+
* "iat": <UtcSec> defines creation date of JWT
266+
* "exp": <UtcSec> defines timestamp at which JWT becomes invalid
267+
* "iss": <String> Issuer/Claim that issued the JWT
268+
* "sub": <String> Subject of the JWT
269+
* "aud": <String> Audience identifier the recipients that the JWT is intended for
258270
* @param String $cookieName Name of the cookie in which the session will be stored (default 'jwt')
259271
* @param Int $cookieExpire Expire seconds for how long the session should be valid (0 = until tab/browser gets closed, default '0')
260272
* @param Boolean $cookieSecure True if session should only be sent if HTTPs is used, if null then $_ENV['JWT_HTTPS_ONLY'] will be used (default null)

0 commit comments

Comments
 (0)