WebAuthnManageController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests\WebauthnRenameRequest;
  6. use DarkGhostHunter\Larapass\Eloquent\WebAuthnCredential;
  7. use App\Exceptions\UnsupportedWithReverseProxyException;
  8. class WebAuthnManageController extends Controller
  9. {
  10. // use RecoversWebAuthn;
  11. /*
  12. |--------------------------------------------------------------------------
  13. | WebAuthn Manage Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. |
  17. */
  18. /**
  19. * Create a new controller instance.
  20. */
  21. public function __construct()
  22. {
  23. }
  24. /**
  25. * List all WebAuthn registered credentials
  26. */
  27. public function index(Request $request)
  28. {
  29. // WebAuthn is useless when authentication is handle by
  30. // a reverse proxy so we return a 202 response to tell the
  31. // client nothing more will happen
  32. if (config('auth.defaults.guard') === 'reverse-proxy-guard') {
  33. return response()->json([
  34. 'message' => 'no webauthn with reverse proxy'], 202);
  35. }
  36. $user = $request->user();
  37. $allUserCredentials = $user->webAuthnCredentials()
  38. ->enabled()
  39. ->get()
  40. ->all();
  41. return response()->json($allUserCredentials, 200);
  42. }
  43. /**
  44. * Rename a WebAuthn device
  45. *
  46. * @param \App\Http\Requests\WebauthnRenameRequest $request
  47. * @return \Illuminate\Http\JsonResponse
  48. */
  49. public function rename(WebauthnRenameRequest $request, string $credential)
  50. {
  51. $validated = $request->validated();
  52. $webAuthnCredential = WebAuthnCredential::where('id', $credential)->firstOrFail();
  53. $webAuthnCredential->name = $validated['name'];
  54. $webAuthnCredential->save();
  55. return response()->json([
  56. 'name' => $webAuthnCredential->name,
  57. ], 200);
  58. }
  59. /**
  60. * Remove the specified credential from storage.
  61. *
  62. * @return \Illuminate\Http\JsonResponse
  63. */
  64. public function delete(Request $request, $credential)
  65. {
  66. $user = $request->user();
  67. $user->removeCredential($credential);
  68. return response()->json(null, 204);
  69. }
  70. }