Skip to content

Commit

Permalink
Csrf-token are now refreshed on each page-load to avoid timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
skipperbent committed Nov 10, 2017
1 parent 97753f5 commit c3072e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/Pecee/CsrfToken.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Pecee;

class CsrfToken
Expand Down Expand Up @@ -48,24 +49,34 @@ public function validate($token)
* Set csrf token cookie
* Overwrite this method to save the token to another storage like session etc.
*
* @param $token
* @param string $token
*/
public function setToken($token)
{
$this->token = $token;
setcookie(static::CSRF_KEY, $token, time() + 60 * 120, '/');
}

/**
* Get csrf token
* @param string|null $defaultValue
* @return string|null
*/
public function getToken()
public function getToken($defaultValue = null)
{
if ($this->hasToken() === true) {
return $_COOKIE[static::CSRF_KEY];
}
$this->token = ($this->hasToken() === true) ? $_COOKIE[static::CSRF_KEY] : null;

return ($this->token !== null) ? $this->token : $defaultValue;
}

return null;
/**
* Refresh existing token
*/
public function refresh()
{
if ($this->token !== null) {
$this->setToken($this->token);
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Pecee/Http/Middleware/BaseCsrfVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct()
$this->csrfToken = new CsrfToken();

// Generate or get the CSRF-Token from Cookie.
$this->token = ($this->hasToken() === false) ? $this->generateToken() : $this->csrfToken->getToken();
$this->token = $this->csrfToken->getToken($this->generateToken());
}

/**
Expand Down Expand Up @@ -73,6 +73,9 @@ public function handle(Request $request)

}

// Refresh existing token
$this->csrfToken->refresh();

}

public function generateToken()
Expand Down

0 comments on commit c3072e8

Please sign in to comment.