Browse Source

Get OTP data from OTPHP parser

Bubka 5 năm trước cách đây
mục cha
commit
ad09f233db
1 tập tin đã thay đổi với 26 bổ sung40 xóa
  1. 26 40
      app/Http/Controllers/QrCodeController.php

+ 26 - 40
app/Http/Controllers/QrCodeController.php

@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
 
 use Validator;
 use Zxing\QrReader;
-use App\Classes\TimedTOTP;
+use OTPHP\TOTP;
+use OTPHP\Factory;
+use Assert\AssertionFailedException;
 use Illuminate\Http\File;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Storage;
@@ -37,59 +39,43 @@ class QrCodecontroller extends Controller
         // qrcode analysis
         $path = $request->file('qrcode')->store('qrcodes');
         $qrcode = new QrReader(storage_path('app/' . $path));
+
         $uri = urldecode($qrcode->text());
 
         // delete uploaded file
         Storage::delete($path);
 
-        // Check uri validity
-        if( !TimedTOTP::get($uri) ) {
-
-            return response()->json([
-                'error' => [
-                   'qrcode' => 'No valid TOTP resource in this QR code'
-                ]
-            ], 400);
-
-        }
+        // return the OTP object
+        try {
 
-        $uriChunks = explode('?', $uri);
+            $otp = Factory::loadFromProvisioningUri($uri);
 
-        foreach(explode('&', $uriChunks[1]) as $option) {
-            $option = explode('=', $option);
-            $options[$option[0]] = $option[1];
-        }
+            if(!$otp->getIssuer()) {
+                $otp->setIssuer($otp->getLabel());
+                $otp->setLabel('');
+            }
 
-        $account = $service = '';
+            // returned object
+            $twofaccount = (object) array(
+                'service' => $otp->getIssuer(),
+                'account' => $otp->getLabel(),
+                'uri' => $uri,
+                'icon' => '',
+                'options' => $otp->getParameters()
+            );
 
-        $serviceChunks = explode(':', str_replace('otpauth://totp/', '', $uriChunks[0]));
+            return response()->json($twofaccount, 200);
 
-        if( count($serviceChunks) > 1 ) {
-            $account = $serviceChunks[1];
         }
+        catch (AssertionFailedException $exception) {
 
-        $service = $serviceChunks[0];
-
-        if( strstr( $service, '@') ) {
-            $account = $service;
-            $service = '';
-        }
+            return response()->json([
+                'error' => [
+                   'qrcode' => 'No valid TOTP resource in this QR code'
+                ]
+            ], 400);
 
-        if( empty($service) & !empty($options['issuer']) ) {
-            $service = $options['issuer'];
         }
-
-
-        // returned object
-        $twofaccount = (object) array(
-            'service' => $service,
-            'account' => $account,
-            'uri' => $uri,
-            'icon' => '',
-            'options' => $options
-        );
-
-        return response()->json($twofaccount, 201);
     }
     
 }