-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi.py
360 lines (307 loc) · 14.1 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""MystBin. Share code easily.
Copyright (C) 2020-Current PythonistaGuild
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import datetime
import json
from typing import TYPE_CHECKING, Any
import asyncpg
import starlette_plus
from core import CONFIG
from core.utils import validate_paste
if TYPE_CHECKING:
from core import Application
class APIView(starlette_plus.View, prefix="api"):
def __init__(self, app: Application) -> None:
self.app: Application = app
@starlette_plus.route("/paste/{id}", methods=["GET"])
@starlette_plus.route("/pastes/{id}", methods=["GET"], include_in_schema=False)
@starlette_plus.limit(**CONFIG["LIMITS"]["paste_get"])
@starlette_plus.limit(**CONFIG["LIMITS"]["paste_get_day"])
async def paste_get(self, request: starlette_plus.Request) -> starlette_plus.Response:
"""Fetch a paste.
---
summary: Fetch a paste.
description:
Fetches a paste with all relevant meta-data and files.\n\n
Fetching pastes does not include the `password` or `safety` fields. You only receive the `safety` field
directly after creating a paste.
parameters:
- in: path
name: id
schema:
type: string
required: true
description: The paste ID.
- in: header
name: Authorization
schema:
type: string
format: basic
required: false
description: The password for the paste; if one is required.
responses:
200:
description: The paste meta-data and files.
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: abc123
created_at:
type: string
example: 2024-01-01T00:00:00.000000+00:00
expires:
type: string
views:
type: integer
example: 3
has_password:
type: boolean
example: false
files:
type: array
items:
type: object
properties:
parent_id:
type: string
content:
type: string
filename:
type: string
loc:
type: integer
charcount:
type: integer
annotation:
type: string
404:
description: The paste does not exist or has been previously deleted.
content:
application/json:
schema:
type: object
properties:
error:
type: string
401:
description: You are not authorized to view this paste or you provided an incorrect password.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Unauthorized.
429:
description: You are requesting too fast.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: You are requesting too fast.
"""
password: str | None = request.headers.get("authorization", None)
identifier: str = request.path_params["id"]
paste = await self.app.database.fetch_paste(identifier, password=password)
if not paste:
return starlette_plus.JSONResponse(
{"error": f'A paste with the id "{identifier}" could not be found or has expired.'}, status_code=404
)
if paste.has_password and not paste.password_ok:
return starlette_plus.JSONResponse({"error": "Unauthorized"}, status_code=401)
to_return: dict[str, Any] = paste.serialize(exclude=["safety", "password", "password_ok"])
return starlette_plus.JSONResponse(to_return)
@starlette_plus.route("/paste", methods=["POST"])
@starlette_plus.route("/pastes", methods=["POST"], include_in_schema=False)
@starlette_plus.limit(**CONFIG["LIMITS"]["paste_post"])
@starlette_plus.limit(**CONFIG["LIMITS"]["paste_post_day"])
async def paste_post(self, request: starlette_plus.Request) -> starlette_plus.Response:
"""Create a paste.
---
summary: Create a paste.
description:
Creates a paste with or without multiple files for view on the web or via the API.
You can use this endpoint to POST valid `JSON` data or `plain-text` content.\n\n\n
When using `plain-text`, only one file will be created, without a password or expiry.\n\n\n
Max Character per file is `300_000`.\n\n
Max file limit is `5`.\n\n
If the paste is regarded as public, and contains Discord authorization tokens,
then these will be invalidated upon paste creation.\n\n
requestBody:
description: The paste data. `password` and `expires` are optional.
content:
application/json:
schema:
type: object
properties:
files:
type: array
items:
type: object
properties:
filename:
type: string
required: false
content:
type: string
required: true
example:
- filename: thing.py
content: print(\"Hello World!\")
- content: Some text or code...
password:
required: false
type: string
example: null
expires:
required: false
type: string
example: null
text/plain:
schema:
type: string
responses:
200:
description: The paste meta-data.
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: abc123
created_at:
type: string
example: 2024-01-01T00:00:00.000000+00:00
expires:
type: string
example: 2024-01-01T00:00:00.000000+00:00
safety:
type: string
400:
description: The paste data was invalid.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: The reason the paste was invalid.
429:
description: You are requesting too fast.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: You are requesting too fast.
"""
content_type: str | None = request.headers.get("content-type", None)
body: dict[str, Any] | str
data: dict[str, Any]
if content_type == "application/json":
try:
body = await request.json()
except json.JSONDecodeError:
return starlette_plus.JSONResponse({"error": "Invalid JSON provided."}, status_code=400)
else:
body = (await request.body()).decode(encoding="UTF-8")
data = {"files": [{"content": body, "filename": None}]} if isinstance(body, str) else body
if resp := validate_paste(data):
return resp
expiry_str: str | None = data.get("expires", None)
try:
expiry: datetime.datetime | None = datetime.datetime.fromisoformat(expiry_str) if expiry_str else None
except Exception as e:
return starlette_plus.JSONResponse({"error": f'Unable to parse "expiry" parameter: {e}'}, status_code=400)
data["expires"] = expiry
data["password"] = data.get("password")
try:
paste = await self.app.database.create_paste(data=data)
except asyncpg.CharacterNotInRepertoireError:
message: str = "File(s)/Filename(s) contain invalid characters or byte sequences."
return starlette_plus.JSONResponse({"error": message}, status_code=400)
to_return: dict[str, Any] = paste.serialize(exclude=["password", "password_ok"])
to_return.pop("files", None)
return starlette_plus.JSONResponse(to_return, status_code=200)
@starlette_plus.route("/security/info/{token}")
async def security_info(self, request: starlette_plus.Request) -> starlette_plus.Response:
token: str | None = request.path_params.get("token", None)
if not token:
return starlette_plus.JSONResponse({"error": "Unauthorized."}, status_code=401)
paste = await self.app.database.fetch_paste_security(token=token)
if not paste:
return starlette_plus.JSONResponse(
{"error": "A paste was not found with the provided token, or has expired or been deleted."},
status_code=404,
)
delete: str = f"{request.url.scheme}://{request.url.hostname}/api/security/delete/{token}"
info: str = f"{request.url.scheme}://{request.url.hostname}/api/security/info/{token}"
data: dict[str, str] = {
"token": paste.safety,
"delete": delete,
"info": info,
"extra": "Visiting the delete URL will remove the paste instantly.",
}
return starlette_plus.JSONResponse(data, status_code=200)
@starlette_plus.route("/security/delete/{token}", methods=["GET"])
async def security_delete(self, request: starlette_plus.Request) -> starlette_plus.Response:
"""Delete a paste.
---
summary: Delete a paste.
description:
Deletes a paste with the associated safety token.\n\n
This action is not reversible.
parameters:
- in: path
name: token
schema:
type: string
required: true
description: The safety token received when creating the paste.
responses:
200:
description: The paste was successfully deleted.
content:
text/plain:
schema:
type: string
401:
description: You are not authorized to delete this paste.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Unauthorized.
"""
token: str | None = request.path_params.get("token", None)
if not token:
return starlette_plus.JSONResponse({"error": "Unauthorized."}, status_code=401)
await self.app.database.delete_paste_security(token=token)
return starlette_plus.Response("Ok", status_code=200)