jiayang 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.
- jiayang-0.1.0/.gitignore +4 -0
- jiayang-0.1.0/CHANGELOG.md +13 -0
- jiayang-0.1.0/LICENSE +202 -0
- jiayang-0.1.0/NOTICE +4 -0
- jiayang-0.1.0/PKG-INFO +285 -0
- jiayang-0.1.0/README.md +249 -0
- jiayang-0.1.0/jiayang/__init__.py +484 -0
- jiayang-0.1.0/jiayang/aio.py +74 -0
- jiayang-0.1.0/jiayang/django.py +123 -0
- jiayang-0.1.0/jiayang/fastapi.py +101 -0
- jiayang-0.1.0/jiayang/flask.py +121 -0
- jiayang-0.1.0/jiayang/gradio.py +39 -0
- jiayang-0.1.0/jiayang/py.typed +0 -0
- jiayang-0.1.0/jiayang/streamlit.py +54 -0
- jiayang-0.1.0/pyproject.toml +58 -0
- jiayang-0.1.0/tests/smoke.sh +32 -0
- jiayang-0.1.0/tests/smoke_test.py +151 -0
- jiayang-0.1.0/tests/support.py +100 -0
- jiayang-0.1.0/tests/test_aio.py +105 -0
- jiayang-0.1.0/tests/test_django.py +114 -0
- jiayang-0.1.0/tests/test_fastapi.py +88 -0
- jiayang-0.1.0/tests/test_flask.py +109 -0
- jiayang-0.1.0/tests/test_require_user.py +347 -0
- jiayang-0.1.0/tests/test_roles.py +44 -0
- jiayang-0.1.0/tests/test_streamlit_and_gradio.py +100 -0
- jiayang-0.1.0/tests/test_vectors.py +94 -0
- jiayang-0.1.0/tests/test_webhook.py +69 -0
- jiayang-0.1.0/uv.lock +2848 -0
jiayang-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 - 2026-09-25
|
|
4
|
+
|
|
5
|
+
First release, so there is nothing to upgrade from.
|
|
6
|
+
|
|
7
|
+
- `require_user` and `verify_identity` check the identity token the platform sends with a person's
|
|
8
|
+
or a bypass token's request, with Flask, FastAPI, Django, Streamlit and Gradio integrations.
|
|
9
|
+
- `require_webhook` and `verify_webhook` check the token it sends with a webhook delivery whose
|
|
10
|
+
signature it verified, and name the provider. The integrations are `webhook_required` and
|
|
11
|
+
`get_webhook` for Flask, the `webhook` dependency for FastAPI, and `webhook_required` for Django,
|
|
12
|
+
which exempts its view from the CSRF check. `jiayang.aio` has both calls. Each kind of token is
|
|
13
|
+
refused where the other is expected.
|
jiayang-0.1.0/LICENSE
ADDED
|
@@ -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.
|
jiayang-0.1.0/NOTICE
ADDED
jiayang-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: jiayang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Verify who is calling your Jiayang Cloud app.
|
|
5
|
+
Project-URL: Homepage, https://jiayang.cloud
|
|
6
|
+
Project-URL: Documentation, https://jiayang.cloud/docs/sdks/python/
|
|
7
|
+
Project-URL: Source, https://github.com/ss2d22/jiayang/tree/main/sdks/python
|
|
8
|
+
Project-URL: Changelog, https://github.com/ss2d22/jiayang/blob/main/sdks/python/CHANGELOG.md
|
|
9
|
+
Project-URL: Issues, https://github.com/ss2d22/jiayang/issues
|
|
10
|
+
Author: The Jiayang Cloud Authors
|
|
11
|
+
License-Expression: Apache-2.0
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
License-File: NOTICE
|
|
14
|
+
Keywords: auth,identity,jiayang,jwt
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Security
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: pyjwt[crypto]<3,>=2.10.1
|
|
25
|
+
Provides-Extra: django
|
|
26
|
+
Requires-Dist: django>=4.2; extra == 'django'
|
|
27
|
+
Provides-Extra: fastapi
|
|
28
|
+
Requires-Dist: fastapi>=0.115; extra == 'fastapi'
|
|
29
|
+
Provides-Extra: flask
|
|
30
|
+
Requires-Dist: flask>=3; extra == 'flask'
|
|
31
|
+
Provides-Extra: gradio
|
|
32
|
+
Requires-Dist: gradio>=5; extra == 'gradio'
|
|
33
|
+
Provides-Extra: streamlit
|
|
34
|
+
Requires-Dist: streamlit>=1.37; extra == 'streamlit'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# jiayang (Python)
|
|
38
|
+
|
|
39
|
+
Verifies who is calling your Jiayang Cloud app. Full docs: https://jiayang.cloud/docs/sdks/python/
|
|
40
|
+
|
|
41
|
+
The edge sends a 60-second identity token in `X-Jiayang-Identity` on every request it lets through.
|
|
42
|
+
`require_user` checks the RS256 signature against the platform's keys, plus issuer, audience (your
|
|
43
|
+
app) and expiry, and returns the caller. A missing or bad token raises `Unauthorized`, and so does
|
|
44
|
+
a request that only carries `X-Jiayang-Email`.
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pip install jiayang
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from jiayang import require_user, Unauthorized
|
|
52
|
+
|
|
53
|
+
@app.get("/")
|
|
54
|
+
def index():
|
|
55
|
+
try:
|
|
56
|
+
user = require_user(request.headers)
|
|
57
|
+
except Unauthorized:
|
|
58
|
+
return "unauthorized", 401
|
|
59
|
+
return f"hello {user.email}"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Headers in any shape: a mapping, Django's `request.META`, anything with `.get`.
|
|
63
|
+
|
|
64
|
+
The platform sets `JIAYANG_APP_ID`, `JIAYANG_IDENTITY_ISSUER` and `JIAYANG_JWKS_URL`. If any is
|
|
65
|
+
missing, or the keys can't be fetched, every request is refused.
|
|
66
|
+
|
|
67
|
+
`user.kind` is `"user"` (with `email`) or `"service"` for a bypass token (`email` is `None`).
|
|
68
|
+
`user.role` is the caller's access to this app: `viewer`, `editor` or `owner`, in that order.
|
|
69
|
+
`require_role(user, "editor")` raises `Forbidden`; `has_role` asks without raising.
|
|
70
|
+
|
|
71
|
+
## Your framework
|
|
72
|
+
|
|
73
|
+
Each of these only needs its own framework. Install `jiayang[flask]`, `jiayang[fastapi]`,
|
|
74
|
+
`jiayang[django]`, `jiayang[streamlit]` or `jiayang[gradio]` to say so.
|
|
75
|
+
|
|
76
|
+
### Flask
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from jiayang.flask import current_user, get_user, login_required
|
|
80
|
+
|
|
81
|
+
@app.get("/")
|
|
82
|
+
@login_required
|
|
83
|
+
def index():
|
|
84
|
+
return f"hello {current_user.email}"
|
|
85
|
+
|
|
86
|
+
@app.post("/orders")
|
|
87
|
+
@login_required(role="editor")
|
|
88
|
+
def place_order():
|
|
89
|
+
...
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
No identity is 401, too little role is 403, and neither reaches the view. `get_user()` returns the
|
|
93
|
+
caller or `None`, for a page that would rather render than refuse.
|
|
94
|
+
|
|
95
|
+
### FastAPI
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from typing import Annotated
|
|
99
|
+
|
|
100
|
+
from fastapi import Depends
|
|
101
|
+
from jiayang import User
|
|
102
|
+
from jiayang.fastapi import CurrentUser, requires
|
|
103
|
+
|
|
104
|
+
@app.get("/")
|
|
105
|
+
async def index(user: CurrentUser):
|
|
106
|
+
return {"hello": user.email}
|
|
107
|
+
|
|
108
|
+
@app.post("/orders")
|
|
109
|
+
async def place_order(user: Annotated[User, Depends(requires("editor"))]):
|
|
110
|
+
...
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`optional_user` is the dependency that answers `None` instead of refusing. These verify
|
|
114
|
+
asynchronously, as `jiayang.aio` below does.
|
|
115
|
+
|
|
116
|
+
### Django
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
MIDDLEWARE = ["jiayang.django.JiayangMiddleware", ...]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from django.http import HttpResponse
|
|
124
|
+
from jiayang.django import login_required, role_required
|
|
125
|
+
|
|
126
|
+
@login_required
|
|
127
|
+
def account(request):
|
|
128
|
+
return HttpResponse(f"hello {request.jiayang_user.email}")
|
|
129
|
+
|
|
130
|
+
@role_required("editor")
|
|
131
|
+
def place_order(request):
|
|
132
|
+
...
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The middleware sets `request.jiayang_user` to the caller or `None`, and refuses nothing by itself,
|
|
136
|
+
so an app can have a public page and a private one. `login_required` and `role_required` are what
|
|
137
|
+
refuse: 401 without an identity, 403 with too little role. Import them from `jiayang.django`:
|
|
138
|
+
Django's own `login_required` checks Django's sessions and redirects to `LOGIN_URL`.
|
|
139
|
+
|
|
140
|
+
### Streamlit
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from jiayang.streamlit import get_user
|
|
144
|
+
|
|
145
|
+
user = get_user()
|
|
146
|
+
if user is None:
|
|
147
|
+
st.error("sign in to use this")
|
|
148
|
+
st.stop()
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Verified once per session and remembered. Streamlit only sees the headers of the request that
|
|
152
|
+
opened the session, and an identity token is good for sixty seconds, so checking again on the next
|
|
153
|
+
rerun would refuse a caller who never left. Nothing is lost by remembering: the edge closes the
|
|
154
|
+
connection within a minute of someone's access being taken away.
|
|
155
|
+
|
|
156
|
+
### Gradio
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from jiayang.gradio import require_user
|
|
160
|
+
|
|
161
|
+
def answer(question, request: gr.Request):
|
|
162
|
+
user = require_user(request)
|
|
163
|
+
...
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Gradio passes `None` for a handler reached through the API or a cached example. That's a caller the
|
|
167
|
+
app knows nothing about, so it's refused.
|
|
168
|
+
|
|
169
|
+
## Webhooks
|
|
170
|
+
|
|
171
|
+
A webhook provider can't sign in, so its route is a public path, opened in the dashboard (Sharing,
|
|
172
|
+
Public paths) along with the provider that signs it and that provider's signing secret. The platform
|
|
173
|
+
checks each delivery's signature before your app sees it and sends it on with a token of kind
|
|
174
|
+
`webhook`. `require_webhook` checks that token and tells you which provider it came from. Your app
|
|
175
|
+
never holds the signing secret.
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from jiayang import Unauthorized, require_webhook
|
|
179
|
+
|
|
180
|
+
@app.post("/hooks/stripe")
|
|
181
|
+
def stripe_hook():
|
|
182
|
+
try:
|
|
183
|
+
hook = require_webhook(request.headers, provider="stripe")
|
|
184
|
+
except Unauthorized:
|
|
185
|
+
return "unauthorized", 401
|
|
186
|
+
event = request.get_json()
|
|
187
|
+
# hook.delivery is Stripe's event id. Skip one you've handled.
|
|
188
|
+
return "", 204
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
`provider` is required: one of `stripe`, `github`, `slack`, `shopify`, `standard_webhooks` and
|
|
192
|
+
`hmac_sha256`, or a list of them. A delivery from any other provider is refused, so a Stripe route
|
|
193
|
+
can't be handed a GitHub delivery after a pattern is widened. `require_user` refuses a webhook's
|
|
194
|
+
token and `require_webhook` refuses a person's.
|
|
195
|
+
|
|
196
|
+
It returns a `Webhook(provider, pattern, delivery, signed_at, workspace_id)`. `pattern` is the
|
|
197
|
+
public path it came in on, such as `/hooks/stripe` or `/hooks/*`. `delivery` is the provider's
|
|
198
|
+
signed id for it and `signed_at` the signed time in unix seconds, each where the provider signs one
|
|
199
|
+
(Stripe, Slack events, Standard Webhooks) and `None` otherwise. `jiayang.aio` has both as
|
|
200
|
+
coroutines.
|
|
201
|
+
|
|
202
|
+
The body arrives as the provider sent it, and nothing needs its raw bytes any more, so read it
|
|
203
|
+
however the framework does.
|
|
204
|
+
|
|
205
|
+
Each framework checks per route, so a webhook's route and a person's can sit side by side:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
# Flask
|
|
209
|
+
from jiayang.flask import get_webhook, webhook_required
|
|
210
|
+
|
|
211
|
+
@app.post("/hooks/stripe")
|
|
212
|
+
@webhook_required(provider="stripe")
|
|
213
|
+
def stripe_hook():
|
|
214
|
+
hook = get_webhook()
|
|
215
|
+
...
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
With Flask-WTF's `CSRFProtect`, exempt the route: a provider has no CSRF token to send, and the
|
|
219
|
+
platform's token is what lets the delivery in.
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
@app.post("/hooks/stripe")
|
|
223
|
+
@csrf.exempt
|
|
224
|
+
@webhook_required(provider="stripe")
|
|
225
|
+
def stripe_hook(): ...
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
# FastAPI
|
|
230
|
+
from typing import Annotated
|
|
231
|
+
|
|
232
|
+
from fastapi import Depends, Request
|
|
233
|
+
from jiayang import Webhook
|
|
234
|
+
from jiayang.fastapi import webhook
|
|
235
|
+
|
|
236
|
+
@app.post("/hooks/stripe")
|
|
237
|
+
async def stripe_hook(hook: Annotated[Webhook, Depends(webhook("stripe"))], request: Request):
|
|
238
|
+
event = await request.json()
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
# Django
|
|
243
|
+
from jiayang.django import webhook_required
|
|
244
|
+
|
|
245
|
+
@webhook_required(provider="stripe")
|
|
246
|
+
def stripe_hook(request):
|
|
247
|
+
hook = request.jiayang_webhook
|
|
248
|
+
event = json.loads(request.body)
|
|
249
|
+
...
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
`JiayangMiddleware` refuses nothing by itself, so it can stay installed for the whole project.
|
|
253
|
+
`webhook_required` exempts its view from Django's CSRF check for the same reason as above.
|
|
254
|
+
|
|
255
|
+
What the token doesn't cover:
|
|
256
|
+
|
|
257
|
+
- Signed deliveries reach your app with a webhook token, and `require_webhook` refuses everything
|
|
258
|
+
else. A route that skips it is open to anyone: for up to a minute after a verifier is added, for
|
|
259
|
+
good once one is removed, if the platform ever rolls its edge back, and on any path that a more
|
|
260
|
+
specific pattern with no verifier decides. A path in another case (`/Hooks/Stripe`) goes to whatever pattern
|
|
261
|
+
matches it and arrives with no token.
|
|
262
|
+
- GitHub, Shopify and plain HMAC senders sign no time, so a captured delivery can be sent again at
|
|
263
|
+
any time, with a different query string and any header but the signature changed. Act on what the
|
|
264
|
+
signed body says, never on `X-GitHub-Event`, `X-Shopify-Topic` or another header, and make that
|
|
265
|
+
safe to repeat.
|
|
266
|
+
- On Stripe, Slack and Standard Webhooks the platform refuses a copy of a delivery your app answered
|
|
267
|
+
with a 2xx. Answer with anything else and a copy can get through within the provider's tolerance,
|
|
268
|
+
so dedupe on `delivery` there as well.
|
|
269
|
+
|
|
270
|
+
## async
|
|
271
|
+
|
|
272
|
+
`require_user` blocks for up to three seconds when it fetches keys, about once every five minutes.
|
|
273
|
+
`jiayang.aio` does the same work without stopping the event loop. The fetch goes to a thread, one
|
|
274
|
+
at a time, and everything else stays on the loop.
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
from fastapi import Request
|
|
278
|
+
from jiayang.aio import require_user
|
|
279
|
+
|
|
280
|
+
@app.get("/")
|
|
281
|
+
async def index(request: Request):
|
|
282
|
+
user = await require_user(request.headers)
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Tests: `uv run --frozen python -m unittest discover -s tests`
|