Skip to content

Commit

Permalink
Merge pull request #5 from Erwane/8-chars-id
Browse files Browse the repository at this point in the history
8 binary chars id
  • Loading branch information
Erwane authored Jul 8, 2019
2 parents a6d0338 + 9121a6d commit 80affa4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
21 changes: 21 additions & 0 deletions config/Migrations/20190708113200_BinaryId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Migrations\AbstractMigration;
use Phinx\Db\Adapter\MysqlAdapter;

class BinaryId extends AbstractMigration
{
public function up()
{
$table = $this->table('token_tokens');

$table
->alterColumn('id', 'string', ['limit' => 8, 'collation' => 'utf8_bin'])
->save();
}

public function down()
{
$table = $this->table('token_tokens');
}
}
4 changes: 2 additions & 2 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ parameters:
tasks:
phplint: ~
phpcs:
report: full
standard: CakePHP
encoding: UTF-8
show_warnings: false
whitelist_patterns:
- /^src\/(.*)/
- /^src\/(.*\.php)/
39 changes: 19 additions & 20 deletions src/Model/Table/TokensTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -13,7 +13,7 @@ class TokensTable extends Table
*/
protected function _initializeSchema(TableSchema $schema)
{
$schema->columnType('content', 'json');
$schema->setColumnType('content', 'json');

return $schema;
}
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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),
]);
Expand All @@ -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;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/TestCase/Model/Table/TokensTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TokensTableTest extends TestCase
* @var array
*/
public $fixtures = [
'plugin.token.tokens',
'plugin.Token.Tokens',
];

/**
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TokenTest extends TestCase
* @var array
*/
public $fixtures = [
'plugin.token.tokens',
'plugin.Token.Tokens',
];

public function testReadExpired()
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'wwwRoot' => WWW_ROOT
]);

Cache::config([
Cache::setConfig([
'default' => [
'className' => 'File',
'path' => CACHE,
Expand Down Expand Up @@ -74,7 +74,7 @@

Configure::write('debug', true);

ConnectionManager::config([
ConnectionManager::setConfig([
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
Expand Down

0 comments on commit 80affa4

Please sign in to comment.