WebAuthnLoginController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Log;
  9. class WebAuthnLoginController extends Controller
  10. {
  11. // use AuthenticatesWebAuthn;
  12. use AuthenticatesWebAuthn {
  13. options as traitOptions;
  14. login as traitLogin;
  15. }
  16. /*
  17. |--------------------------------------------------------------------------
  18. | WebAuthn Login Controller
  19. |--------------------------------------------------------------------------
  20. |
  21. | This controller allows the WebAuthn user device to request a login and
  22. | return the correctly signed challenge. Most of the hard work is done
  23. | by your Authentication Guard once the user is attempting to login.
  24. |
  25. */
  26. /**
  27. * @return \Illuminate\Http\JsonResponse|\Webauthn\PublicKeyCredentialRequestOptions
  28. */
  29. public function options(Request $request)
  30. {
  31. // Since 2FAuth is single user designed we fetch the user instance
  32. // and merge its email address to the request. This let Larapass validate
  33. // the request against a user instance without the need to ask the visitor
  34. // for an email address.
  35. //
  36. // This approach override the Larapass 'userless' config value that seems buggy.
  37. $user = User::first();
  38. if (!$user) {
  39. return response()->json([
  40. 'message' => 'no registered user'
  41. ], 400);
  42. }
  43. else $request->merge(['email' => $user->email]);
  44. return $this->traitOptions($request);
  45. }
  46. /**
  47. * Log the user in.
  48. *
  49. * @param \Illuminate\Http\Request $request
  50. *
  51. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  52. */
  53. public function login(Request $request)
  54. {
  55. Log::info('User login via webauthn requested');
  56. $request->validate($this->assertionRules());
  57. if ($request->has('response')) {
  58. $response = $request->response;
  59. // Some authenticators do not send a userHandle so we hack the response to be compliant
  60. // with Larapass/webauthn-lib implementation that wait for a userHandle
  61. if(!$response['userHandle']) {
  62. $user = User::getFromCredentialId($request->id);
  63. $response['userHandle'] = base64_encode($user->userHandle());
  64. $request->merge(['response' => $response]);
  65. }
  66. }
  67. return $this->traitLogin($request);
  68. }
  69. /**
  70. * The user has been authenticated.
  71. *
  72. * @param \Illuminate\Http\Request $request
  73. * @param mixed $user
  74. *
  75. * @return void|\Illuminate\Http\JsonResponse
  76. */
  77. protected function authenticated(Request $request, $user)
  78. {
  79. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  80. $user->save();
  81. Log::info('User authenticated via webauthn');
  82. }
  83. }