WebAuthnLoginController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use DarkGhostHunter\Larapass\Http\AuthenticatesWebAuthn;
  7. class WebAuthnLoginController extends Controller
  8. {
  9. // use AuthenticatesWebAuthn;
  10. use AuthenticatesWebAuthn {
  11. options as traitOptions;
  12. login as traitLogin;
  13. }
  14. /*
  15. |--------------------------------------------------------------------------
  16. | WebAuthn Login Controller
  17. |--------------------------------------------------------------------------
  18. |
  19. | This controller allows the WebAuthn user device to request a login and
  20. | return the correctly signed challenge. Most of the hard work is done
  21. | by your Authentication Guard once the user is attempting to login.
  22. |
  23. */
  24. public function __construct()
  25. {
  26. // $this->middleware(['guest', 'throttle:10,1']);
  27. }
  28. public function options(Request $request)
  29. {
  30. // Since 2FAuth is single user designed we fetch the user instance
  31. // and merge its email address to the request. This let Larapass validated
  32. // the request against a user instance without the need to ask the visitor
  33. // for an email address.
  34. //
  35. // This approach override the Larapass 'userless' config value that seems buggy.
  36. $user = User::first();
  37. if (!$user) {
  38. return response()->json([
  39. 'message' => 'no registered user'
  40. ], 400);
  41. }
  42. else $request->merge(['email' => $user->email]);
  43. return $this->traitOptions($request);
  44. }
  45. /**
  46. * Log the user in.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. *
  50. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  51. */
  52. public function login(Request $request)
  53. {
  54. if ($request->has('response')) {
  55. $response = $request->response;
  56. // Some authenticators do not send a userHandle so we hack the response to be compliant
  57. // with Larapass/webauthn-lib implementation that wait for a userHandle
  58. if(!$response['userHandle']) {
  59. $user = User::getFromCredentialId($request->id);
  60. $response['userHandle'] = base64_encode($user->userHandle());
  61. $request->merge(['response' => $response]);
  62. }
  63. }
  64. return $this->traitLogin($request);
  65. }
  66. }