diff --git a/datasette/views/special.py b/datasette/views/special.py index c80e816f..e997b788 100644 --- a/datasette/views/special.py +++ b/datasette/views/special.py @@ -75,6 +75,9 @@ class AuthTokenView(BaseView): has_json_alternate = False async def get(self, request): + # If already signed in as root, redirect + if request.actor and request.actor.get("id") == "root": + return Response.redirect(self.ds.urls.instance()) token = request.args.get("token") or "" if not self.ds._root_token: raise Forbidden("Root token has already been used") diff --git a/tests/test_auth.py b/tests/test_auth.py index 74e8283a..fd7fcc65 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -26,6 +26,12 @@ async def test_auth_token(ds_client): # Check that a second with same token fails assert ds_client.ds._root_token is None assert (await ds_client.get(path)).status_code == 403 + # But attempting with same token while logged in as root should redirect to / + response = await ds_client.get( + path, cookies={"ds_actor": ds_client.actor_cookie({"id": "root"})} + ) + assert response.status_code == 302 + assert response.headers["Location"] == "/" @pytest.mark.asyncio