Skip to content

Commit 0e3949e

Browse files
Merge pull request #51 from Laragear/fix/empty-issuer
2 parents 676e794 + ebc453f commit 0e3949e

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/Models/Concerns/SerializesSharedSecret.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BaconQrCode\Renderer\ImageRenderer;
77
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
88
use BaconQrCode\Writer;
9+
use InvalidArgumentException;
910

1011
use function array_values;
1112
use function chunk_split;
@@ -24,8 +25,9 @@ trait SerializesSharedSecret
2425
*/
2526
public function toUri(): string
2627
{
27-
$issuer = config('two-factor.issuer', config('app.name'));
28-
28+
$issuer = config('two-factor.issuer')
29+
?: config('app.name')
30+
?: throw new InvalidArgumentException('The TOTP issuer cannot be empty.');
2931
$query = http_build_query([
3032
'issuer' => $issuer,
3133
'label' => $this->attributes['label'],

src/Models/TwoFactorAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function authenticatable(): MorphTo
8282
/**
8383
* Sets the Algorithm to lowercase.
8484
*
85-
* @param $value
85+
* @param $value
8686
* @return void
8787
*/
8888
protected function setAlgorithmAttribute($value): void

tests/Eloquent/TwoFactorAuthenticationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\RefreshDatabase;
77
use Illuminate\Support\Facades\Cache;
88
use Illuminate\Support\Facades\Date;
9+
use InvalidArgumentException;
910
use Laragear\TwoFactor\Models\TwoFactorAuthentication;
1011
use ParagonIE\ConstantTime\Base32;
1112
use Tests\Stubs\UserStub;
@@ -315,6 +316,20 @@ public function test_serializes_uri_to_json(): void
315316
static::assertEquals($uri, $tfa->toJson());
316317
}
317318

319+
public function test_uses_app_name_as_issuer(): void
320+
{
321+
$tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([
322+
'label' => 'test@foo.com',
323+
'shared_secret' => static::SECRET,
324+
'algorithm' => 'sHa256',
325+
'digits' => 14,
326+
]);
327+
328+
$uri = 'otpauth://totp/Laravel%3Atest@foo.com?issuer=Laravel&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14';
329+
330+
static::assertSame($uri, $tfa->toUri());
331+
}
332+
318333
public function test_changes_issuer(): void
319334
{
320335
$this->app->make('config')->set('two-factor.issuer', 'foo bar');
@@ -331,6 +346,24 @@ public function test_changes_issuer(): void
331346
static::assertSame($uri, $tfa->toUri());
332347
}
333348

349+
public function test_throws_exception_when_issuer_is_empty(): void
350+
{
351+
$this->app->make('config')->set('app.name', '');
352+
$this->app->make('config')->set('two-factor.issuer', '');
353+
354+
$tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([
355+
'label' => 'test@foo.com',
356+
'shared_secret' => static::SECRET,
357+
'algorithm' => 'sHa256',
358+
'digits' => 14,
359+
]);
360+
361+
$this->expectException(InvalidArgumentException::class);
362+
$this->expectExceptionMessage('The TOTP issuer cannot be empty.');
363+
364+
$tfa->toUri();
365+
}
366+
334367
public function test_uses_custom_generator(): void
335368
{
336369
$i = 0;

0 commit comments

Comments
 (0)