Skip to content

Commit f251930

Browse files
committed
CSRF token stored in _SESSION #24
1 parent cb0e3b1 commit f251930

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Middleware/CsrfGuard.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public function call() {
5656
public function check() {
5757
// Create token
5858
$env = $this->app->environment();
59-
$token = sha1($env['REMOTE_ADDR'] . '|' . $env['USER_AGENT']);
59+
60+
if ( PHP_SESSION_ACTIVE === session_status() ){
61+
if ( ! isset( $_SESSION[ $this->key ] ) ){
62+
$_SESSION[ $this->key ] = sha1( serialize( $_SERVER ) . rand( 0, 0xffffffff ) );
63+
}
64+
} else {
65+
throw new Exception( "Session are required to use CSRF Guard" );
66+
}
67+
$token = $_SESSION[ $this -> key ];
6068

6169
// Validate
6270
if ( in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE')) ) {
@@ -74,4 +82,4 @@ public function check() {
7482
}
7583
}
7684

77-
?>
85+
?>

0 commit comments

Comments
 (0)