Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Added issuer config key plus other minor fixes.
  • Loading branch information
DarkGhostHunter authored May 9, 2020
2 parents 17909c1 + 21e32ff commit 5110363
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Help me support this package

ko_fi: DarkGhostHunter
custom: ['https://paypal.me/darkghosthunter']
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ return [
'expiration_days' => 14,
],
'secret_length' => 20,
'issuer' => env('APP_NAME', 'Laravel'),
'totp' => [
'digits' => 6,
'seconds' => 30,
Expand Down Expand Up @@ -386,6 +387,7 @@ It's recommended to use 128-bit or 160-bit because some Authenticator apps may h

```php
return [
'issuer' => env('APP_NAME', 'Laravel'),
'totp' => [
'digits' => 6,
'seconds' => 30,
Expand All @@ -397,10 +399,11 @@ return [

This controls TOTP code generation and verification mechanisms:

* Digits: The amount of digits to ask for TOTP code.
* Seconds: The number of seconds a code is considered valid.
* Window: Additional steps of seconds to keep a code as valid.
* Algorithm: The system-supported algorithm to handle code generation.
* Issuer: The name of the issuer of the TOTP. Default is the application name.
* TOTP Digits: The amount of digits to ask for TOTP code.
* TOTP Seconds: The number of seconds a code is considered valid.
* TOTP Window: Additional steps of seconds to keep a code as valid.
* TOTP Algorithm: The system-supported algorithm to handle code generation.

This configuration values are always passed down to the authentication app as URI parameters:

Expand Down
2 changes: 2 additions & 0 deletions config/laraguard.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
|
*/

'issuer' => env('APP_NAME', 'Laravel'),

'totp' => [
'digits' => 6,
'seconds' => 30,
Expand Down
2 changes: 2 additions & 0 deletions resources/views/notice.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<p class="text-center">
{{ __('To proceed, you need to enable Two Factor Authentication.') }}
</p>
@isset($url)
<div class="col-auto mb-3">
<a href="{{ $url }}" class="btn btn-primary btn-lg">
{{ __('Enable') }} &raquo;
</a>
</div>
@endisset
@endsection
4 changes: 2 additions & 2 deletions src/Eloquent/SerializesSharedSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ trait SerializesSharedSecret
public function toUri() : string
{
$query = http_build_query([
'issuer' => $issuer = rawurlencode(config('app.name')),
'issuer' => $issuer = config('laraguard.issuer') ?? config('app.name'),
'label' => $this->attributes['label'],
'secret' => $this->shared_secret,
'algorithm' => strtoupper($this->attributes['algorithm']),
'digits' => $this->attributes['digits'],
], null, '&', PHP_QUERY_RFC3986);

return "otpauth://totp/$issuer%3A{$this->attributes['label']}?$query";
return 'otpauth://totp/' . rawurlencode($issuer) . '%3A' . $this->attributes['label'] . "?$query";
}

/**
Expand Down
26 changes: 22 additions & 4 deletions tests/Eloquent/TwoFactorAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Tests\Eloquent;

use Carbon\Carbon;
use Tests\RunsPublishableMigrations;
use Tests\RegistersPackage;
use Orchestra\Testbench\TestCase;
use ParagonIE\ConstantTime\Base32;
use Tests\Stubs\UserTwoFactorStub;
use Tests\RunsPublishableMigrations;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Foundation\Testing\DatabaseMigrations;
Expand Down Expand Up @@ -269,7 +269,7 @@ public function test_serializes_to_grouped_string()

public function test_serializes_to_uri()
{
config(['app.name' => 'quz']);
config(['laraguard.issuer' => 'quz']);

$tfa = factory(TwoFactorAuthentication::class)->states('with recovery', 'with safe devices')->make([
'label' => '[email protected]',
Expand All @@ -285,7 +285,7 @@ public function test_serializes_to_uri()

public function test_serializes_to_qr_and_renders_to_qr()
{
config(['app.name' => 'quz']);
config(['laraguard.issuer' => 'quz']);

$tfa = factory(TwoFactorAuthentication::class)->states('with recovery', 'with safe devices')->make([
'label' => '[email protected]',
Expand All @@ -300,7 +300,7 @@ public function test_serializes_to_qr_and_renders_to_qr()

public function test_serializes_uri_to_json()
{
config(['app.name' => 'quz']);
config(['laraguard.issuer' => 'quz']);

$tfa = factory(TwoFactorAuthentication::class)->states('with recovery', 'with safe devices')->make([
'label' => '[email protected]',
Expand All @@ -315,4 +315,22 @@ public function test_serializes_uri_to_json()
$this->assertEquals($uri, $tfa->toJson());
$this->assertEquals($uri, json_encode($tfa));
}

public function test_changes_issuer()
{
config(['laraguard.issuer' => 'foo bar']);

$tfa = factory(TwoFactorAuthentication::class)->states('with recovery', 'with safe devices')->make([
'label' => '[email protected]',
'shared_secret' => 'KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3',
'algorithm' => 'sHa256',
'digits' => 14,
]);

$uri = '"otpauth:\/\/totp\/foo%20bar%[email protected]?issuer=foo%20bar&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14"';

$this->assertJson($tfa->toJson());
$this->assertEquals($uri, $tfa->toJson());
$this->assertEquals($uri, json_encode($tfa));
}
}

2 comments on commit 5110363

@danieljausovec
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change env variable in config for issuer with something else, that developers can easier add variable to own .env and change value.
You already set the default value with app name in toUri function.

I suggest OTP_TOTP_ISSUER

Good job btw :)

@DarkGhostHunter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change env variable in config for issuer with something else, that developers can easier add variable to own .env and change value.
You already set the default value with app name in toUri function.

I suggest OTP_TOTP_ISSUER

Good job btw :)

After consideration, I will add it since it may become very handful when testing on different environments. Ill add it on 1.3.1

Please sign in to comment.