WebAuthnLoginController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. use Illuminate\Support\Arr;
  13. class WebAuthnLoginController extends Controller
  14. {
  15. /*
  16. |--------------------------------------------------------------------------
  17. | WebAuthn Login Controller
  18. |--------------------------------------------------------------------------
  19. |
  20. | This controller allows the WebAuthn user device to request a login and
  21. | return the correctly signed challenge. Most of the hard work is done
  22. | by your Authentication Guard once the user is attempting to login.
  23. |
  24. */
  25. /**
  26. * Returns the challenge to assertion.
  27. *
  28. * @param \Laragear\WebAuthn\Http\Requests\AssertionRequest $request
  29. * @return \Illuminate\Contracts\Support\Responsable|\Illuminate\Http\JsonResponse
  30. */
  31. public function options(AssertionRequest $request) : Responsable|JsonResponse
  32. {
  33. switch (config('webauthn.user_verification')) {
  34. case WebAuthn::USER_VERIFICATION_DISCOURAGED:
  35. $request = $request->fastLogin(); // Makes the authenticator to only check for user presence on registration
  36. break;
  37. case WebAuthn::USER_VERIFICATION_REQUIRED:
  38. $request = $request->secureLogin(); // Makes the authenticator to always verify the user thoroughly on registration
  39. break;
  40. }
  41. // Since 2FAuth is single user designed we fetch the user instance.
  42. // This lets Larapass validate the request without the need to ask
  43. // the visitor for an email address.
  44. $user = User::first();
  45. return $user
  46. ? $request->toVerify($user)
  47. : response()->json([
  48. 'message' => 'no registered user',
  49. ], 400);
  50. }
  51. /**
  52. * Log the user in.
  53. *
  54. * @param \Laragear\WebAuthn\Http\Requests\AssertedRequest $request
  55. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  56. */
  57. public function login(AssertedRequest $request)
  58. {
  59. Log::info('User login via webauthn requested');
  60. if ($request->has('response')) {
  61. $response = $request->response;
  62. // Some authenticators do not send a userHandle so we hack the response to be compliant
  63. // with Larapass/webauthn-lib implementation that waits for a userHandle
  64. if (!Arr::exists($response, 'userHandle') || blank($response['userHandle'])) {
  65. $response['userHandle'] = User::getFromCredentialId($request->id)?->userHandle();
  66. $request->merge(['response' => $response]);
  67. }
  68. }
  69. $user = $request->login();
  70. if ($user) {
  71. $this->authenticated($user);
  72. return response()->noContent();
  73. }
  74. return response()->noContent(422);
  75. }
  76. /**
  77. * The user has been authenticated.
  78. *
  79. * @param mixed $user
  80. * @return void|\Illuminate\Http\JsonResponse
  81. */
  82. protected function authenticated($user)
  83. {
  84. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  85. $user->save();
  86. Log::info('User authenticated via webauthn');
  87. }
  88. }