Repository navigation
Expand file tree
/
Copy pathbackend.py
More file actions
660 lines (610 loc) · 24.1 KB
/
Copy pathbackend.py
File metadata and controls
660 lines (610 loc) · 24.1 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import os
import time
import json
import redis
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, Request, Depends
from slowapi import _rate_limit_exceeded_handler, Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse, FileResponse, Response
from pydantic import BaseModel, Field, EmailStr
from typing import Optional, Dict, Any, Literal
from supabase import create_client, Client
import httpx
import asyncio
from http.cookiejar import Cookie, DefaultCookiePolicy
load_dotenv()
BACKEND_URL = os.getenv('BACKEND_URL', '').rstrip('/')
FRONTEND_URL = os.getenv('FRONTEND_URL', '').rstrip('/')
REDIS_URL=os.getenv("redisurl")
if not BACKEND_URL or not FRONTEND_URL:
raise RuntimeError('BACKEND_URL and FRONTEND_URL must be set in the environment.')
OAUTH_CALLBACK_URL = f'{BACKEND_URL}/api/auth/callback'
WORKSPACE_URL = f'{FRONTEND_URL}/api-tester.html'
PROJECT_ROOT = Path(__file__).resolve().parent
WEBSITE_ROOT = PROJECT_ROOT / 'website'
ASSET_ROOT = PROJECT_ROOT / 'assets'
bearer=HTTPBearer(auto_error=False)
app=FastAPI()
supabase_url = os.getenv('url')
supabase_service_key = os.getenv('service')
supabase: Client = create_client(supabase_url, supabase_service_key)
admin_supabase: Client = create_client(supabase_url, supabase_service_key)
app.add_middleware(CORSMiddleware, allow_credentials=True, allow_headers=['*'], allow_methods=['*'], allow_origins=[FRONTEND_URL])
limiter=Limiter(key_func=get_remote_address)
app.state.limiter=limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
try:
rediscient = (
redis.from_url(REDIS_URL, decode_responses=True, socket_connect_timeout=2)
if REDIS_URL
else None
)
except (TypeError, ValueError):
rediscient = None
class Sendrequest(BaseModel):
method: str= 'GET'
url: str
headers: Dict= {}
params: Dict= {}
body: str=None
timeout: float = Field(default=30, gt=0, le=60)
followredirection: bool=True
credentialsMode: Literal['same-origin', 'include', 'omit'] = 'same-origin'
cookies: list[dict[str, Any]] = Field(default_factory=list, max_length=300)
bodyType: str = "json"
name: Optional[str]=None
folderid: Optional[str]=None
collectionid: Optional[str] = None
class User(BaseModel):
email: EmailStr
password: str=Field(...,min_length=6, max_length=100)
class Refreshtoken(BaseModel):
refreshtoken: str
class Newfolder(BaseModel):
name: str=Field(...,min_length=1, max_length=30)
class Editname(BaseModel):
name: str=Field(...,min_length=1, max_length=30)
AuthType=Literal['none', 'bearer', 'basic', 'apikey']
class CollectionIn(BaseModel):
name: str = Field(..., min_length=1, max_length=30)
headers: list = Field(default=[], max_length=50)
variables: list = Field(default=[], max_length=50)
authtype: AuthType = 'none'
authconfig: dict = {}
settings: dict = {}
requestids: list[str] = Field(default_factory=list, max_length=100)
class Log(BaseModel):
endpoint: str
statuscode: int
message: str
class RejectCookiesPolicy(DefaultCookiePolicy):
def set_ok(self, cookie, request):
return False
def return_ok(self, cookie, request):
return False
@app.post('/api/sendrequest')
@limiter.limit('100/min')
async def sendrequest(request: Request, body: Sendrequest):
start=time.perf_counter()
outgoing_headers = dict(body.headers)
has_content_type = any(
name.lower() == "content-type"
for name in outgoing_headers
)
content_types = {
"json": "application/json",
"urlencoded": "application/x-www-form-urlencoded",
"raw": "text/plain",
"graphql": "application/graphql",
}
if body.body and not has_content_type:
outgoing_headers["Content-Type"] = content_types.get(
body.bodyType,
"text/plain",
)
try:
target_url = httpx.URL(body.url)
page_origin = request.headers.get('origin') or FRONTEND_URL
try:
page_url = httpx.URL(page_origin)
target_port = target_url.port or (443 if target_url.scheme == 'https' else 80)
page_port = page_url.port or (443 if page_url.scheme == 'https' else 80)
same_origin = (
target_url.scheme == page_url.scheme
and target_url.host == page_url.host
and target_port == page_port
)
except httpx.InvalidURL:
same_origin = False
use_cookies = body.credentialsMode == 'include' or (
body.credentialsMode == 'same-origin' and same_origin
)
async with httpx.AsyncClient(
timeout=body.timeout,
follow_redirects=body.followredirection,
) as client:
if not use_cookies:
client.cookies.jar.set_policy(RejectCookiesPolicy())
if use_cookies:
for item in body.cookies:
name = str(item.get('name', ''))
value = str(item.get('value', ''))
domain = str(item.get('domain', '')).lstrip('.').lower()
path = str(item.get('path') or '/')
expires = item.get('expires')
try:
expires_at = int(expires) if expires else None
except (TypeError, ValueError):
continue
if (
not name or any(char in name for char in ';=\r\n')
or any(char in value for char in '\r\n')
or not domain or not path.startswith('/')
or (expires_at and expires_at <= time.time())
):
continue
rest = {}
if item.get('httpOnly'):
rest['HttpOnly'] = True
if item.get('sameSite'):
rest['SameSite'] = str(item['sameSite'])
client.cookies.jar.set_cookie(Cookie(
version=0,
name=name,
value=value,
port=None,
port_specified=False,
domain=domain,
domain_specified=not bool(item.get('hostOnly', True)),
domain_initial_dot=not bool(item.get('hostOnly', True)),
path=path,
path_specified=True,
secure=bool(item.get('secure')),
expires=expires_at,
discard=not bool(expires_at),
comment=None,
comment_url=None,
rest=rest,
rfc2109=False,
))
req = await client.request(
method=body.method,
url=body.url,
headers=outgoing_headers,
params=body.params,
content=body.body or None,
)
cookies = []
if use_cookies:
for cookie in client.cookies.jar:
if len(cookies) >= 300:
break
cookies.append({
'name': cookie.name,
'value': cookie.value,
'domain': cookie.domain.lstrip('.').lower(),
'hostOnly': not cookie.domain_specified,
'path': cookie.path or '/',
'secure': cookie.secure,
'httpOnly': cookie.has_nonstandard_attr('HttpOnly'),
'sameSite': cookie.get_nonstandard_attr('SameSite'),
'expires': cookie.expires,
})
else:
cookies = body.cookies
except (httpx.HTTPError, httpx.InvalidURL) as e:
raise HTTPException(status_code=502, detail=str(e))
ms= round((time.perf_counter() - start)* 1000)
try:
data=req.json()
except ValueError:
data=None
return {
'status': req.status_code,
'reason': req.reason_phrase,
'time': ms,
'size': len(req.content),
'finalurl': str(req.url),
'headers': dict(req.headers),
'cookies': cookies,
'text': req.text,
'json': data
}
@app.post('/api/auth/register')
@limiter.limit('10/minute')
def register(request: Request, body: User):
try:
supabase.auth.sign_up({
'email': body.email,
'password': body.password,
'options': {'email_redirect_to': f'{FRONTEND_URL}/email-verified.html'},
})
return {'message': 'Check your inbox to confim!'}
except Exception as e:
print(str(e))
raise HTTPException(status_code=500, detail='Registration failed')
@app.post('/api/auth/login')
@limiter.limit('10/minute')
def login(request: Request, body: User):
try:
response=supabase.auth.sign_in_with_password({'email': body.email, 'password': body.password})
return {'accestoken': response.session.access_token, 'tefreshtoken': response.session.refresh_token}
except Exception:
raise HTTPException(status_code=500, detail='Invalid email or password')
@app.post('/api/auth/logout')
@limiter.limit('10/minute')
def logout(request: Request, body: Refreshtoken, cred: HTTPAuthorizationCredentials | None=Depends(bearer)):
try:
supabase.auth.set_session(cred.credentials, body.refreshtoken)
supabase.auth.sign_out({'scope': 'local'})
except Exception:
pass
return {'message': 'Logged out'}
@app.post('/api/auth/refresh')
@limiter.limit('10/minute')
def refresh(request: Request, body: Refreshtoken):
try:
response=supabase.auth.refresh_session(body.refreshtoken).session
except Exception:
raise HTTPException(status_code=401, detail="Session expired")
return {'accestoken': response.access_token, 'resfreshtoken': response.refresh_token}
def getuser(cred: HTTPAuthorizationCredentials | None=Depends(bearer)):
try:
if not cred:
raise HTTPException(status_code=401, detail="Not logged in")
user=supabase.auth.get_user(cred.credentials).user
except Exception:
raise HTTPException(status_code=401, detail="Session expired")
if user is None:
raise HTTPException(status_code=401, detail="Session expired")
return {'id': user.id, 'email': user.email, 'created': getattr(user, 'created_at', None)}
@app.get('/api/auth/oauth/{provider}')
@limiter.limit('10/minute')
def oauth(request: Request, provider: Literal['google', 'github']):
response=supabase.auth.sign_in_with_oauth({
'provider': provider,
'options': {'redirect_to': OAUTH_CALLBACK_URL},
})
return RedirectResponse(response.url)
@app.get('/api/auth/callback')
@limiter.limit('10/minute')
def callback(request: Request, code: str | None = None):
fall=RedirectResponse(f'{WORKSPACE_URL}?auth_error=1', status_code=303)
if not code:
return fall
try:
out=supabase.auth.exchange_code_for_session({'auth_code': code})
session = out.session
if not session or not session.access_token or not session.refresh_token:
return fall
except Exception:
return fall
return RedirectResponse(f'{WORKSPACE_URL}#access_token={session.access_token}&refresh_token={session.refresh_token}',
status_code=303)
@app.get('/api/folders', status_code=200)
@limiter.limit('50/minute')
def getfolders(request: Request, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('apifolders').select('*').eq('userid', userid).execute()
if not response.data:
return []
return response.data
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.post('/api/folders/add', status_code=201)
@limiter.limit('50/minute')
def addfolder(request: Request, body: Newfolder, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('apifolders').insert({'name': body.name, 'userid': userid}).execute()
if not response.data:
raise HTTPException(status_code=400, detail="Server error")
return response.data
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.patch('/api/folders/edit/{id}', status_code=204)
@limiter.limit('50/minute')
def editname(request: Request, body: Editname, id: str, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('apifolders').update({'name': body.name}).eq('id', id).eq('userid', userid).execute()
if not response.data:
raise HTTPException(status_code=404, detail="id dosen't exist")
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.delete('/api/folders/delete/{id}', status_code=204)
@limiter.limit('50/minute')
def deleteforder(request: Request, id: str, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('apifolders').delete().eq('id', id).eq('userid', userid).execute()
if not response.data:
raise HTTPException(status_code=404, detail="id dosen't exist")
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.post('/api/folders/addrequest/{folderid}', status_code=200)
@limiter.limit('50/minute')
def addrequesttofolder(request: Request, folderid: str, body: Sendrequest, user=Depends(getuser)):
userid=user['id']
try:
insertdata={
'userid': userid,
'folderid': folderid,
'name': body.name,
'method': body.method,
'url': body.url,
'params': body.params,
'headers': body.headers,
'bodytype': body.bodyType,
'body': body.body,
}
response=supabase.table('requests').insert(insertdata).execute()
if not response.data:
raise HTTPException(status_code=400, detail="Database error")
return response.data[0]
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.get('/api/requests', status_code=200)
@limiter.limit('50/minute')
def getallrequest(request: Request,folderid: Optional[str]=None,collectionid: Optional[str] = None, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('requests').select('*').eq('userid', userid)
if collectionid and collectionid.lower() != 'null':
response=response.eq('collectionid', collectionid).is_('folderid', 'null')
elif folderid and folderid.lower() != 'null':
response=response.eq('folderid', folderid).is_('collectionid', 'null')
else:
response=response.is_('folderid', 'null').is_('collectionid', 'null')
response=response.execute()
if not response.data:
return []
return response.data
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.post('/api/request/add', status_code=201)
@limiter.limit('50/minute')
def saverequest(request: Request,body: Sendrequest ,user=Depends(getuser)):
userid=user['id']
try:
insertdata={
'userid': userid,
'folderid': body.folderid,
'name': body.name,
'method': body.method,
'url': body.url,
'params': body.params,
'headers': body.headers,
'bodytype': body.bodyType,
'body': body.body,
'collectionid': body.collectionid,
}
response=supabase.table('requests').insert(insertdata).execute()
if not response.data:
raise HTTPException(status_code=400, detail="Database error")
return response.data[0]
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.patch('/api/request/rename/{requestid}', status_code=204)
@limiter.limit('50/minute')
def renamerequest(request: Request, requestid: str,body: Editname, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('requests').update({'name': body.name}).eq('userid', userid).eq('id', requestid).execute()
if not response.data:
raise HTTPException(status_code=400, detail="Database error")
return response.data[0]
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.delete('/api/request/delete/{requestid}', status_code=204)
def deleterequest(request: Request, requestid: str, user=Depends(getuser)):
userid=user['id']
try:
supabase.table('requests').delete().eq('userid', userid).eq('id', requestid).execute()
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.get('/api/collections', status_code=200)
@limiter.limit('50/minute')
def getallcollections(request: Request, user=Depends(getuser)):
userid=user['id']
try:
response=supabase.table('collections').select('*').eq('userid', userid).execute()
if not response.data:
return []
return response.data
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.post("/api/addcollection", status_code=201)
@limiter.limit('50/minute')
def addcollections(
request: Request,
body: CollectionIn,
user=Depends(getuser),
):
userid=user["id"]
try:
result = supabase.table("collections").insert({
"userid": userid,
"name": body.name,
"headers": body.headers,
"variables": body.variables,
"authtype": body.authtype,
"authconfig": body.authconfig,
"settings": body.settings,
}).execute()
collection=result.data[0]
for request_id in body.requestids:
source = (
supabase.table("requests")
.select("*")
.eq("id", request_id)
.eq("userid", userid)
.execute()
)
if source.data:
old = source.data[0]
supabase.table("requests").insert({
"userid": userid,
"folderid": None,
"collectionid": collection["id"],
"name": old["name"],
"method": old["method"],
"url": old["url"],
"params": old["params"],
"headers": old["headers"],
"bodytype": old["bodytype"],
"body": old["body"],
}).execute()
return collection
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.delete('/api/collections/delete/{id}', status_code=204)
@limiter.limit('50/minute')
def deletecollection(request: Request, id: str, user=Depends(getuser)):
userid=user["id"]
try:
response=supabase.table('collections').delete().eq('userid', userid).eq('id', id).execute()
if not response.data:
raise HTTPException(status_code=404, detail="Id not found")
return response.data[0]
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.patch('/api/collections/update={id}', status_code=204)
@limiter.limit('50/minute')
def update(request: Request, id: str, new: CollectionIn, user=Depends(getuser)):
userid=user["id"]
newdates={
"userid": userid,
"name": new.name,
"headers": new.headers,
"variables": new.variables,
"authtype": new.authtype,
"authconfig": new.authconfig,
"settings": new.settings,
}
try:
response=supabase.table('collections').update(newdates).eq('userid', userid).eq('id', id).execute()
if not response.data:
raise HTTPException(status_code=404, detail="Id not found")
return response.data[0]
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.get('/api/user', status_code=200)
@limiter.limit('50/minute')
def user(request: Request, user=Depends(getuser)):
try:
return{
'userid': user['id'],
'email': user['email'],
'created': user['created']
}
except Exception:
raise HTTPException(status_code=500, detail="Server error")
@app.get('/api/user/logs', status_code=200)
@limiter.limit('50/minute')
def getlogs(request: Request, user=Depends(getuser)):
userid=user["id"]
key=f"logs:{userid}"
if rediscient:
try:
cacheddata=rediscient.get(key)
if cacheddata:
return json.loads(cacheddata)
except Exception:
pass
try:
response=(
supabase.table('logs')
.select('*')
.eq('userid', userid)
.order('created', desc=True)
.limit(50)
.execute()
)
responsedata= response.data or []
if rediscient:
try:
rediscient.set(key, json.dumps(responsedata), ex=60)
except Exception:
pass
return responsedata
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Could not load logs: {e}")
@app.post('/api/user/logs/add', status_code=201)
@limiter.limit('100/minute')
def addlogs(request: Request, body: Log, user=Depends(getuser)):
userid=user["id"]
try:
insertlog={
'userid': userid,
'endpoint': body.endpoint[:2000],
'statuscode': body.statuscode,
'message': body.message[:1000]
}
response=supabase.table('logs').insert(insertlog).execute()
if not response.data:
raise HTTPException(status_code=400, detail='Database error')
if rediscient:
try:
rediscient.delete(f"logs:{userid}")
except Exception:
pass
return response.data[0]
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Could not save log: {e}")
@app.delete('/api/users/delete', status_code=200)
@limiter.limit('10/minute')
def deleteuser(request: Request, user=Depends(getuser)):
userid=user["id"]
try:
admin_supabase.auth.admin.delete_user(userid)
except Exception as e:
print(str(e))
raise HTTPException(status_code=500, detail="Server error")
@app.get('/runtime-config.js', include_in_schema=False)
async def runtime_config():
public_config = json.dumps({'backendUrl': BACKEND_URL})
return Response(
content=f'window.APIDESK_CONFIG = Object.freeze({public_config});',
media_type='application/javascript',
headers={'Cache-Control': 'no-store'},
)
@app.get('/root')
async def root(request: Request):
return {"status": "Server are running"}
@app.get('/{asset_path:path}', include_in_schema=False)
async def serve_frontend(asset_path: str = ''):
relative_path = asset_path or 'index.html'
requested_path = Path(relative_path)
allowed_extensions = {
'.html', '.js', '.css', '.png', '.jpg', '.jpeg', '.webp',
'.svg', '.ico', '.woff', '.woff2', '.webmanifest', '.xml', '.txt',
}
if any(part.startswith('.') for part in requested_path.parts):
raise HTTPException(status_code=404, detail='Not found')
if requested_path.parts and requested_path.parts[0].lower() == 'assets':
static_root = ASSET_ROOT
relative_asset = Path(*requested_path.parts[1:])
file_path = (static_root / relative_asset).resolve()
else:
static_root = WEBSITE_ROOT
file_path = (static_root / requested_path).resolve()
if (
static_root not in file_path.parents
or file_path.suffix.lower() not in allowed_extensions
or not file_path.is_file()
):
raise HTTPException(status_code=404, detail='Not found')
return FileResponse(file_path)