WebAuthnLoginController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use Carbon\Carbon;
  6. use Illuminate\Contracts\Support\Responsable;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\Log;
  9. use Laragear\WebAuthn\Http\Requests\AssertedRequest;
  10. use Laragear\WebAuthn\Http\Requests\AssertionRequest;
  11. use Laragear\WebAuthn\WebAuthn;
  12. class WebAuthnLoginController extends Controller
  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. /**
  25. * Returns the challenge to assertion.
  26. *
  27. * @param \Laragear\WebAuthn\Http\Requests\AssertionRequest $request
  28. * @return \Illuminate\Contracts\Support\Responsable|\Illuminate\Http\JsonResponse
  29. */
  30. public function options(AssertionRequest $request) : Responsable|JsonResponse
  31. {
  32. switch (env('WEBAUTHN_USER_VERIFICATION')) {
  33. case WebAuthn::USER_VERIFICATION_DISCOURAGED:
  34. $request = $request->fastLogin(); // Makes the authenticator to only check for user presence on registration
  35. break;
  36. case WebAuthn::USER_VERIFICATION_REQUIRED:
  37. $request = $request->secureLogin(); // Makes the authenticator to always verify the user thoroughly on registration
  38. break;
  39. }
  40. // Since 2FAuth is single user designed we fetch the user instance.
  41. // This lets Larapass validate the request without the need to ask
  42. // the visitor for an email address.
  43. $user = User::first();
  44. return $user
  45. ? $request->toVerify($user)
  46. : response()->json([
  47. 'message' => 'no registered user',
  48. ], 400);
  49. }
  50. /**
  51. * Log the user in.
  52. *
  53. * @param \Laragear\WebAuthn\Http\Requests\AssertedRequest $request
  54. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  55. */
  56. public function login(AssertedRequest $request)
  57. {
  58. Log::info('User login via webauthn requested');
  59. if ($request->has('response')) {
  60. $response = $request->response;
  61. // Some authenticators do not send a userHandle so we hack the response to be compliant
  62. // with Larapass/webauthn-lib implementation that waits for a userHandle
  63. if (! $response['userHandle']) {
  64. $response['userHandle'] = User::getFromCredentialId($request->id)?->userHandle();
  65. $request->merge(['response' => $response]);
  66. }
  67. }
  68. $user = $request->login();
  69. if ($user) {
  70. $this->authenticated($user);
  71. return response()->noContent();
  72. }
  73. return response()->noContent(422);
  74. }
  75. /**
  76. * The user has been authenticated.
  77. *
  78. * @param mixed $user
  79. * @return void|\Illuminate\Http\JsonResponse
  80. */
  81. protected function authenticated($user)
  82. {
  83. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  84. $user->save();
  85. Log::info('User authenticated via webauthn');
  86. }
  87. }