WebAuthnManageController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\WebauthnRenameRequest;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Log;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. class WebAuthnManageController extends Controller
  10. {
  11. /**
  12. * List all WebAuthn registered credentials
  13. *
  14. * @return \Illuminate\Http\JsonResponse
  15. */
  16. public function index(Request $request)
  17. {
  18. if (Gate::denies('manage-webauthn-credentials')) {
  19. throw new AccessDeniedHttpException(__('errors.unsupported_with_sso_only'));
  20. }
  21. $allUserCredentials = $request->user()->webAuthnCredentials()->WhereEnabled()->get();
  22. return response()->json($allUserCredentials, 200);
  23. }
  24. /**
  25. * Rename a WebAuthn credential
  26. *
  27. * @return \Illuminate\Http\JsonResponse
  28. */
  29. public function rename(WebauthnRenameRequest $request, string $credential)
  30. {
  31. $validated = $request->validated();
  32. abort_if(! $request->user()->renameCredential($credential, $validated['name']), 404);
  33. return response()->json([
  34. 'name' => $validated['name'],
  35. ], 200);
  36. }
  37. /**
  38. * Remove the specified credential from storage.
  39. *
  40. * @param string|array $credential
  41. * @return \Illuminate\Http\JsonResponse
  42. */
  43. public function delete(Request $request, $credential)
  44. {
  45. Log::info('Deletion of security device requested');
  46. if (Gate::denies('manage-webauthn-credentials')) {
  47. throw new AccessDeniedHttpException(__('errors.unsupported_with_sso_only'));
  48. }
  49. $user = $request->user();
  50. $user->flushCredential($credential);
  51. // Webauthn user options need to be reset to prevent impossible login when
  52. // no more registered device exists.
  53. // See #110
  54. if (blank($user->webAuthnCredentials()->WhereEnabled()->get())) {
  55. $request->user()->preferences['useWebauthnOnly'] = false;
  56. $request->user()->save();
  57. Log::notice(sprintf('No more Webauthn credential for user ID #%s, useWebauthnOnly user preference reset to false', $user->id));
  58. }
  59. Log::info(sprintf('User ID #%s revoked a security device', $user->id));
  60. return response()->json(null, 204);
  61. }
  62. }