Skip to content

Commit 605be82

Browse files
committed
Changed token length maximum value
If bigger than max value of token length is set, max value will be used
1 parent 02aa893 commit 605be82

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ protected static function boot()
2626
});
2727
}
2828

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

6772
$magiclink = new static();
6873

69-
$magiclink->token = Str::random(config('magiclink.token.length', 64));
74+
$magiclink->token = Str::random(self::getTokenLength());
7075
$magiclink->available_at = $lifetime
7176
? Carbon::now()->addMinute($lifetime)
7277
: 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)