interlude-sdk 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/PKG-INFO +2 -2
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/README.md +1 -1
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/pyproject.toml +1 -1
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/__init__.py +1 -1
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/client.py +18 -7
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude_sdk.egg-info/PKG-INFO +2 -2
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_end_user.py +20 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/setup.cfg +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/_http.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/_parse.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/account.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/errors.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/models.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/storage.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude/table.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude_sdk.egg-info/SOURCES.txt +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude_sdk.egg-info/dependency_links.txt +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude_sdk.egg-info/requires.txt +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/src/interlude_sdk.egg-info/top_level.txt +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_account_session.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_client_auth.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_integration.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_storage.py +0 -0
- {interlude_sdk-0.2.0 → interlude_sdk-0.3.0}/tests/test_table.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: interlude-sdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Official Python SDK for Interlude: tables, storage, end users and projects
|
|
5
5
|
License: MIT
|
|
6
6
|
Project-URL: Homepage, https://useinterlude.com
|
|
@@ -82,7 +82,7 @@ client = Client(
|
|
|
82
82
|
publishable_key="il_pk_v1_...",
|
|
83
83
|
)
|
|
84
84
|
|
|
85
|
-
user = client.auth.get_user(
|
|
85
|
+
user = client.auth.get_user(request.headers.get("authorization")) # who sent this request? 401 if the token is bad
|
|
86
86
|
|
|
87
87
|
# Read and write rows *as that user*: the table's access rules apply, and the
|
|
88
88
|
# API key is not sent.
|
|
@@ -62,7 +62,7 @@ client = Client(
|
|
|
62
62
|
publishable_key="il_pk_v1_...",
|
|
63
63
|
)
|
|
64
64
|
|
|
65
|
-
user = client.auth.get_user(
|
|
65
|
+
user = client.auth.get_user(request.headers.get("authorization")) # who sent this request? 401 if the token is bad
|
|
66
66
|
|
|
67
67
|
# Read and write rows *as that user*: the table's access rules apply, and the
|
|
68
68
|
# API key is not sent.
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import re
|
|
5
6
|
from typing import Any, Optional
|
|
6
7
|
|
|
7
8
|
import httpx
|
|
@@ -118,16 +119,16 @@ class Client:
|
|
|
118
119
|
"""Your app's users: check who an access token belongs to."""
|
|
119
120
|
return EndUserAuth(self)
|
|
120
121
|
|
|
121
|
-
def as_user(self, access_token: str) -> "UserScope":
|
|
122
|
+
def as_user(self, access_token: Optional[str]) -> "UserScope":
|
|
122
123
|
"""Acts as one of your app's users, with the access token they sent
|
|
123
|
-
your server.
|
|
124
|
+
your server (or the request's whole ``Authorization`` header).
|
|
124
125
|
|
|
125
126
|
Requests carry their token and the publishable key, never the API
|
|
126
127
|
key, so the table's access rules apply exactly as they would to that
|
|
127
128
|
user's own app. Use this rather than the API key whenever a request is
|
|
128
129
|
made on a user's behalf.
|
|
129
130
|
"""
|
|
130
|
-
if not access_token:
|
|
131
|
+
if not _bare_token(access_token):
|
|
131
132
|
raise InterludeError("access_token_required", "an end-user access token is required", 0)
|
|
132
133
|
return UserScope(self, access_token)
|
|
133
134
|
|
|
@@ -186,23 +187,33 @@ class Client:
|
|
|
186
187
|
return self._publishable_key
|
|
187
188
|
|
|
188
189
|
|
|
190
|
+
_BEARER = re.compile(r"^\s*bearer(\s+|$)", re.IGNORECASE)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _bare_token(credential: Optional[str]) -> str:
|
|
194
|
+
"""A token from either the token itself or an ``Authorization`` header."""
|
|
195
|
+
return _BEARER.sub("", credential or "").strip()
|
|
196
|
+
|
|
197
|
+
|
|
189
198
|
class EndUserAuth:
|
|
190
199
|
"""Your app's users, as seen from your server."""
|
|
191
200
|
|
|
192
201
|
def __init__(self, client: Client) -> None:
|
|
193
202
|
self._client = client
|
|
194
203
|
|
|
195
|
-
def get_user(self, access_token: str) -> EndUser:
|
|
204
|
+
def get_user(self, access_token: Optional[str]) -> EndUser:
|
|
196
205
|
"""The user an access token belongs to.
|
|
197
206
|
|
|
198
207
|
This is how your server learns who sent a request: the app sends its
|
|
199
208
|
user's token, and this asks Interlude whether the token is valid and
|
|
200
|
-
whose it is.
|
|
209
|
+
whose it is. Pass the token, or the request's whole ``Authorization``
|
|
210
|
+
header; ``Bearer`` is stripped. An invalid or expired token raises
|
|
201
211
|
:class:`InterludeError` with ``status_code`` 401.
|
|
202
212
|
"""
|
|
203
213
|
client = self._client
|
|
204
214
|
project_id = client._require_project_id("auth")
|
|
205
215
|
publishable_key = client._require_publishable_key("auth")
|
|
216
|
+
access_token = _bare_token(access_token)
|
|
206
217
|
if not access_token:
|
|
207
218
|
raise InterludeError("access_token_required", "an end-user access token is required", 0)
|
|
208
219
|
data, _ = client._transport.send(
|
|
@@ -217,9 +228,9 @@ class EndUserAuth:
|
|
|
217
228
|
class UserScope:
|
|
218
229
|
"""The data plane as one of your app's users. From :meth:`Client.as_user`."""
|
|
219
230
|
|
|
220
|
-
def __init__(self, client: Client, access_token: str) -> None:
|
|
231
|
+
def __init__(self, client: Client, access_token: Optional[str]) -> None:
|
|
221
232
|
self._client = client
|
|
222
|
-
self._access_token = access_token
|
|
233
|
+
self._access_token = _bare_token(access_token)
|
|
223
234
|
|
|
224
235
|
def table(self, name: str) -> TableClient:
|
|
225
236
|
"""A table's rows, as this user, within the table's access rules."""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: interlude-sdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Official Python SDK for Interlude: tables, storage, end users and projects
|
|
5
5
|
License: MIT
|
|
6
6
|
Project-URL: Homepage, https://useinterlude.com
|
|
@@ -82,7 +82,7 @@ client = Client(
|
|
|
82
82
|
publishable_key="il_pk_v1_...",
|
|
83
83
|
)
|
|
84
84
|
|
|
85
|
-
user = client.auth.get_user(
|
|
85
|
+
user = client.auth.get_user(request.headers.get("authorization")) # who sent this request? 401 if the token is bad
|
|
86
86
|
|
|
87
87
|
# Read and write rows *as that user*: the table's access rules apply, and the
|
|
88
88
|
# API key is not sent.
|
|
@@ -70,3 +70,23 @@ def test_keys_in_the_wrong_place_are_refused(options, mistake):
|
|
|
70
70
|
with pytest.raises(ValueError) as excinfo:
|
|
71
71
|
build_client(Recorder(ok(None)), **options)
|
|
72
72
|
assert mistake in str(excinfo.value)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@pytest.mark.parametrize("header", ["Bearer user.jwt.token", "bearer user.jwt.token ", "user.jwt.token"])
|
|
76
|
+
def test_get_user_takes_the_authorization_header_as_it_arrives(header):
|
|
77
|
+
rec = Recorder(ok(USER))
|
|
78
|
+
|
|
79
|
+
client(rec).auth.get_user(header)
|
|
80
|
+
|
|
81
|
+
assert rec.last.headers["Authorization"] == "Bearer user.jwt.token"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@pytest.mark.parametrize("missing", [None, "", "Bearer ", " "])
|
|
85
|
+
def test_get_user_without_a_token_is_refused_before_sending(missing):
|
|
86
|
+
rec = Recorder(ok(USER))
|
|
87
|
+
|
|
88
|
+
with pytest.raises(InterludeError) as err:
|
|
89
|
+
client(rec).auth.get_user(missing)
|
|
90
|
+
|
|
91
|
+
assert err.value.code == "access_token_required"
|
|
92
|
+
assert rec.requests == []
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|