Skip to content

Commit

Permalink
Drop API token requirement from API explorer, refs #1871
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 30, 2022
1 parent f6bf2d8 commit 9eb9ffa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
9 changes: 9 additions & 0 deletions datasette/default_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,12 @@ def create_token(id, secret, expires_after, debug):
if debug:
click.echo("\nDecoded:\n")
click.echo(json.dumps(ds.unsign(token, namespace="token"), indent=2))


@hookimpl
def skip_csrf(scope):
# Skip CSRF check for requests with content-type: application/json
if scope["type"] == "http":
headers = scope.get("headers") or {}
if dict(headers).get(b"content-type") == b"application/json":
return True
13 changes: 4 additions & 9 deletions datasette/templates/api_explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ <h1>API Explorer</h1>
{% endif %}

<form method="post" id="api-explorer">
<div>
<label for="auth-token">API token:</label>
<input type="text" id="auth-token" name="token" value="" style="width: 40%">
</div>
<div>
<label for="path">API path:</label>
<input type="text" id="path" name="path" value="/fixtures/searchable/-/insert" style="width: 40%">
<input type="text" id="path" name="path" value="/fixtures/searchable/-/insert" style="width: 60%">
</div>
<div>
<textarea name="json" style="width: 60%; height: 200px; font-family: monospace; font-size: 0.8em;"></textarea>
<div style="margin: 0.5em 0">
<label for="apiJson" style="vertical-align: top">JSON:</label>
<textarea id="apiJson" name="json" style="width: 60%; height: 200px; font-family: monospace; font-size: 0.8em;"></textarea>
</div>
<p><button id="json-format" type="button">Format JSON</button> <input type="submit" value="POST"></p>
</form>
Expand All @@ -46,7 +43,6 @@ <h1>API Explorer</h1>
var formData = new FormData(form);
var json = formData.get('json');
var path = formData.get('path');
var token = formData.get('token');
// Validate JSON
try {
var data = JSON.parse(json);
Expand All @@ -60,7 +56,6 @@ <h1>API Explorer</h1>
body: json,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
}).then(r => r.json()).then(r => {
alert(JSON.stringify(r, null, 2));
Expand Down

0 comments on commit 9eb9ffa

Please sign in to comment.