Skip to content

Commit f20b282

Browse files
authored
Merge pull request #36 from vanushwashere/feature/max_token_length
Token length changes
2 parents 02aa893 + 72e6422 commit f20b282

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

config/magiclink.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
| Token size
99
|--------------------------------------------------------------------------
1010
|
11-
| Here you may specifiy the length of token to verify the identify.
11+
| Here you may specify the length of token to verify the identify.
12+
| Max value is 255 characters, it will be used if bigger value is set.
1213
|
1314
*/
1415
'length' => 64,

databases/migrations/2017_07_06_000000_create_table_magic_links.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create(config('magiclink.magiclink_table', 'magic_links'), function (Blueprint $table) {
1717
$table->uuid('id')->primary();
18-
$table->string('token', 100);
18+
$table->string('token', 255);
1919
$table->text('action');
2020
$table->unsignedTinyInteger('num_visits')->default(0);
2121
$table->unsignedTinyInteger('max_visits')->nullable();

src/MagicLink.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ protected static function boot()
2626
});
2727
}
2828

29+
protected static function getTokenLength()
30+
{
31+
return config('magiclink.token.length', 64) <= 255
32+
? config('magiclink.token.length', 64)
33+
: 255;
34+
}
35+
2936
public function getActionAttribute($value)
3037
{
3138
if ($this->getConnection()->getDriverName() === 'pgsql') {
@@ -66,7 +73,7 @@ public static function create(ActionInterface $action, ?int $lifetime = 4320, ?i
6673

6774
$magiclink = new static();
6875

69-
$magiclink->token = Str::random(config('magiclink.token.length', 64));
76+
$magiclink->token = Str::random(self::getTokenLength());
7077
$magiclink->available_at = $lifetime
7178
? Carbon::now()->addMinute($lifetime)
7279
: null;

tests/ConfigTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public function test_custom_token_length()
1818
$this->assertEquals(10, strlen($this->getTokenFromUrl($url)));
1919
}
2020

21+
public function test_custom_token_length_bigger_than_max_value()
22+
{
23+
$this->app['config']->set('magiclink.token.length', 256);
24+
25+
$url = MagicLink::create(new ResponseAction())->url;
26+
27+
$this->assertEquals(255, strlen($this->getTokenFromUrl($url)));
28+
}
29+
2130
protected function getTokenFromUrl($url)
2231
{
2332
$parts = explode(':', $url);

0 commit comments

Comments
 (0)