Skip to content

Commit

Permalink
New request.cookies property
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 2, 2020
1 parent b4cd879 commit 1d0bea1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
9 changes: 2 additions & 7 deletions datasette/actor_auth_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

@hookimpl
def actor_from_request(datasette, request):
cookies = SimpleCookie()
cookies.load(
dict(request.scope.get("headers") or []).get(b"cookie", b"").decode("utf-8")
)
if "ds_actor" not in cookies:
if "ds_actor" not in request.cookies:
return None
ds_actor = cookies["ds_actor"].value
try:
return datasette.unsign(ds_actor, "actor")
return datasette.unsign(request.cookies["ds_actor"], "actor")
except BadSignature:
return None
7 changes: 7 additions & 0 deletions datasette/utils/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import parse_qs, urlunparse, parse_qsl
from pathlib import Path
from html import escape
from http.cookies import SimpleCookie
import re
import aiofiles

Expand Down Expand Up @@ -44,6 +45,12 @@ def headers(self):
def host(self):
return self.headers.get("host") or "localhost"

@property
def cookies(self):
cookies = SimpleCookie()
cookies.load(self.headers.get("cookie", ""))
return {key: value.value for key, value in cookies.items()}

@property
def path(self):
if self.scope.get("raw_path") is not None:
Expand Down
3 changes: 3 additions & 0 deletions docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The request object is passed to various plugin hooks. It represents an incoming
``.headers`` - dictionary (str -> str)
A dictionary of incoming HTTP request headers.

``.cookies`` - dictionary (str -> str)
A dictionary of incoming cookies

``.host`` - string
The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``.

Expand Down

0 comments on commit 1d0bea1

Please sign in to comment.