QrCodeController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\Models\TwoFAccount;
  4. use App\Services\QrCodeService;
  5. use App\Services\TwoFAccountService;
  6. use App\Api\v1\Requests\QrCodeDecodeRequest;
  7. use App\Http\Controllers\Controller;
  8. class QrCodeController extends Controller
  9. {
  10. /**
  11. * The QR code Service instance.
  12. */
  13. protected $qrcodeService;
  14. /**
  15. * The TwoFAccount Service instance.
  16. */
  17. protected $twofaccountService;
  18. /**
  19. * Create a new controller instance.
  20. *
  21. * @param \App\Services\QrCodeService $qrcodeService
  22. * @param \App\Services\TwoFAccountService $twofaccountService
  23. * @return void
  24. */
  25. public function __construct(QrCodeService $qrcodeService, TwoFAccountService $twofaccountService)
  26. {
  27. $this->qrcodeService = $qrcodeService;
  28. $this->twofaccountService = $twofaccountService;
  29. }
  30. /**
  31. * Show a QR code image
  32. *
  33. * @param App\Models\TwoFAccount $twofaccount
  34. * @return \Illuminate\Http\JsonResponse
  35. */
  36. public function show(TwoFAccount $twofaccount)
  37. {
  38. $uri = $this->twofaccountService->getURI($twofaccount);
  39. return response()->json(['qrcode' => $this->qrcodeService->encode($uri)], 200);
  40. }
  41. /**
  42. * Decode an uploaded QR Code image
  43. *
  44. * @param \App\Api\v1\Requests\QrCodeDecodeRequest $request
  45. * @return \Illuminate\Http\JsonResponse
  46. */
  47. public function decode(QrCodeDecodeRequest $request)
  48. {
  49. $file = $request->file('qrcode');
  50. return response()->json(['data' => $this->qrcodeService->decode($file)], 200);
  51. }
  52. }