Prefill icon field with imageLink resource

This commit is contained in:
Bubka 2020-11-18 23:48:51 +01:00
parent b39b55fc0e
commit 101a26b035
7 changed files with 29 additions and 9 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
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 @@ class QrCodeController extends Controller
$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);
}

View File

@ -44,7 +44,6 @@ class TwoFAccountController extends Controller
'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 @@ class TwoFAccountController extends Controller
'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

View File

@ -164,9 +164,6 @@ class TwoFAccount extends Model implements Sortable
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([

View File

@ -111,8 +111,6 @@
<form-field v-if="form.otpType === 'totp'" :form="form" fieldName="totpPeriod" inputType="text" :label="$t('twofaccounts.forms.totpPeriod.label')" :placeholder="$t('twofaccounts.forms.totpPeriod.placeholder')" :help="$t('twofaccounts.forms.totpPeriod.help')" />
<!-- HOTP counter -->
<form-field v-if="form.otpType === 'hotp'" :form="form" fieldName="hotpCounter" inputType="text" :label="$t('twofaccounts.forms.hotpCounter.label')" :placeholder="$t('twofaccounts.forms.hotpCounter.placeholder')" :help="$t('twofaccounts.forms.hotpCounter.help')" />
<!-- image link -->
<form-field :form="form" fieldName="imageLink" inputType="text" :label="$t('twofaccounts.forms.image_link.label')" :placeholder="$t('twofaccounts.forms.image_link.placeholder')" :help="$t('twofaccounts.forms.image_link.help')" />
</div>
<vue-footer :showButtons="true">
<p class="control">
@ -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
},

View File

@ -83,8 +83,6 @@
<field-error :form="form" field="uri" class="help-for-file" />
<p class="help" v-html="$t('twofaccounts.forms.hotpCounter.help_lock')"></p>
</div>
<!-- image link -->
<form-field :form="form" fieldName="imageLink" inputType="text" :label="$t('twofaccounts.forms.image_link.label')" :placeholder="$t('twofaccounts.forms.image_link.placeholder')" :help="$t('twofaccounts.forms.image_link.help')" />
</div>
<!-- form buttons -->
<vue-footer :showButtons="true">

View File

@ -1,4 +1,5 @@
*
!imagesLink/
!public/
!qrcodes/
!.gitignore

2
storage/app/imagesLink/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore