From d98a8effb10ce8fe04a03eae42baa8a9cb0ca3f7 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 13 Dec 2022 20:59:28 -0800 Subject: [PATCH] UI for restricting permissions on /-/create-token, refs #1947 Also fixes test failures I introduced in #1951 --- datasette/app.py | 2 +- datasette/templates/create_token.html | 75 ++++++++++++---- datasette/views/special.py | 118 ++++++++++++++++++++------ tests/test_auth.py | 2 +- 4 files changed, 150 insertions(+), 47 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index fd28a016..f3cb8876 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -475,7 +475,7 @@ class Datasette: restrict_database: Optional[Dict[str, Iterable[str]]] = None, restrict_resource: Optional[Dict[str, Dict[str, Iterable[str]]]] = None, ): - token = {"a": actor_id, "token": "dstok", "t": int(time.time())} + token = {"a": actor_id, "t": int(time.time())} if expires_after: token["d"] = expires_after diff --git a/datasette/templates/create_token.html b/datasette/templates/create_token.html index a94881ed..a39d6ecb 100644 --- a/datasette/templates/create_token.html +++ b/datasette/templates/create_token.html @@ -2,11 +2,36 @@ {% block title %}Create an API token{% endblock %} +{% block extra_head %} + +{% endblock %} + {% block content %}

Create an API token

-

This token will allow API access with the same abilities as your current user.

+

This token will allow API access with the same abilities as your current user, {{ request.actor.id }}

+ +{% if token %} +
+

Your API token

+
+ + +
+ +
+ Token details +
{{ token_bits|tojson(4) }}
+
+
+

Create another token

+{% endif %} {% if errors %} {% for error in errors %} @@ -27,23 +52,39 @@ - - -{% if token %} -
-

Your API token

-
- - -
- -
- Token details -
{{ token_bits|tojson }}
-
-
- {% endif %} +
+ Restrict actions that can be performed using this token +

All databases and tables

+ + + {% for database in database_with_tables %} +

All tables in "{{ database.name }}"

+ + {% endfor %} +

Specific tables

+ {% for database in database_with_tables %} + {% for table in database.tables %} +

{{ database.name }}: {{ table.name }}

+ + {% endfor %} + {% endfor %} +
+ + +