-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10831e4
commit a0535d8
Showing
6 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/** | ||
* Platine Framework | ||
* | ||
* Platine Framework is a lightweight, high-performance, simple and elegant PHP | ||
* Web framework | ||
* | ||
* This content is released under the MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Platine Framework | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* @file CsrfUserSessionStorage.php | ||
* | ||
* The CSRF storage based on user session | ||
* | ||
* @package Platine\Framework\Security\Csrf\Storage | ||
* @author Platine Developers team | ||
* @copyright Copyright (c) 2020 | ||
* @license http://opensource.org/licenses/MIT MIT License | ||
* @link https://www.platine-php.com | ||
* @version 1.0.0 | ||
* @filesource | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Platine\Framework\Security\Csrf\Storage; | ||
|
||
/** | ||
* @class CsrfUserSessionStorage | ||
* @package Platine\Framework\Security\Csrf\Storage | ||
*/ | ||
class CsrfUserSessionStorage extends CsrfSessionStorage | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $name): ?string | ||
{ | ||
$key = $this->getKeyName($name); | ||
$token = $this->session->get($key, null); | ||
|
||
return $token; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set(string $name, string $token, int $expire): void | ||
{ | ||
$key = $this->getKeyName($name); | ||
$this->session->set($key, $token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/Security/Csrf/Storage/CsrfUserSessionStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Platine\Test\Framework\Security\Csrf\Storage; | ||
|
||
use Platine\Dev\PlatineTestCase; | ||
use Platine\Framework\Security\Csrf\Storage\CsrfSessionStorage; | ||
use Platine\Framework\Security\Csrf\Storage\CsrfUserSessionStorage; | ||
use Platine\Session\Session; | ||
|
||
/* | ||
* @group core | ||
* @group security | ||
*/ | ||
class CsrfUserSessionStorageTest extends PlatineTestCase | ||
{ | ||
public function testConstruct(): void | ||
{ | ||
$session = new Session(); | ||
|
||
$o = new CsrfUserSessionStorage($session); | ||
|
||
$this->assertInstanceOf(CsrfUserSessionStorage::class, $o); | ||
} | ||
|
||
public function testGetNull(): void | ||
{ | ||
$session = new Session(); | ||
|
||
$o = new CsrfUserSessionStorage($session); | ||
|
||
$this->assertNull($o->get('token')); | ||
} | ||
|
||
public function testGetDeleteClearSuccess(): void | ||
{ | ||
$_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY] = ['token' => 'foobar']; | ||
$session = new Session(); | ||
|
||
$o = new CsrfUserSessionStorage($session); | ||
|
||
$res = $o->get('token'); | ||
$this->assertEquals('foobar', $res); | ||
$this->assertCount(1, $_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
$o->delete('token'); | ||
|
||
$this->assertNull($o->get('token')); | ||
$this->assertIsArray($_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
$this->assertCount(0, $_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
|
||
$o->clear(); | ||
$this->assertNull($o->get('token')); | ||
$this->assertArrayNotHasKey(CsrfSessionStorage::CSRF_SESSION_KEY, $_SESSION); | ||
} | ||
|
||
public function testSet(): void | ||
{ | ||
$session = new Session(); | ||
|
||
$o = new CsrfUserSessionStorage($session); | ||
|
||
$this->assertNull($o->get('token')); | ||
$this->assertArrayNotHasKey(CsrfSessionStorage::CSRF_SESSION_KEY, $_SESSION); | ||
|
||
$o->set('token', 'foobar', time() + 100); | ||
|
||
$res = $o->get('token'); | ||
$this->assertEquals('foobar', $res); | ||
$this->assertCount(1, $_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
$o->delete('token'); | ||
|
||
$this->assertNull($o->get('token')); | ||
$this->assertIsArray($_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
$this->assertCount(0, $_SESSION[CsrfSessionStorage::CSRF_SESSION_KEY]); | ||
|
||
$o->clear(); | ||
$this->assertNull($o->get('token')); | ||
$this->assertArrayNotHasKey(CsrfSessionStorage::CSRF_SESSION_KEY, $_SESSION); | ||
} | ||
} |