Phosphorum\Utils\Security::destroyPrefixedToken PHP Method

destroyPrefixedToken() public method

Removes the value of the CSRF token and key from session.
public destroyPrefixedToken ( string $prefix )
$prefix string
    public function destroyPrefixedToken($prefix)
    {
        $prefixedKey = $prefix . ':' . $this->_tokenKeySessionID;
        $prefixedValue = $prefix . ':' . $this->_tokenValueSessionID;
        /** @var \Phalcon\Session\AdapterInterface $session */
        $session = $this->getDI()->getShared('session');
        $session->remove($prefixedKey);
        $session->remove($prefixedValue);
        unset($this->prefixedTokenKeys[$prefixedKey], $this->prefixedTokens[$prefixedValue]);
        return $this;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Tests Security::checkPrefixedToken method
  */
 public function testCheckPrefixedToken()
 {
     $this->specify('The Security::checkPrefixedToken works incorrectly', function () {
         $di = $this->setupDI();
         $s = new Security();
         $s->setDI($di);
         // Random token and token key check
         $tokenKey = $s->getPrefixedTokenKey('y');
         $token = $s->getPrefixedToken('y');
         $_POST = [$tokenKey => $token];
         expect($s->checkPrefixedToken('y', null, null, false))->true();
         expect($s->checkPrefixedToken('y'))->true();
         expect($s->checkPrefixedToken('y'))->false();
         // Destroy token check
         $tokenKey = $s->getPrefixedToken('z');
         $token = $s->getPrefixedToken('z');
         $s->destroyPrefixedToken('z');
         $_POST = [$tokenKey => $token];
         expect($s->checkPrefixedToken('z'))->false();
         // Custom token key check
         $token = $s->getPrefixedToken('abc');
         $_POST = ['custom_key' => $token];
         expect($s->checkPrefixedToken('abc', null, null, false))->false();
         expect($s->checkPrefixedToken('abc', 'other_custom_key', null, false))->false();
         expect($s->checkPrefixedToken('abc', 'custom_key'))->true();
         // Custom token value check
         $token = $s->getPrefixedToken('xyz');
         $_POST = [];
         expect($s->checkPrefixedToken('xyz', null, null, false))->false();
         expect($s->checkPrefixedToken('xyz', 'some_random_key', 'some_random_value', false))->false();
         expect($s->checkPrefixedToken('xyz', 'custom_key', $token))->true();
     });
 }