Refactoring

This commit is contained in:
Bubka 2020-01-25 18:22:45 +01:00
parent ffaef86909
commit 2233f1119b
2 changed files with 25 additions and 33 deletions

View File

@ -15,15 +15,10 @@ class OTP
* @param \App\TwoFAccount $twofaccount
* @return an array that represent the totp code
*/
public static function get($uri)
public static function generate($uri)
{
try {
$otp = Factory::loadFromProvisioningUri($uri);
}
catch (AssertionFailedException $exception) {
return false;
}
$otp = $this->get($uri);
if( get_class($otp) === 'OTPHP\TOTP' ) {
@ -58,7 +53,27 @@ public static function get($uri)
return $hotp;
}
}
/**
* check if the provided uri is a valid OTP uri
*
* @param \App\TwoFAccount $twofaccount
* @return \Illuminate\Http\Response
*/
public static function get(String $uri) {
try {
return Factory::loadFromProvisioningUri($uri);
}
catch (AssertionFailedException $exception) {
$error = \Illuminate\Validation\ValidationException::withMessages([
'qrcode' => __('errors.response.no_valid_totp')
]);
throw $error;
}
}

View File

@ -37,7 +37,7 @@ public function store(Request $request)
'uri' => 'required|regex:/^otpauth:\/\/[h,t]otp\//i',
]);
$this->validateOTP($request->uri);
OTP::get($request->uri);
$twofaccount = TwoFAccount::create([
'service' => $request->service,
@ -76,7 +76,7 @@ public function show($id)
public function generateOTP(TwoFAccount $twofaccount)
{
return response()->json(OTP::get($twofaccount->uri), 200);
return response()->json(OTP::generate($twofaccount->uri), 200);
}
@ -142,27 +142,4 @@ public function destroy($id)
}
}
/**
* check if the provided uri is a valid OTP uri
*
* @param \App\TwoFAccount $twofaccount
* @return \Illuminate\Http\Response
*/
private function validateOTP(String $uri) {
try {
$otp = \OTPHP\Factory::loadFromProvisioningUri($uri);
return true;
}
catch (\Assert\AssertionFailedException $exception) {
$error = \Illuminate\Validation\ValidationException::withMessages([
'qrcode' => __('errors.response.no_valid_totp')
]);
throw $error;
}
}
}