Skip to content

Commit 80affa4

Browse files
authored
Merge pull request #5 from Erwane/8-chars-id
8 binary chars id
2 parents a6d0338 + 9121a6d commit 80affa4

7 files changed

Lines changed: 68 additions & 32 deletions

File tree

composer.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
],
1313
"require": {
1414
"php": ">=5.6",
15-
"cakephp/cakephp": "^3.4.0"
15+
"cakephp/cakephp": "^3.7.0"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "6.*",
19-
"cakephp/cakephp-codesniffer" : "dev-master",
20-
"phpro/grumphp": "^0.11.3",
21-
"jakub-onderka/php-parallel-lint": "^0.9.2"
18+
"phpunit/phpunit": "^6.0",
19+
"cakephp/cakephp-codesniffer" : "^3",
20+
"jakub-onderka/php-parallel-lint": "^1.0",
21+
"phpro/grumphp": "^0.15"
2222
},
2323

2424
"autoload": {
@@ -33,5 +33,15 @@
3333
}
3434
},
3535

36-
"minimum-stability": "beta"
36+
"scripts": {
37+
"post-install-cmd": [
38+
"if [ $COMPOSER_DEV_MODE -eq 1 ]; then phpcs --config-set installed_paths vendor/cakephp/cakephp-codesniffer; fi"
39+
],
40+
"post-update-cmd": [
41+
"if [ $COMPOSER_DEV_MODE -eq 1 ]; then phpcs --config-set installed_paths vendor/cakephp/cakephp-codesniffer; fi"
42+
],
43+
"csfix": "phpcbf --standard=CakePHP --encoding=UTF-8 --report=full--colors --extensions=php src/ InterfaceAdmin/ InterfaceReseller/ InterfaceUser/"
44+
},
45+
46+
"minimum-stability": "stable"
3747
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Migrations\AbstractMigration;
4+
use Phinx\Db\Adapter\MysqlAdapter;
5+
6+
class BinaryId extends AbstractMigration
7+
{
8+
public function up()
9+
{
10+
$table = $this->table('token_tokens');
11+
12+
$table
13+
->alterColumn('id', 'string', ['limit' => 8, 'collation' => 'utf8_bin'])
14+
->save();
15+
}
16+
17+
public function down()
18+
{
19+
$table = $this->table('token_tokens');
20+
}
21+
}

grumphp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ parameters:
77
tasks:
88
phplint: ~
99
phpcs:
10+
report: full
1011
standard: CakePHP
1112
encoding: UTF-8
12-
show_warnings: false
1313
whitelist_patterns:
14-
- /^src\/(.*)/
14+
- /^src\/(.*\.php)/

src/Model/Table/TokensTable.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Cake\Chronos\Chronos;
55
use Cake\Database\Schema\TableSchema;
66
use Cake\ORM\Table;
7-
use Cake\Utility\Text;
7+
use Cake\Utility\Security;
88

99
class TokensTable extends Table
1010
{
@@ -13,7 +13,7 @@ class TokensTable extends Table
1313
*/
1414
protected function _initializeSchema(TableSchema $schema)
1515
{
16-
$schema->columnType('content', 'json');
16+
$schema->setColumnType('content', 'json');
1717

1818
return $schema;
1919
}
@@ -24,10 +24,10 @@ protected function _initializeSchema(TableSchema $schema)
2424
public function initialize(array $config)
2525
{
2626
parent::initialize($config);
27-
$this->setTable('token_tokens');
28-
$this->setPrimaryKey('id');
2927

30-
$this->addBehavior('Timestamp');
28+
$this->setTable('token_tokens')
29+
->setPrimaryKey('id')
30+
->addBehavior('Timestamp');
3131
}
3232

3333
/**
@@ -56,7 +56,7 @@ public function read($id)
5656
public function newToken(array $content = [], $expire = null)
5757
{
5858
$entity = $this->newEntity([
59-
'id' => $this->uniqId(),
59+
'id' => $this->_uniqId(),
6060
'content' => $content,
6161
'expire' => is_null($expire) ? Chronos::parse('+1 day') : Chronos::parse($expire),
6262
]);
@@ -70,25 +70,24 @@ public function newToken(array $content = [], $expire = null)
7070
* generate uniq token id
7171
* @return string
7272
*/
73-
protected function uniqId()
73+
protected function _uniqId()
7474
{
7575
$exists = true;
7676

77-
while ($exists) {
78-
$key = $this->generateKey();
79-
$exists = $this->find()->where(['id' => $key])->first();
80-
}
77+
$length = 8;
8178

82-
return $key;
83-
}
79+
do {
80+
// generate random
81+
$random = base64_encode(Security::randomBytes($length * 4));
8482

85-
/**
86-
* generate random key
87-
* @return string 8 chars key
88-
*/
89-
protected function generateKey()
90-
{
91-
return substr(hash('sha256', Text::uuid()), 0, 8);
83+
// cleanup
84+
$clean = preg_replace('/[^A-Za-z0-9]/', '', $random);
85+
86+
// random part length
87+
$key = substr($clean, random_int(1, $length * 2), $length);
88+
} while ($this->exists(['id' => $key]));
89+
90+
return $key;
9291
}
9392

9493
/**

tests/TestCase/Model/Table/TokensTableTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TokensTableTest extends TestCase
2424
* @var array
2525
*/
2626
public $fixtures = [
27-
'plugin.token.tokens',
27+
'plugin.Token.Tokens',
2828
];
2929

3030
/**
@@ -64,6 +64,12 @@ public function testReadExists()
6464
$this->assertSame('abcde123', $entity->id);
6565
}
6666

67+
public function testReadExistsBInary()
68+
{
69+
$entity = $this->Tokens->read('abcdE123');
70+
$this->assertNull($entity);
71+
}
72+
6773
public function testReadContent()
6874
{
6975
$entity = $this->Tokens->read('abcde789');

tests/TestCase/TokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TokenTest extends TestCase
1515
* @var array
1616
*/
1717
public $fixtures = [
18-
'plugin.token.tokens',
18+
'plugin.Token.Tokens',
1919
];
2020

2121
public function testReadExpired()

tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
'wwwRoot' => WWW_ROOT
4747
]);
4848

49-
Cache::config([
49+
Cache::setConfig([
5050
'default' => [
5151
'className' => 'File',
5252
'path' => CACHE,
@@ -74,7 +74,7 @@
7474

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

77-
ConnectionManager::config([
77+
ConnectionManager::setConfig([
7878
'test' => [
7979
'className' => 'Cake\Database\Connection',
8080
'driver' => 'Cake\Database\Driver\Sqlite',

0 commit comments

Comments
 (0)