Fix getOfficialIcons being ignored & Add relevant tests - Fix #194

This commit is contained in:
Bubka 2023-06-07 16:24:06 +02:00
parent d4d1eb5276
commit d902e3ecae
4 changed files with 65 additions and 22 deletions

View File

@ -17,6 +17,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@ -424,7 +425,7 @@ public function fillWithOtpParameters(array $parameters, bool $skipIconFetching
$this->enforceAsSteam();
}
if (! $this->icon && $this->shouldGetOfficialIcon() && ! $skipIconFetching) {
if (! $this->icon && ! $skipIconFetching) {
$this->icon = $this->getDefaultIcon();
}
@ -476,7 +477,7 @@ public function fillWithURI(string $uri, bool $isSteamTotp = false, bool $skipIc
self::setIcon($this->generator->getParameter('image'));
}
if (! $this->icon && $this->shouldGetOfficialIcon() && ! $skipIconFetching) {
if (! $this->icon && ! $skipIconFetching) {
$this->icon = $this->getDefaultIcon();
}
@ -704,25 +705,17 @@ private function storeRemoteImageAsIcon(string $url) : string|null
}
/**
* Fetch a logo in the tfa directory and store it as a new stand alone icon
* Triggers logo fetching if necessary
*
* @return string|null The icon
*/
private function getDefaultIcon()
{
$logoService = App::make(LogoService::class);
// $logoService = App::make(LogoService::class);
return $this->shouldGetOfficialIcon() ? $logoService->getIcon($this->service) : null;
}
/**
* Tells if an official icon should be fetched
*/
private function shouldGetOfficialIcon() : bool
{
return is_null($this->user)
? (bool) config('2fauth.preferences.getOfficialIcons')
: (bool) $this->user->preferences['getOfficialIcons'];
return (bool) Auth::user()?->preferences['getOfficialIcons']
? App::make(LogoService::class)->getIcon($this->service)
: null;
}
/**

View File

@ -86,6 +86,18 @@ class OtpTestData
'counter' => null,
];
const ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP_NO_ICON = [
'service' => self::SERVICE,
'account' => self::ACCOUNT,
'icon' => null,
'otp_type' => 'totp',
'secret' => self::SECRET,
'digits' => self::DIGITS_CUSTOM,
'algorithm' => self::ALGORITHM_CUSTOM,
'period' => self::PERIOD_CUSTOM,
'counter' => null,
];
const ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP = [
'account' => self::ACCOUNT,
'otp_type' => 'totp',

View File

@ -4,9 +4,11 @@
use App\Models\TwoFAccount;
use App\Models\User;
use App\Services\LogoService;
use Illuminate\Http\Testing\FileFactory;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Mockery\MockInterface;
use Tests\Data\HttpRequestTestData;
use Tests\Data\OtpTestData;
use Tests\FeatureTestCase;
@ -44,7 +46,9 @@ public function setUp() : void
{
parent::setUp();
/** @var \Illuminate\Contracts\Auth\Authenticatable $user */
$this->user = User::factory()->create();
$this->actingAs($this->user, 'api-guard');
$this->customTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
'legacy_uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
@ -234,6 +238,47 @@ public function test_fill_with_uri_without_label_returns_ValidationException()
$twofaccount->fillWithURI('otpauth://totp/?secret=' . OtpTestData::SECRET);
}
/**
* @test
*/
public function test_fill_with_getOfficialIcons_On_triggers_icon_fetching()
{
// Set the getOfficialIcons preference On
$this->user['preferences->getOfficialIcons'] = true;
$this->user->save();
$this->mock(LogoService::class, function (MockInterface $logoService) {
$logoService->expects()
->getIcon(OtpTestData::SERVICE)
->twice()
->andReturn(null);
});
$twofaccount = new TwoFAccount;
$twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI_NO_IMG);
$twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP_NO_ICON);
}
/**
* @test
*/
public function test_fill_with_getOfficialIcons_Off_skips_icon_fetching()
{
// Set the getOfficialIcons preference Off
$this->user['preferences->getOfficialIcons'] = false;
$this->user->save();
$this->mock(LogoService::class, function (MockInterface $logoService) {
$logoService->shouldNotReceive('getIcon');
});
$twofaccount = new TwoFAccount;
$twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI_NO_IMG);
$twofaccount = new TwoFAccount;
$twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP);
}
/**
* @test
*/

View File

@ -7,7 +7,6 @@
use App\Exceptions\UnsupportedMigrationException;
use App\Factories\MigratorFactory;
use App\Models\TwoFAccount;
use App\Services\LogoService;
use App\Services\Migrators\AegisMigrator;
use App\Services\Migrators\GoogleAuthMigrator;
use App\Services\Migrators\Migrator;
@ -74,12 +73,6 @@ public function setUp() : void
->andReturn(false);
});
$this->mock(LogoService::class, function (MockInterface $logoService) {
$logoService->allows([
'getIcon' => null,
]);
});
$this->totpTwofaccount = new TwoFAccount;
$this->totpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI_NO_IMG;
$this->totpTwofaccount->service = OtpTestData::SERVICE;