WebAuthnLoginController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 options(Request $request)
  25. {
  26. // Since 2FAuth is single user designed we fetch the user instance
  27. // and merge its email address to the request. This let Larapass validate
  28. // the request against a user instance without the need to ask the visitor
  29. // for an email address.
  30. //
  31. // This approach override the Larapass 'userless' config value that seems buggy.
  32. $user = User::first();
  33. if (!$user) {
  34. return response()->json([
  35. 'message' => 'no registered user'
  36. ], 400);
  37. }
  38. else $request->merge(['email' => $user->email]);
  39. return $this->traitOptions($request);
  40. }
  41. /**
  42. * Log the user in.
  43. *
  44. * @param \Illuminate\Http\Request $request
  45. *
  46. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  47. */
  48. public function login(Request $request)
  49. {
  50. $request->validate($this->assertionRules());
  51. if ($request->has('response')) {
  52. $response = $request->response;
  53. // Some authenticators do not send a userHandle so we hack the response to be compliant
  54. // with Larapass/webauthn-lib implementation that wait for a userHandle
  55. if(!$response['userHandle']) {
  56. $user = User::getFromCredentialId($request->id);
  57. $response['userHandle'] = base64_encode($user->userHandle());
  58. $request->merge(['response' => $response]);
  59. }
  60. }
  61. return $this->traitLogin($request);
  62. }
  63. }