diff --git a/composer.json b/composer.json index 7961ef9..51dafa1 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,13 @@ ], "require": { "php": ">=5.6", - "cakephp/cakephp": "^3.4.0" + "cakephp/cakephp": "^3.7.0" }, "require-dev": { - "phpunit/phpunit": "6.*", - "cakephp/cakephp-codesniffer" : "dev-master", - "phpro/grumphp": "^0.11.3", - "jakub-onderka/php-parallel-lint": "^0.9.2" + "phpunit/phpunit": "^6.0", + "cakephp/cakephp-codesniffer" : "^3", + "jakub-onderka/php-parallel-lint": "^1.0", + "phpro/grumphp": "^0.15" }, "autoload": { @@ -33,5 +33,15 @@ } }, - "minimum-stability": "beta" + "scripts": { + "post-install-cmd": [ + "if [ $COMPOSER_DEV_MODE -eq 1 ]; then phpcs --config-set installed_paths vendor/cakephp/cakephp-codesniffer; fi" + ], + "post-update-cmd": [ + "if [ $COMPOSER_DEV_MODE -eq 1 ]; then phpcs --config-set installed_paths vendor/cakephp/cakephp-codesniffer; fi" + ], + "csfix": "phpcbf --standard=CakePHP --encoding=UTF-8 --report=full--colors --extensions=php src/ InterfaceAdmin/ InterfaceReseller/ InterfaceUser/" + }, + + "minimum-stability": "stable" } diff --git a/config/Migrations/20190708113200_BinaryId.php b/config/Migrations/20190708113200_BinaryId.php new file mode 100644 index 0000000..809828c --- /dev/null +++ b/config/Migrations/20190708113200_BinaryId.php @@ -0,0 +1,21 @@ +table('token_tokens'); + + $table + ->alterColumn('id', 'string', ['limit' => 8, 'collation' => 'utf8_bin']) + ->save(); + } + + public function down() + { + $table = $this->table('token_tokens'); + } +} diff --git a/grumphp.yml b/grumphp.yml index 3cc477f..fdcbbb7 100644 --- a/grumphp.yml +++ b/grumphp.yml @@ -7,8 +7,8 @@ parameters: tasks: phplint: ~ phpcs: + report: full standard: CakePHP encoding: UTF-8 - show_warnings: false whitelist_patterns: - - /^src\/(.*)/ + - /^src\/(.*\.php)/ diff --git a/src/Model/Table/TokensTable.php b/src/Model/Table/TokensTable.php index e2bf377..72a1fbc 100644 --- a/src/Model/Table/TokensTable.php +++ b/src/Model/Table/TokensTable.php @@ -4,7 +4,7 @@ use Cake\Chronos\Chronos; use Cake\Database\Schema\TableSchema; use Cake\ORM\Table; -use Cake\Utility\Text; +use Cake\Utility\Security; class TokensTable extends Table { @@ -13,7 +13,7 @@ class TokensTable extends Table */ protected function _initializeSchema(TableSchema $schema) { - $schema->columnType('content', 'json'); + $schema->setColumnType('content', 'json'); return $schema; } @@ -24,10 +24,10 @@ protected function _initializeSchema(TableSchema $schema) public function initialize(array $config) { parent::initialize($config); - $this->setTable('token_tokens'); - $this->setPrimaryKey('id'); - $this->addBehavior('Timestamp'); + $this->setTable('token_tokens') + ->setPrimaryKey('id') + ->addBehavior('Timestamp'); } /** @@ -56,7 +56,7 @@ public function read($id) public function newToken(array $content = [], $expire = null) { $entity = $this->newEntity([ - 'id' => $this->uniqId(), + 'id' => $this->_uniqId(), 'content' => $content, 'expire' => is_null($expire) ? Chronos::parse('+1 day') : Chronos::parse($expire), ]); @@ -70,25 +70,24 @@ public function newToken(array $content = [], $expire = null) * generate uniq token id * @return string */ - protected function uniqId() + protected function _uniqId() { $exists = true; - while ($exists) { - $key = $this->generateKey(); - $exists = $this->find()->where(['id' => $key])->first(); - } + $length = 8; - return $key; - } + do { + // generate random + $random = base64_encode(Security::randomBytes($length * 4)); - /** - * generate random key - * @return string 8 chars key - */ - protected function generateKey() - { - return substr(hash('sha256', Text::uuid()), 0, 8); + // cleanup + $clean = preg_replace('/[^A-Za-z0-9]/', '', $random); + + // random part length + $key = substr($clean, random_int(1, $length * 2), $length); + } while ($this->exists(['id' => $key])); + + return $key; } /** diff --git a/tests/TestCase/Model/Table/TokensTableTest.php b/tests/TestCase/Model/Table/TokensTableTest.php index b44fae6..79dbacb 100644 --- a/tests/TestCase/Model/Table/TokensTableTest.php +++ b/tests/TestCase/Model/Table/TokensTableTest.php @@ -24,7 +24,7 @@ class TokensTableTest extends TestCase * @var array */ public $fixtures = [ - 'plugin.token.tokens', + 'plugin.Token.Tokens', ]; /** @@ -64,6 +64,12 @@ public function testReadExists() $this->assertSame('abcde123', $entity->id); } + public function testReadExistsBInary() + { + $entity = $this->Tokens->read('abcdE123'); + $this->assertNull($entity); + } + public function testReadContent() { $entity = $this->Tokens->read('abcde789'); diff --git a/tests/TestCase/TokenTest.php b/tests/TestCase/TokenTest.php index f5619b6..5fc226f 100644 --- a/tests/TestCase/TokenTest.php +++ b/tests/TestCase/TokenTest.php @@ -15,7 +15,7 @@ class TokenTest extends TestCase * @var array */ public $fixtures = [ - 'plugin.token.tokens', + 'plugin.Token.Tokens', ]; public function testReadExpired() diff --git a/tests/bootstrap.php b/tests/bootstrap.php index de0059f..0d54675 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -46,7 +46,7 @@ 'wwwRoot' => WWW_ROOT ]); -Cache::config([ +Cache::setConfig([ 'default' => [ 'className' => 'File', 'path' => CACHE, @@ -74,7 +74,7 @@ Configure::write('debug', true); -ConnectionManager::config([ +ConnectionManager::setConfig([ 'test' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Sqlite',