WebAuthnLoginController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class WebAuthnLoginController extends Controller
  9. {
  10. // use AuthenticatesWebAuthn;
  11. use AuthenticatesWebAuthn {
  12. options as traitOptions;
  13. login as traitLogin;
  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. * @return \Illuminate\Http\JsonResponse|\Webauthn\PublicKeyCredentialRequestOptions
  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 validate
  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. $request->validate($this->assertionRules());
  55. if ($request->has('response')) {
  56. $response = $request->response;
  57. // Some authenticators do not send a userHandle so we hack the response to be compliant
  58. // with Larapass/webauthn-lib implementation that wait for a userHandle
  59. if(!$response['userHandle']) {
  60. $user = User::getFromCredentialId($request->id);
  61. $response['userHandle'] = base64_encode($user->userHandle());
  62. $request->merge(['response' => $response]);
  63. }
  64. }
  65. return $this->traitLogin($request);
  66. }
  67. /**
  68. * The user has been authenticated.
  69. *
  70. * @param \Illuminate\Http\Request $request
  71. * @param mixed $user
  72. *
  73. * @return void|\Illuminate\Http\JsonResponse
  74. */
  75. protected function authenticated(Request $request, $user)
  76. {
  77. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  78. $user->save();
  79. }
  80. }