Skip to content

Commit 9386296

Browse files
committed
Form tokens improved with a optional time check and one-time use
1 parent a6ee0ed commit 9386296

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Returns the decryped output as a string using [defuse/php-encryption](https://gi
2929
### encrypt($input, $key = false)
3030
Encrypt a string, if no key is given one will be generated for you (Recommended) using [defuse/php-encryption](https://github.com/defuse/php-encryption)'s library.
3131

32+
### getFormToken('form_token_id', $_POST['form_token'], $limit = false)
33+
Verify a form token for the given id. The $limit is optional andm ust be given in seconds, if the limit is 300 and the token is used after 300 seconds it will be considered invalid.
34+
3235
### password_hash($password)
3336
Hash the given password. This function allows for longer passwords and isn't affected by the null-byte issue.
3437

@@ -47,8 +50,13 @@ Return a random key using [defuse/php-encryption](https://github.com/defuse/php-
4750
### randomString($length)
4851
Returns a random string for the given length
4952

50-
### pseudoBytes($length)
51-
Returns random bytes for the given length
53+
### setFormToken($id)
54+
Set a unique token in the session and returns it, can be used to verify post/get requests
5255

5356
### strlen($str)
5457
Returns the length of the given string using mb_strlen when available
58+
59+
### pseudoBytes($length)
60+
Returns random bytes for the given length
61+
62+

src/SecureFuncs.php

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,28 @@ public static function encrypt($input, $key = false)
5353
/**
5454
* Checks if the given id and token match > If not the form has been sent twice or the ID is incorrect
5555
* @param $id
56+
* @param $limit_time
5657
* @return md5hash
5758
*/
58-
public static function getFormToken($id, $token)
59+
public static function getFormToken($id, $token, $limit_time = false)
5960
{
60-
if (empty($_SESSION['formtoken'][$id])) {
61-
return false;
61+
$valid = false;
62+
// Check if isset
63+
if (!empty($_SESSION['formtoken'][$id]) && !empty($_SESSION['formtoken_time'][$id])) {
64+
// Check if token is correct
65+
if (md5($_SESSION['formtoken'][$id]) === $token) {
66+
$valid = true;
67+
// If time limit is set, check if isset
68+
if ($limit_time !== false) {
69+
// if time < limit time return true/false
70+
if (empty($_SESSION['formtoken_time'][$id]) || $_SESSION['formtoken_time'][$id] < time() - $limit_time){
71+
$valid = false;
72+
}
73+
}
74+
}
6275
}
63-
return md5($_SESSION['formtoken'][$id]) == $token;
76+
unset($_SESSION['formtoken'][$id]);
77+
return $valid;
6478
}
6579

6680
/**
@@ -82,17 +96,6 @@ public static function password_verify($password, $hash)
8296
return password_verify(base64_encode(hash('sha256', $password, true)), $hash);
8397
}
8498

85-
/**
86-
* Sets a new random token using the given id
87-
* @param $id
88-
* @return md5hash
89-
*/
90-
public static function setFormToken($id)
91-
{
92-
$_SESSION['formtoken'][$id] = self::randomString(100);
93-
return md5($_SESSION['formtoken'][$id]);
94-
}
95-
9699
/**
97100
* @param int $length
98101
* @return string
@@ -165,18 +168,15 @@ public static function randomString($length)
165168
}
166169

167170
/**
168-
* @param int $length
169-
* @return string
170-
* @throws \Exception
171+
* Sets a new random token using the given id
172+
* @param $id
173+
* @return md5hash
171174
*/
172-
public static function pseudoBytes($length = 1)
175+
public static function setFormToken($id)
173176
{
174-
$bytes = \openssl_random_pseudo_bytes($length, $strong);
175-
if ($strong === TRUE) {
176-
return $bytes;
177-
} else {
178-
throw new \Exception ('Insecure server! (OpenSSL Random byte generation insecure.)');
179-
}
177+
$_SESSION['formtoken'][$id] = self::randomString(100);
178+
$_SESSION['formtoken_time'][$id] = time();
179+
return md5($_SESSION['formtoken'][$id]);
180180
}
181181

182182
/**
@@ -197,4 +197,19 @@ public static function strlen($str)
197197
}
198198
}
199199

200+
/**
201+
* @param int $length
202+
* @return string
203+
* @throws \Exception
204+
*/
205+
public static function pseudoBytes($length = 1)
206+
{
207+
$bytes = \openssl_random_pseudo_bytes($length, $strong);
208+
if ($strong === TRUE) {
209+
return $bytes;
210+
} else {
211+
throw new \Exception ('Insecure server! (OpenSSL Random byte generation insecure.)');
212+
}
213+
}
214+
200215
}

0 commit comments

Comments
 (0)