From 7bdd286fb25a9a816e1d73d9bebc5d61d45e18be Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Mon, 16 Nov 2020 14:45:24 +0100 Subject: [PATCH] Update Edit form to match with the new TwoFAccount model --- .../Controllers/TwoFAccountController.php | 43 ++--- resources/js/views/twofaccounts/Edit.vue | 165 ++++++++++++++---- resources/lang/en/twofaccounts.php | 4 +- 3 files changed, 145 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/TwoFAccountController.php b/app/Http/Controllers/TwoFAccountController.php index 1b9090e3..3095748b 100644 --- a/app/Http/Controllers/TwoFAccountController.php +++ b/app/Http/Controllers/TwoFAccountController.php @@ -40,7 +40,7 @@ class TwoFAccountController extends Controller 'account' => 'required_without:uri|nullable|string|regex:/^[^:]+$/i', 'icon' => 'nullable|string', 'uri' => 'nullable|string|regex:/^otpauth:\/\/[h,t]otp\//i', - 'otpType' => 'required_without:uri|in:totp,hotp,TOTP,HOTP', + 'otpType' => 'required_without:uri|in:totp,hotp', 'secret' => 'required_without:uri|string', 'digits' => 'nullable|integer|between:6,10', 'algorithm' => 'nullable|in:sha1,sha256,sha512,md5', @@ -176,10 +176,19 @@ class TwoFAccountController extends Controller { $this->validate($request, [ - 'service' => 'required', + 'service' => 'required|string', + 'account' => 'required_without:uri|nullable|string|regex:/^[^:]+$/i', + 'icon' => 'nullable|string', + 'uri' => 'nullable|string|regex:/^otpauth:\/\/[h,t]otp\//i', + 'otpType' => 'required_without:uri|in:totp,hotp', + 'secret' => 'required_without:uri|string', + 'digits' => 'nullable|integer|between:6,10', + '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 // delete orphan submited icon try { @@ -194,33 +203,9 @@ class TwoFAccountController extends Controller throw $e; } - - if( $twofaccount->otpType === 'hotp' ) { - // HOTP can be desynchronized from the verification - // server so we let the user the possibility to force - // the counter. - - $this->validate($request, [ - 'counter' => 'required|integer', - ]); - - // we set an OTP object to get the its current counter - // and we update it if a new one has been submited - $otp = OTP::get($twofaccount->uri); - - if( $otp->getCounter() !== $request->counter ) { - $otp->setParameter( 'counter', $request->counter ); - $twofaccount->uri = $otp->getProvisioningUri(); - } - } - - $twofaccount->update([ - 'service' => $request->service, - 'account' => $request->account, - 'icon' => $request->icon, - 'uri' => $twofaccount->uri, - ]); + $twofaccount->populate($request->all()); + $twofaccount->save(); return response()->json($twofaccount, 200); diff --git a/resources/js/views/twofaccounts/Edit.vue b/resources/js/views/twofaccounts/Edit.vue index 3e580155..2b448a1b 100644 --- a/resources/js/views/twofaccounts/Edit.vue +++ b/resources/js/views/twofaccounts/Edit.vue @@ -1,33 +1,11 @@