From 101a26b03578b480b937a35732ff5ffeab080856 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Wed, 18 Nov 2020 23:48:51 +0100 Subject: [PATCH] Prefill icon field with imageLink resource --- app/Http/Controllers/QrCodeController.php | 24 +++++++++++++++++++ .../Controllers/TwoFAccountController.php | 2 -- app/TwoFAccount.php | 3 --- resources/js/views/twofaccounts/Create.vue | 4 ++-- resources/js/views/twofaccounts/Edit.vue | 2 -- storage/app/.gitignore | 1 + storage/app/imagesLink/.gitignore | 2 ++ 7 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 storage/app/imagesLink/.gitignore diff --git a/app/Http/Controllers/QrCodeController.php b/app/Http/Controllers/QrCodeController.php index e42ddc2f..bda327e2 100644 --- a/app/Http/Controllers/QrCodeController.php +++ b/app/Http/Controllers/QrCodeController.php @@ -5,6 +5,7 @@ use Zxing\QrReader; use App\TwoFAccount; use App\Classes\Options; +use Illuminate\Support\Str; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use chillerlan\QRCode\{QRCode, QROptions}; @@ -71,6 +72,29 @@ public function decode(Request $request) $twofaccount = new TwoFAccount; $twofaccount->uri = $uri; + // When present, use the imageLink parameter to prefill the icon field + if( $twofaccount->imageLink ) { + + $chunks = explode('.', $twofaccount->imageLink); + $hashFilename = Str::random(40) . '.' . end($chunks); + + try { + + Storage::disk('local')->put('imagesLink/' . $hashFilename, file_get_contents($twofaccount->imageLink)); + + if( in_array(Storage::mimeType('imagesLink/' . $hashFilename), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) ) { + if( getimagesize(storage_path() . '/app/imagesLink/' . $hashFilename) ) { + + Storage::move('imagesLink/' . $hashFilename, 'public/icons/' . $hashFilename); + $twofaccount->icon = $hashFilename; + } + } + } + catch( Exception $e ) { + $twofaccount->imageLink = null; + } + } + return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200); } diff --git a/app/Http/Controllers/TwoFAccountController.php b/app/Http/Controllers/TwoFAccountController.php index bb518013..e1435bf3 100644 --- a/app/Http/Controllers/TwoFAccountController.php +++ b/app/Http/Controllers/TwoFAccountController.php @@ -44,7 +44,6 @@ public function store(Request $request) 'algorithm' => 'nullable|in:sha1,sha256,sha512,md5', 'totpPeriod' => 'nullable|integer|min:1', 'hotpCounter' => 'nullable|integer|min:0', - 'imageLink' => 'nullable|url', ]); // Two possible cases : @@ -187,7 +186,6 @@ public function update(Request $request, $id) 'algorithm' => 'nullable|in:sha1,sha256,sha512,md5', 'totpPeriod' => 'required_if:otpType,totp|nullable|integer|min:1', 'hotpCounter' => 'required_if:otpType,hotp|nullable|integer|min:0', - 'imageLink' => 'nullable|url', ]); // Here we catch a possible missing model exception in order to diff --git a/app/TwoFAccount.php b/app/TwoFAccount.php index c7c6ee7c..47c8e197 100644 --- a/app/TwoFAccount.php +++ b/app/TwoFAccount.php @@ -164,9 +164,6 @@ public function populate(Array $attrib = []) if (array_key_exists('hotpCounter', $attrib) && $attrib['hotpCounter'] && $attrib['otpType'] === 'hotp') { $this->otp->setParameter( 'counter', (int) $attrib['hotpCounter'] ); } - if (array_key_exists('imageLink', $attrib) && $attrib['imageLink']) - { $this->otp->setParameter( 'image', $attrib['imageLink'] ); } - } catch (\Exception $e) { throw \Illuminate\Validation\ValidationException::withMessages([ diff --git a/resources/js/views/twofaccounts/Create.vue b/resources/js/views/twofaccounts/Create.vue index 3961ce32..a16d208a 100644 --- a/resources/js/views/twofaccounts/Create.vue +++ b/resources/js/views/twofaccounts/Create.vue @@ -111,8 +111,6 @@ - -

@@ -183,6 +181,7 @@ if( this.$route.params.qrAccount ) { this.form.fill(this.$route.params.qrAccount) + this.tempIcon = this.$route.params.qrAccount.icon ? this.$route.params.qrAccount.icon : null this.isQuickForm = true } @@ -241,6 +240,7 @@ this.form.fill(data) this.form.secretIsBase32Encoded = 1 + this.tempIcon = data.icon ? data.icon : null this.form.uri = '' // we don't want the uri because the user can change any otp parameter in the form }, diff --git a/resources/js/views/twofaccounts/Edit.vue b/resources/js/views/twofaccounts/Edit.vue index 2b448a1b..76708b1b 100644 --- a/resources/js/views/twofaccounts/Edit.vue +++ b/resources/js/views/twofaccounts/Edit.vue @@ -83,8 +83,6 @@

- - diff --git a/storage/app/.gitignore b/storage/app/.gitignore index 45aea3e4..f709f265 100644 --- a/storage/app/.gitignore +++ b/storage/app/.gitignore @@ -1,4 +1,5 @@ * +!imagesLink/ !public/ !qrcodes/ !.gitignore diff --git a/storage/app/imagesLink/.gitignore b/storage/app/imagesLink/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/storage/app/imagesLink/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore