seamless-auth 0.1.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.
- seamless_auth-0.1.0/.gitignore +10 -0
- seamless_auth-0.1.0/CHANGELOG.md +14 -0
- seamless_auth-0.1.0/LICENSE +202 -0
- seamless_auth-0.1.0/PKG-INFO +238 -0
- seamless_auth-0.1.0/README.md +202 -0
- seamless_auth-0.1.0/pyproject.toml +98 -0
- seamless_auth-0.1.0/src/seamless_auth/__init__.py +34 -0
- seamless_auth-0.1.0/src/seamless_auth/_adapter.py +911 -0
- seamless_auth-0.1.0/src/seamless_auth/_client_ip.py +85 -0
- seamless_auth-0.1.0/src/seamless_auth/_cookies.py +128 -0
- seamless_auth-0.1.0/src/seamless_auth/_delivery.py +44 -0
- seamless_auth-0.1.0/src/seamless_auth/_http.py +87 -0
- seamless_auth-0.1.0/src/seamless_auth/_jwks.py +132 -0
- seamless_auth-0.1.0/src/seamless_auth/_jwt.py +52 -0
- seamless_auth-0.1.0/src/seamless_auth/_manifest.py +247 -0
- seamless_auth-0.1.0/src/seamless_auth/_refresh.py +81 -0
- seamless_auth-0.1.0/src/seamless_auth/django.py +203 -0
- seamless_auth-0.1.0/src/seamless_auth/fastapi.py +123 -0
- seamless_auth-0.1.0/src/seamless_auth/manifest.json +233 -0
- seamless_auth-0.1.0/src/seamless_auth/py.typed +0 -0
- seamless_auth-0.1.0/tests/conftest.py +230 -0
- seamless_auth-0.1.0/tests/test_adapter.py +852 -0
- seamless_auth-0.1.0/tests/test_client_ip.py +40 -0
- seamless_auth-0.1.0/tests/test_django.py +189 -0
- seamless_auth-0.1.0/tests/test_django_urls.py +31 -0
- seamless_auth-0.1.0/tests/test_fastapi.py +145 -0
- seamless_auth-0.1.0/tests/test_guard.py +225 -0
- seamless_auth-0.1.0/tests/test_manifest.py +89 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-10-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Added
|
|
7
|
+
|
|
8
|
+
* Seamless Auth server adapter for FastAPI and Django ([1f3fab7](https://github.com/fells-code/seamless-auth-python/commit/1f3fab7ff35fa44b5587a6a095c24c4b05888166))
|
|
9
|
+
|
|
10
|
+
## Changelog
|
|
11
|
+
|
|
12
|
+
All notable changes to this project are documented in this file. The release
|
|
13
|
+
automation (release-please) writes each entry from the Conventional Commits it
|
|
14
|
+
releases.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: seamless-auth
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Seamless Auth server adapter for FastAPI and Django: cookie and bearer sessions in front of the Seamless Auth API
|
|
5
|
+
Project-URL: Homepage, https://github.com/fells-code/seamless-auth-python
|
|
6
|
+
Project-URL: Repository, https://github.com/fells-code/seamless-auth-python
|
|
7
|
+
Project-URL: Issues, https://github.com/fells-code/seamless-auth-python/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/fells-code/seamless-auth-python/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Fells Code LLC
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: auth,django,fastapi,passkeys,session,webauthn
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: Django
|
|
15
|
+
Classifier: Framework :: Django :: 5.2
|
|
16
|
+
Classifier: Framework :: FastAPI
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Session
|
|
26
|
+
Classifier: Topic :: Security
|
|
27
|
+
Classifier: Typing :: Typed
|
|
28
|
+
Requires-Python: >=3.11
|
|
29
|
+
Requires-Dist: httpx2<3,>=2.13
|
|
30
|
+
Requires-Dist: pyjwt[crypto]<3,>=2.10
|
|
31
|
+
Provides-Extra: django
|
|
32
|
+
Requires-Dist: django>=5.2; extra == 'django'
|
|
33
|
+
Provides-Extra: fastapi
|
|
34
|
+
Requires-Dist: fastapi>=0.115; extra == 'fastapi'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# seamless-auth-python
|
|
38
|
+
|
|
39
|
+
A [Seamless Auth](https://github.com/fells-code/seamless-auth-api) server adapter for Python, with
|
|
40
|
+
bindings for [FastAPI](https://fastapi.tiangolo.com) and [Django](https://www.djangoproject.com).
|
|
41
|
+
|
|
42
|
+
The adapter sits in your backend between your users and the Seamless Auth API:
|
|
43
|
+
|
|
44
|
+
- **Browsers** talk to it over `HttpOnly` cookies on your own domain. The API's tokens never
|
|
45
|
+
reach page scripts.
|
|
46
|
+
- **Native clients** (mobile, CLIs) talk to it over bearer tokens, sending
|
|
47
|
+
`x-seamless-auth-transport: bearer`.
|
|
48
|
+
- **The auth API** sees bearer tokens plus a service token that lets it trust the client
|
|
49
|
+
address and user agent the adapter forwards.
|
|
50
|
+
|
|
51
|
+
Which routes the adapter serves, and what each does to the session, comes from the adapter
|
|
52
|
+
manifest the auth API publishes, so a new API route works without a new release of this package.
|
|
53
|
+
|
|
54
|
+
It is held to the same [conformance suite](https://github.com/fells-code/seamless-cli/blob/main/verify/CONFORMANCE.md)
|
|
55
|
+
as the Express, Fastify, Go and Rust adapters, with a FastAPI and a Django reference app, in CI on
|
|
56
|
+
every change.
|
|
57
|
+
|
|
58
|
+
## Install
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install "seamless-auth[fastapi]" # or "seamless-auth[django]"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Python 3.11 or later. Django 5.2 or later.
|
|
65
|
+
|
|
66
|
+
## FastAPI
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import os
|
|
70
|
+
|
|
71
|
+
from fastapi import Depends, FastAPI
|
|
72
|
+
from seamless_auth import Adapter, User
|
|
73
|
+
from seamless_auth.fastapi import RequireUser, auth_router
|
|
74
|
+
|
|
75
|
+
auth = Adapter(
|
|
76
|
+
auth_server_url=os.environ["AUTH_SERVER_URL"],
|
|
77
|
+
cookie_secret=os.environ["COOKIE_SECRET"], # at least 32 bytes
|
|
78
|
+
service_secret=os.environ["SERVICE_SECRET"], # the API's API_SERVICE_TOKEN
|
|
79
|
+
jwks_kid=os.environ["JWKS_KID"],
|
|
80
|
+
)
|
|
81
|
+
current_user = RequireUser(auth)
|
|
82
|
+
|
|
83
|
+
app = FastAPI()
|
|
84
|
+
# The auth routes, at /auth, which is where the client SDKs call.
|
|
85
|
+
app.include_router(auth_router(auth))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# Your own routes, behind the adapter's guard.
|
|
89
|
+
@app.get("/api/me")
|
|
90
|
+
def me(user: User = Depends(current_user)):
|
|
91
|
+
return {"id": user.id}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`RequireUser` answers 401 unless the request carries a session, and returns the `User`.
|
|
95
|
+
|
|
96
|
+
## Django
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# settings.py
|
|
100
|
+
SEAMLESS_AUTH = {
|
|
101
|
+
"auth_server_url": os.environ["AUTH_SERVER_URL"],
|
|
102
|
+
"cookie_secret": os.environ["COOKIE_SECRET"],
|
|
103
|
+
"service_secret": os.environ["SERVICE_SECRET"],
|
|
104
|
+
"jwks_kid": os.environ["JWKS_KID"],
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
# urls.py
|
|
108
|
+
urlpatterns = [
|
|
109
|
+
path("auth/", include("seamless_auth.django")),
|
|
110
|
+
path("api/me", views.me),
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
# views.py
|
|
114
|
+
from django.http import JsonResponse
|
|
115
|
+
from seamless_auth.django import require_auth, user_of
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@require_auth
|
|
119
|
+
def me(request):
|
|
120
|
+
return JsonResponse({"id": user_of(request).id})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`SEAMLESS_AUTH` takes the keyword arguments of `Adapter` (below). `deliver` and
|
|
124
|
+
`resolve_client_ip` may be dotted paths to callables. `require_auth` works on sync and async
|
|
125
|
+
views. The auth routes are exempt from Django's CSRF middleware: the adapter does its own
|
|
126
|
+
cross-site checks (below), and the client SDKs send no CSRF token.
|
|
127
|
+
|
|
128
|
+
## The guard
|
|
129
|
+
|
|
130
|
+
The guard (`RequireUser`, `require_auth`) accepts the adapter's session cookie, or an auth API
|
|
131
|
+
access token in `Authorization: Bearer` for clients with no cookie jar. The cookie wins when both
|
|
132
|
+
are present. It does not refresh: the auth routes refresh a browser session silently, and a bearer
|
|
133
|
+
client calls `POST /auth/refresh` itself. `Adapter.authenticate(headers)` does the same check
|
|
134
|
+
anywhere else.
|
|
135
|
+
|
|
136
|
+
A request the guard admits on the session cookie also gets the auth routes' cross-site check:
|
|
137
|
+
while cookies are `SameSite=None`, a state-changing request from another site (`Sec-Fetch-Site:
|
|
138
|
+
cross-site`, or an `Origin` outside `allowed_origins`) answers 403. A bearer token is never
|
|
139
|
+
attached by a browser, so it is not checked.
|
|
140
|
+
|
|
141
|
+
### Request bodies
|
|
142
|
+
|
|
143
|
+
The auth routes forward a request body to the auth API as JSON, so a body must be sent as
|
|
144
|
+
`application/json` (or a `+json` type). Anything else answers 415 `unsupported_media_type`: a
|
|
145
|
+
cross-site form can post a `text/plain` body shaped like JSON with no CORS preflight, and read as
|
|
146
|
+
JSON it would be a sign-in the user never made. The client SDKs already send JSON.
|
|
147
|
+
|
|
148
|
+
## Options
|
|
149
|
+
|
|
150
|
+
| Argument | Default | Purpose |
|
|
151
|
+
| --- | --- | --- |
|
|
152
|
+
| `auth_server_url` | required | Where the adapter reaches the auth API |
|
|
153
|
+
| `auth_server_issuer` | the URL | Expected `iss` of the API's tokens, when it differs from the URL you reach it at |
|
|
154
|
+
| `audience` | the issuer | Expected `aud` of the API's tokens |
|
|
155
|
+
| `cookie_secret` | required | Signs the session cookies (32 bytes or more) |
|
|
156
|
+
| `service_secret` | required | The API's `API_SERVICE_TOKEN` (32 bytes or more) |
|
|
157
|
+
| `jwks_kid` | `dev-main` | `kid` header on service tokens |
|
|
158
|
+
| `cookie_domain` | none | Cookie `Domain` |
|
|
159
|
+
| `insecure_cookies` | `False` | Drops `Secure`, for local development over HTTP |
|
|
160
|
+
| `same_site` | `None`, or `Lax` with `insecure_cookies` | Cookie `SameSite` |
|
|
161
|
+
| `allowed_origins` | none | The only cross-origin callers allowed to change state while cookies are `SameSite=None` |
|
|
162
|
+
| `access_cookie_name`, `refresh_cookie_name` | `seamless-access`, `seamless-refresh` | Session cookie names |
|
|
163
|
+
| `registration_cookie_name`, `pre_auth_cookie_name` | `seamless-ephemeral` | Sign-in flow cookie names |
|
|
164
|
+
| `deliver` | none | Sends OTP codes and magic links through your own transports |
|
|
165
|
+
| `resolve_client_ip` | the connecting peer | The end user's address. See below |
|
|
166
|
+
| `disable_manifest_fetch` | `False` | Use only the manifest bundled with this version |
|
|
167
|
+
| `http_client` | 15 second timeout, no redirects | Outbound `httpx2.Client` |
|
|
168
|
+
|
|
169
|
+
### Client IP
|
|
170
|
+
|
|
171
|
+
The adapter forwards the end user's address and user agent so the auth API rate limits and
|
|
172
|
+
audits against the user, not your server. Behind a proxy, name it:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from seamless_auth import TrustedProxies
|
|
176
|
+
|
|
177
|
+
auth = Adapter(..., resolve_client_ip=TrustedProxies(["10.0.0.0/8"]))
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`TrustedProxies` walks `X-Forwarded-For` from the right, skipping trusted proxies. There is
|
|
181
|
+
deliberately no hop-count option: a hop count cannot tell a proxy from a client that sent its
|
|
182
|
+
own header.
|
|
183
|
+
|
|
184
|
+
### Delivery
|
|
185
|
+
|
|
186
|
+
Without `deliver`, the auth API sends OTP codes and magic links itself. With it, the adapter
|
|
187
|
+
asks the API for the message and hands it to you:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from seamless_auth import Delivery
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def deliver(d: Delivery) -> None:
|
|
194
|
+
mailer.send(d.to, d.kind, d.token, d.magic_link_url)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
auth = Adapter(..., deliver=deliver)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
An `async def` callback works too; under FastAPI it runs on the application's event loop. A
|
|
201
|
+
delivery error answers the request with 502 `delivery_failed`.
|
|
202
|
+
|
|
203
|
+
## How it runs
|
|
204
|
+
|
|
205
|
+
The adapter is synchronous and thread-safe: one `Adapter` serves the whole process. Under FastAPI
|
|
206
|
+
the bindings run it on Starlette's threadpool, so it never blocks the event loop. Under Django it
|
|
207
|
+
runs in the request thread, on WSGI or ASGI. Refresh sharing (one rotation per refresh token,
|
|
208
|
+
handed to every parallel request for 5 seconds) works across threads.
|
|
209
|
+
|
|
210
|
+
## The manifest
|
|
211
|
+
|
|
212
|
+
On its first request the adapter fetches `/.well-known/seamless-adapter.json` from the auth API
|
|
213
|
+
(5 second timeout) and keeps it for the life of the process. If the API serves none, it uses the
|
|
214
|
+
copy bundled in this package and tries again a minute later. A manifest with anything this
|
|
215
|
+
version does not understand is refused whole, rather than following a route with the wrong token.
|
|
216
|
+
Refresh the bundled copy with `scripts/sync-manifest.sh`.
|
|
217
|
+
|
|
218
|
+
## Conformance
|
|
219
|
+
|
|
220
|
+
`conformance/` holds the FastAPI and Django reference apps the conformance suite drives. To run
|
|
221
|
+
one locally you need Docker, a checkout of `seamless-auth-api` and the `seamless` CLI:
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
PORT=8080 AUTH_SERVER_URL=http://localhost:5312 AUTH_SERVER_ISSUER=http://auth-api:5312 \
|
|
225
|
+
API_SERVICE_TOKEN=verify-dev-service-token-not-a-real-secret \
|
|
226
|
+
COOKIE_SIGNING_KEY=verify-dev-service-token-not-a-real-secret JWKS_KID=dev-main \
|
|
227
|
+
uv run python conformance/fastapi_app.py & # or conformance/django_app.py
|
|
228
|
+
|
|
229
|
+
SEAMLESS_API_DIR=../seamless-auth-api seamless verify --adapter-url=http://localhost:8080
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Status
|
|
233
|
+
|
|
234
|
+
Pre-1.0. The public API may change between minor versions until 1.0.
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
Apache-2.0
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# seamless-auth-python
|
|
2
|
+
|
|
3
|
+
A [Seamless Auth](https://github.com/fells-code/seamless-auth-api) server adapter for Python, with
|
|
4
|
+
bindings for [FastAPI](https://fastapi.tiangolo.com) and [Django](https://www.djangoproject.com).
|
|
5
|
+
|
|
6
|
+
The adapter sits in your backend between your users and the Seamless Auth API:
|
|
7
|
+
|
|
8
|
+
- **Browsers** talk to it over `HttpOnly` cookies on your own domain. The API's tokens never
|
|
9
|
+
reach page scripts.
|
|
10
|
+
- **Native clients** (mobile, CLIs) talk to it over bearer tokens, sending
|
|
11
|
+
`x-seamless-auth-transport: bearer`.
|
|
12
|
+
- **The auth API** sees bearer tokens plus a service token that lets it trust the client
|
|
13
|
+
address and user agent the adapter forwards.
|
|
14
|
+
|
|
15
|
+
Which routes the adapter serves, and what each does to the session, comes from the adapter
|
|
16
|
+
manifest the auth API publishes, so a new API route works without a new release of this package.
|
|
17
|
+
|
|
18
|
+
It is held to the same [conformance suite](https://github.com/fells-code/seamless-cli/blob/main/verify/CONFORMANCE.md)
|
|
19
|
+
as the Express, Fastify, Go and Rust adapters, with a FastAPI and a Django reference app, in CI on
|
|
20
|
+
every change.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install "seamless-auth[fastapi]" # or "seamless-auth[django]"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Python 3.11 or later. Django 5.2 or later.
|
|
29
|
+
|
|
30
|
+
## FastAPI
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import os
|
|
34
|
+
|
|
35
|
+
from fastapi import Depends, FastAPI
|
|
36
|
+
from seamless_auth import Adapter, User
|
|
37
|
+
from seamless_auth.fastapi import RequireUser, auth_router
|
|
38
|
+
|
|
39
|
+
auth = Adapter(
|
|
40
|
+
auth_server_url=os.environ["AUTH_SERVER_URL"],
|
|
41
|
+
cookie_secret=os.environ["COOKIE_SECRET"], # at least 32 bytes
|
|
42
|
+
service_secret=os.environ["SERVICE_SECRET"], # the API's API_SERVICE_TOKEN
|
|
43
|
+
jwks_kid=os.environ["JWKS_KID"],
|
|
44
|
+
)
|
|
45
|
+
current_user = RequireUser(auth)
|
|
46
|
+
|
|
47
|
+
app = FastAPI()
|
|
48
|
+
# The auth routes, at /auth, which is where the client SDKs call.
|
|
49
|
+
app.include_router(auth_router(auth))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# Your own routes, behind the adapter's guard.
|
|
53
|
+
@app.get("/api/me")
|
|
54
|
+
def me(user: User = Depends(current_user)):
|
|
55
|
+
return {"id": user.id}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`RequireUser` answers 401 unless the request carries a session, and returns the `User`.
|
|
59
|
+
|
|
60
|
+
## Django
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
# settings.py
|
|
64
|
+
SEAMLESS_AUTH = {
|
|
65
|
+
"auth_server_url": os.environ["AUTH_SERVER_URL"],
|
|
66
|
+
"cookie_secret": os.environ["COOKIE_SECRET"],
|
|
67
|
+
"service_secret": os.environ["SERVICE_SECRET"],
|
|
68
|
+
"jwks_kid": os.environ["JWKS_KID"],
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
# urls.py
|
|
72
|
+
urlpatterns = [
|
|
73
|
+
path("auth/", include("seamless_auth.django")),
|
|
74
|
+
path("api/me", views.me),
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
# views.py
|
|
78
|
+
from django.http import JsonResponse
|
|
79
|
+
from seamless_auth.django import require_auth, user_of
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@require_auth
|
|
83
|
+
def me(request):
|
|
84
|
+
return JsonResponse({"id": user_of(request).id})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`SEAMLESS_AUTH` takes the keyword arguments of `Adapter` (below). `deliver` and
|
|
88
|
+
`resolve_client_ip` may be dotted paths to callables. `require_auth` works on sync and async
|
|
89
|
+
views. The auth routes are exempt from Django's CSRF middleware: the adapter does its own
|
|
90
|
+
cross-site checks (below), and the client SDKs send no CSRF token.
|
|
91
|
+
|
|
92
|
+
## The guard
|
|
93
|
+
|
|
94
|
+
The guard (`RequireUser`, `require_auth`) accepts the adapter's session cookie, or an auth API
|
|
95
|
+
access token in `Authorization: Bearer` for clients with no cookie jar. The cookie wins when both
|
|
96
|
+
are present. It does not refresh: the auth routes refresh a browser session silently, and a bearer
|
|
97
|
+
client calls `POST /auth/refresh` itself. `Adapter.authenticate(headers)` does the same check
|
|
98
|
+
anywhere else.
|
|
99
|
+
|
|
100
|
+
A request the guard admits on the session cookie also gets the auth routes' cross-site check:
|
|
101
|
+
while cookies are `SameSite=None`, a state-changing request from another site (`Sec-Fetch-Site:
|
|
102
|
+
cross-site`, or an `Origin` outside `allowed_origins`) answers 403. A bearer token is never
|
|
103
|
+
attached by a browser, so it is not checked.
|
|
104
|
+
|
|
105
|
+
### Request bodies
|
|
106
|
+
|
|
107
|
+
The auth routes forward a request body to the auth API as JSON, so a body must be sent as
|
|
108
|
+
`application/json` (or a `+json` type). Anything else answers 415 `unsupported_media_type`: a
|
|
109
|
+
cross-site form can post a `text/plain` body shaped like JSON with no CORS preflight, and read as
|
|
110
|
+
JSON it would be a sign-in the user never made. The client SDKs already send JSON.
|
|
111
|
+
|
|
112
|
+
## Options
|
|
113
|
+
|
|
114
|
+
| Argument | Default | Purpose |
|
|
115
|
+
| --- | --- | --- |
|
|
116
|
+
| `auth_server_url` | required | Where the adapter reaches the auth API |
|
|
117
|
+
| `auth_server_issuer` | the URL | Expected `iss` of the API's tokens, when it differs from the URL you reach it at |
|
|
118
|
+
| `audience` | the issuer | Expected `aud` of the API's tokens |
|
|
119
|
+
| `cookie_secret` | required | Signs the session cookies (32 bytes or more) |
|
|
120
|
+
| `service_secret` | required | The API's `API_SERVICE_TOKEN` (32 bytes or more) |
|
|
121
|
+
| `jwks_kid` | `dev-main` | `kid` header on service tokens |
|
|
122
|
+
| `cookie_domain` | none | Cookie `Domain` |
|
|
123
|
+
| `insecure_cookies` | `False` | Drops `Secure`, for local development over HTTP |
|
|
124
|
+
| `same_site` | `None`, or `Lax` with `insecure_cookies` | Cookie `SameSite` |
|
|
125
|
+
| `allowed_origins` | none | The only cross-origin callers allowed to change state while cookies are `SameSite=None` |
|
|
126
|
+
| `access_cookie_name`, `refresh_cookie_name` | `seamless-access`, `seamless-refresh` | Session cookie names |
|
|
127
|
+
| `registration_cookie_name`, `pre_auth_cookie_name` | `seamless-ephemeral` | Sign-in flow cookie names |
|
|
128
|
+
| `deliver` | none | Sends OTP codes and magic links through your own transports |
|
|
129
|
+
| `resolve_client_ip` | the connecting peer | The end user's address. See below |
|
|
130
|
+
| `disable_manifest_fetch` | `False` | Use only the manifest bundled with this version |
|
|
131
|
+
| `http_client` | 15 second timeout, no redirects | Outbound `httpx2.Client` |
|
|
132
|
+
|
|
133
|
+
### Client IP
|
|
134
|
+
|
|
135
|
+
The adapter forwards the end user's address and user agent so the auth API rate limits and
|
|
136
|
+
audits against the user, not your server. Behind a proxy, name it:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from seamless_auth import TrustedProxies
|
|
140
|
+
|
|
141
|
+
auth = Adapter(..., resolve_client_ip=TrustedProxies(["10.0.0.0/8"]))
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`TrustedProxies` walks `X-Forwarded-For` from the right, skipping trusted proxies. There is
|
|
145
|
+
deliberately no hop-count option: a hop count cannot tell a proxy from a client that sent its
|
|
146
|
+
own header.
|
|
147
|
+
|
|
148
|
+
### Delivery
|
|
149
|
+
|
|
150
|
+
Without `deliver`, the auth API sends OTP codes and magic links itself. With it, the adapter
|
|
151
|
+
asks the API for the message and hands it to you:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from seamless_auth import Delivery
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def deliver(d: Delivery) -> None:
|
|
158
|
+
mailer.send(d.to, d.kind, d.token, d.magic_link_url)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
auth = Adapter(..., deliver=deliver)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
An `async def` callback works too; under FastAPI it runs on the application's event loop. A
|
|
165
|
+
delivery error answers the request with 502 `delivery_failed`.
|
|
166
|
+
|
|
167
|
+
## How it runs
|
|
168
|
+
|
|
169
|
+
The adapter is synchronous and thread-safe: one `Adapter` serves the whole process. Under FastAPI
|
|
170
|
+
the bindings run it on Starlette's threadpool, so it never blocks the event loop. Under Django it
|
|
171
|
+
runs in the request thread, on WSGI or ASGI. Refresh sharing (one rotation per refresh token,
|
|
172
|
+
handed to every parallel request for 5 seconds) works across threads.
|
|
173
|
+
|
|
174
|
+
## The manifest
|
|
175
|
+
|
|
176
|
+
On its first request the adapter fetches `/.well-known/seamless-adapter.json` from the auth API
|
|
177
|
+
(5 second timeout) and keeps it for the life of the process. If the API serves none, it uses the
|
|
178
|
+
copy bundled in this package and tries again a minute later. A manifest with anything this
|
|
179
|
+
version does not understand is refused whole, rather than following a route with the wrong token.
|
|
180
|
+
Refresh the bundled copy with `scripts/sync-manifest.sh`.
|
|
181
|
+
|
|
182
|
+
## Conformance
|
|
183
|
+
|
|
184
|
+
`conformance/` holds the FastAPI and Django reference apps the conformance suite drives. To run
|
|
185
|
+
one locally you need Docker, a checkout of `seamless-auth-api` and the `seamless` CLI:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
PORT=8080 AUTH_SERVER_URL=http://localhost:5312 AUTH_SERVER_ISSUER=http://auth-api:5312 \
|
|
189
|
+
API_SERVICE_TOKEN=verify-dev-service-token-not-a-real-secret \
|
|
190
|
+
COOKIE_SIGNING_KEY=verify-dev-service-token-not-a-real-secret JWKS_KID=dev-main \
|
|
191
|
+
uv run python conformance/fastapi_app.py & # or conformance/django_app.py
|
|
192
|
+
|
|
193
|
+
SEAMLESS_API_DIR=../seamless-auth-api seamless verify --adapter-url=http://localhost:8080
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Status
|
|
197
|
+
|
|
198
|
+
Pre-1.0. The public API may change between minor versions until 1.0.
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
Apache-2.0
|