Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
1171 lines (982 loc) · 46.3 KB
/
Copy pathauth.py
File metadata and controls
1171 lines (982 loc) · 46.3 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import logging
import random
import re
import secrets
import smtplib
import string
from datetime import datetime, timedelta, timezone
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import Any, Dict, List, Optional
import jwt
from fastapi import (
APIRouter,
Depends,
Header,
HTTPException,
Query,
Request,
Response,
Cookie,
WebSocket,
status,
)
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer, OAuth2PasswordBearer
from pydantic import BaseModel
from starlette.websockets import WebSocketState
from werkzeug.security import check_password_hash, generate_password_hash
try:
from jwt.exceptions import ExpiredSignatureError, InvalidTokenError
except ImportError:
from jwt import ExpiredSignatureError, InvalidTokenError
import config
auth_router = APIRouter()
http_bearer = HTTPBearer(auto_error=False)
oauth2_scheme = http_bearer
logger = logging.getLogger(__name__)
# --- Email Sending Function (For Account Verification/Registration) ---
def send_verification_email(email: str, code: str):
"""
Sends a general verification email using GMAIL's SMTP.
"""
try:
# 1. Email Construction
msg = MIMEMultipart("alternative")
msg["Subject"] = "Email Verification for Your Account"
msg["From"] = f"PUNoted <{config.SMTP_USERNAME}>"
msg["To"] = email
html_content = f"""
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Email Verification</title>
<style>
body {{ font-family: sans-serif; line-height: 1.6; color: #333; }}
.container {{ max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9; }}
h1 {{ font-size: 24px; color: #0056b3; margin-bottom: 20px; }}
p {{ margin-bottom: 10px; }}
.code-box {{ background-color: #eee; padding: 15px; border-radius: 5px; font-size: 20px; font-weight: bold; text-align: center; margin: 20px 0; }}
.footer {{ font-size: 12px; color: #777; text-align: center; margin-top: 30px; }}
</style>
</head>
<body>
<div class="container">
<h1>Verify Your Email Address</h1>
<p>Thank you for registering with our service!</p>
<p>To complete your registration, please use the following verification code:</p>
<div class="code-box">
{code}
</div>
<p>This code will expire in {config.EMAIL_VERIFICATION_CODE_LIFESPAN_SECONDS / 60:.0f} minutes.</p>
<p>If you did not register for an account, please ignore this email.</p>
<div class="footer">
<p>© {datetime.now().year} PUNoted. All rights reserved.</p>
</div>
</div>
</body>
</html>
"""
msg.attach(MIMEText(html_content, "html"))
# 2. SMTP Connection and Sending
with smtplib.SMTP_SSL(config.SMTP_SERVER, 465, timeout=120) as server:
server.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
server.sendmail(config.SMTP_USERNAME, email, msg.as_string())
print(f"Verification email sent to {email} via Gmail SMTP.")
return True
except Exception as e:
print(f"Failed to send verification email to {email}: {e}")
return False
# -----------------------------------------------------------------
# --- Email Sending Function for PASSWORD RESET (Using Gmail SMTP) ---
def send_password_reset_email(email: str, code: str):
"""
Sends a password reset code email using GMAIL's SMTP.
Args:
email (str): The recipient's email address.
code (str): The password reset code to send.
"""
try:
# 1. Email Construction
msg = MIMEMultipart("alternative")
msg["Subject"] = "Password Reset Request - ACTION REQUIRED"
msg["From"] = f"PUNoted <{config.SMTP_USERNAME}>"
msg["To"] = email
html_content = f"""
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Password Reset</title>
<style>
body {{ font-family: sans-serif; line-height: 1.6; color: #333; }}
.container {{ max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9; }}
h1 {{ font-size: 24px; color: #b30000; margin-bottom: 20px; }}
p {{ margin-bottom: 10px; }}
/* Warning box style for reset code */
.code-box {{ background-color: #fce4e4; border: 1px solid #b30000; color: #b30000; padding: 15px; border-radius: 5px; font-size: 20px; font-weight: bold; text-align: center; margin: 20px 0; }}
.warning {{ color: #b30000; font-weight: bold; }}
.footer {{ font-size: 12px; color: #777; text-align: center; margin-top: 30px; }}
</style>
</head>
<body>
<div class="container">
<h1>Action Required: Password Reset</h1>
<p>We received a request to reset the password for your account associated with this email address.</p>
<p>To continue, please use the following **one-time code** on the password reset page:</p>
<div class="code-box">
{code}
</div>
<p class="warning">⚠️ IMPORTANT: If you did not request a password reset, please ignore this email immediately. Your password will remain unchanged.</p>
<p>This code will expire in {config.EMAIL_VERIFICATION_CODE_LIFESPAN_SECONDS / 60:.0f} minutes.</p>
<div class="footer">
<p>© {datetime.now().year} PUNoted. All rights reserved.</p>
</div>
</div>
</body>
</html>
"""
msg.attach(MIMEText(html_content, "html"))
# 2. SMTP Connection and Sending
with smtplib.SMTP_SSL(config.SMTP_SERVER, 465, timeout=120) as server:
server.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
server.sendmail(config.SMTP_USERNAME, email, msg.as_string())
print(f"Password reset email sent to {email} via Gmail SMTP.")
return True
except Exception as e:
print(f"Failed to send password reset email to {email}: {e}")
return False
# ==============================================================================
# JWT Token Generation and Validation Functions
# ==============================================================================
async def generate_auth_tokens(conn, user_id: str, request: Request, is_website: bool = False) -> tuple[str, str, int]:
"""
Generates both a short-lived Access Token and a long-lived Refresh Token.
Returns: (access_token, refresh_token, access_expires_at_timestamp)
"""
user_agent = request.headers.get("user-agent", "Unknown Browser")
x_forwarded_for = request.headers.get("X-Forwarded-For")
ip_address = x_forwarded_for.split(",")[0].strip() if x_forwarded_for else (request.client.host or "N/A")
now_aware = datetime.now(timezone.utc)
# Lifespans
access_lifespan = timedelta(days=7) if is_website else timedelta(days=365)
refresh_lifespan = timedelta(days=30)
access_expires_dt = now_aware + access_lifespan
refresh_expires_dt = now_aware + refresh_lifespan
# Generate Access Token
access_payload = {
"user_id": user_id,
"type": "access" if is_website else "extension_access",
"iat_id": secrets.token_hex(8),
"exp": access_expires_dt,
"iat": now_aware.timestamp()
}
access_token = jwt.encode(access_payload, config.JWT_SECRET_KEY, algorithm="HS256")
# Generate Refresh Token (Only for website)
refresh_token = ""
if is_website:
refresh_payload = {
"user_id": user_id,
"type": "refresh",
"iat_id": secrets.token_hex(8),
"exp": refresh_expires_dt,
"iat": now_aware.timestamp()
}
refresh_token = jwt.encode(refresh_payload, config.JWT_SECRET_KEY, algorithm="HS256")
try:
# Store the REFRESH token in the DB (or the access token for extensions)
db_token = refresh_token if is_website else access_token
db_token_type = "refresh" if is_website else "extension_access"
db_expires_naive = (refresh_expires_dt if is_website else access_expires_dt).replace(tzinfo=None)
await conn.execute(
"""
WITH inserted AS (
INSERT INTO user_tokens (userid, type, token, expiresat, user_agent, last_ip, iat_id, xata_updatedat)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW())
RETURNING userid, type
)
DELETE FROM user_tokens
WHERE id IN (
SELECT id FROM user_tokens
WHERE userid = (SELECT userid FROM inserted)
AND type = (SELECT type FROM inserted)
ORDER BY expiresat DESC
OFFSET 10
) OR (userid = $1 AND expiresat < NOW());
""",
user_id, db_token_type, db_token, db_expires_naive, user_agent, ip_address, access_payload["iat_id"]
)
return access_token, refresh_token, int(access_expires_dt.timestamp())
except Exception as e:
print(f"Database error during token generation: {e}")
return "", "", 0
async def validate_token(conn, token: str) -> tuple[str, int, None] | tuple[None, None, str]:
# 1. GROUP TOKEN: Stateful DB Lookup
if "grp_" in token:
try:
access_key, user_suffix = token.rsplit("-", 1)
except ValueError:
return None, None, "Invalid token format"
sql = """
SELECT m.user_id, m.status, m.can_read_data, g.id as group_id, u.username, u.accountid
FROM data_sharing_groups g
JOIN data_group_members m ON m.group_id = g.id
JOIN users u ON u.accountid = m.user_id
WHERE g.access_key = $1 AND m.personal_suffix = $2
"""
member = await conn.fetchrow(sql, access_key, user_suffix)
if not member or member["status"] != 'ACCEPTED':
return None, None, "Invalid Group Token"
# for now, we return the user ID and a dummy expiry
return str(member["accountid"]), 2147483647, None
# 2. PERSONAL TOKEN: Stateful DB Lookup
if token.startswith("ptk_"):
query = "SELECT user_id FROM user_api_tokens WHERE token_hash = $1 AND group_id IS NULL"
token_data = await conn.fetchrow(query, token)
return (str(token_data["user_id"]), 2147483647, None) if token_data else (None, None, "Invalid data token")
# 3. JWT TOKEN: Stateless + Statefull DB check
try:
decoded_payload = jwt.decode(token, config.JWT_SECRET_KEY, algorithms=["HS256"], leeway=5)
except (ExpiredSignatureError, InvalidTokenError) as e:
return None, None, f"Invalid token: {e}"
user_id_from_jwt = decoded_payload.get("user_id")
token_type = decoded_payload.get("type")
if not user_id_from_jwt or token_type not in ["access", "extension_access", "refresh"]:
return None, None, "Invalid token payload."
if token_type == "access":
return str(user_id_from_jwt), int(decoded_payload["exp"]), None
db_token_record = await conn.fetchrow(
"SELECT id, userid, expiresat FROM user_tokens WHERE token = $1 AND type = $2;",
token, token_type
)
if not db_token_record:
return None, None, "Token revoked or not found."
return str(db_token_record["userid"]), int(db_token_record["expiresat"].timestamp()), None
# ==============================================================================
# Email Verification Code Functions
# ==============================================================================
def generate_verification_code_str(length: int = 6) -> str:
"""Generates a random alphanumeric verification code."""
characters = string.ascii_uppercase + string.digits
return "".join(random.choice(characters) for i in range(length))
async def store_verification_code(conn, email: str, code: str, server_code: str) -> Optional[str]:
"""
Stores a verification code in the database with an expiration.
Args:
email (str): The email address the code is for.
code (str): The generated verification code.
Returns:
Optional[str]: The Xata record ID if successful, None otherwise.
"""
expires_at_dt = datetime.now() + timedelta(seconds=config.EMAIL_VERIFICATION_CODE_LIFESPAN_SECONDS)
try:
# First, delete any existing codes for this email to ensure only one is active.
delete_query = "DELETE FROM user_verification_codes WHERE email = $1;"
await conn.execute(delete_query, email)
print(f"Deleted old verification codes for {email}.")
# Now, insert the new verification code.
insert_query = """
INSERT INTO user_verification_codes (email, code, servercode, expiresat)
VALUES ($1, $2, $3, $4)
RETURNING id;
"""
record_id = await conn.fetch_rows(insert_query, email, code, server_code, expires_at_dt)
if record_id:
print(f"Stored verification code for {email}. Expires at: {expires_at_dt.isoformat()}")
return record_id[0]["id"]
else:
raise Exception("Insert operation returned no ID.")
except Exception as e:
print(f"Error storing verification code for {email}: {e}")
return None
async def get_verification_code_from_db(conn, email: str, code: str) -> tuple[str, None] | tuple[None, str]:
"""
Asynchronously retrieves and validates a verification code from the database.
Returns: A tuple containing the record ID of the code if valid and an error message if not.
"""
try:
query = """
SELECT id, expiresat FROM user_verification_codes
WHERE email = $1 AND code = $2;
"""
record = await conn.fetch_rows(query, email, code)
if not record:
return None, "Invalid email or verification code."
db_expires_at_dt = record[0]["expiresat"]
if datetime.now(db_expires_at_dt.tzinfo) > db_expires_at_dt:
# Code expired, delete it
await conn.execute("DELETE FROM user_verification_codes WHERE id = $1;", record[0]["id"])
return None, "Verification code expired."
return str(record[0]["id"]), None # Return the record ID if valid
except Exception as e:
print(f"Error retrieving/validating verification code for {email}: {e}")
return None, f"Server error during code validation: {e}"
async def delete_verification_code(conn, record_id: str) -> bool:
"""
Asynchronously deletes a verification code record from the database.
Returns:
bool: True if deletion was successful, False otherwise.
"""
try:
status_string = await conn.execute("DELETE FROM user_verification_codes WHERE id = $1;", int(record_id))
# Check if any rows were actually deleted. The status will be "DELETE 1" for one row.
if status_string.split()[-1] == "0":
print(f"Warning: Verification code record '{record_id}' not found for deletion.")
return False
print(f"Deleted verification code record '{record_id}'.")
return True
except Exception as e:
print(f"Error deleting verification code record '{record_id}': {e}")
return False
async def get_current_user_id(
request: Request,
bearer_creds: Optional[HTTPAuthorizationCredentials] = Depends(http_bearer),
) -> str:
token = bearer_creds.credentials if bearer_creds else None
if not token:
auth_header = request.headers.get("Authorization")
if auth_header and auth_header.startswith("Bearer "):
token = auth_header.split(" ", 1)[1].strip()
if not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated. Missing Bearer token.",
headers={"WWW-Authenticate": "Bearer"},
)
# Internal routes strictly forbid personal (ptk_) or group (grp_) tokens.
# Those tokens are only permitted on public external /v1/ endpoints.
if token.startswith("ptk_") or "grp_" in token:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="API data tokens (ptk_/grp_) are not allowed on internal endpoints. Use /v1/ endpoints or authenticate with user session credentials.",
)
pool = request.app.state.db.pool
async with pool.acquire() as conn:
user_id, expires_at, error = await validate_token(conn, token)
if error is not None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=error,
headers={"WWW-Authenticate": "Bearer"},
)
if user_id.startswith("rec_"):
user_id = await conn.fetch("SELECT accountid FROM users WHERE xata_id=$1", user_id)
user_id = str(user_id[0]["accountid"])
return user_id
async def get_current_user_id_ws(websocket: WebSocket) -> str:
"""
Manually extracts the token from the WebSocket query params and validates it.
This works when called directly inside the websocket endpoint.
"""
# 1. Manually get token string from QueryParams
# This fixes the issue where 'token' was a Query() object or None.
token = websocket.query_params.get("token")
if not token:
print("WS Auth Failed: Missing 'token' query parameter")
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="Missing token")
raise Exception("Missing token")
# 2. Access DB pool from App State
pool = websocket.app.state.db.pool
try:
async with pool.acquire() as conn:
# 3. Validate Token
user_id, expires_at, error = await validate_token(conn, token)
if error is not None:
print(f"WS Token Error: {error}")
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason=f"Auth failed: {error}")
raise Exception(error)
# 4. Handle 'rec_' ID conversion (if using Xata IDs) - This is old and needs to be fixed in DB and backend no more xata and rec_
if user_id.startswith("rec_"):
row = await conn.fetchrow("SELECT accountid FROM users WHERE xata_id=$1", user_id)
if row:
user_id = str(row["accountid"])
else:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="User mismatch")
raise Exception("User record not found")
return user_id
except Exception as e:
print(f"WS Auth Exception: {e}")
# Only close if not already closed
if websocket.client_state != WebSocketState.DISCONNECTED:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
raise
class RequireAuth:
def __init__(
self,
required_permissions: Optional[List[str]] = None,
is_single_user_endpoint: bool = False
):
self.required_permissions = required_permissions or []
self.is_single_user_endpoint = is_single_user_endpoint
async def __call__(
self,
request: Request,
token_header: Optional[str] = Header(None, alias="X-Data-Token"),
token_query: Optional[str] = Query(None, alias="token"),
token_cookie_tok: Optional[str] = Cookie(None, alias="refresh_token"),
) -> str:
raw_token = token_header or token_query or token_cookie_tok
if not raw_token:
auth_header = request.headers.get("Authorization") or request.headers.get("authorization")
if auth_header and auth_header.startswith("Bearer "):
raw_token = auth_header.split(" ", 1)[1].strip()
if not raw_token:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authentication required.")
final_token = raw_token.removeprefix("Bearer ")
pool = request.app.state.db.pool
q_params = request.query_params
async with pool.acquire() as conn:
# 1. CENTRALIZED VALIDATION
user_id, _, error = await validate_token(conn, final_token)
if error:
raise HTTPException(status_code=401, detail=error)
# 2. PERMISSION LOGIC
req_perm = self.required_permissions[0] if self.required_permissions else None
requested_users_str = q_params.get("usernames") or q_params.get("username")
requested_users = [u.strip() for u in requested_users_str.split(",") if u.strip()] if requested_users_str else None
me_username = await conn.fetchval("SELECT username FROM users WHERE accountid::text = $1", user_id)
if requested_users:
sql_targets = """
SELECT DISTINCT u.username
FROM users u
WHERE u.username = ANY($3::text[])
AND (
u.accountid::text = $1
OR EXISTS (
SELECT 1
FROM data_group_members gm_target
INNER JOIN data_group_members gm_requester ON gm_requester.group_id = gm_target.group_id
WHERE gm_target.user_id::text = u.accountid::text
AND gm_requester.user_id::text = $1
AND gm_requester.status = 'ACCEPTED'
AND gm_target.status = 'ACCEPTED'
AND gm_requester.can_read_data = TRUE
AND (
$2::text IS NULL
OR gm_target.granted_permissions::text LIKE '%' || $2::text || '%'
OR gm_target.granted_permissions::text LIKE '%all%'
)
)
)
"""
valid_rows = await conn.fetch(sql_targets, user_id, req_perm, requested_users)
valid_usernames = [r["username"] for r in valid_rows]
else:
if self.is_single_user_endpoint:
valid_usernames = [me_username] if me_username else []
else:
sql_targets = """
SELECT DISTINCT u.username
FROM users u
WHERE (
u.accountid::text = $1
OR EXISTS (
SELECT 1 FROM data_group_members gm_target
JOIN data_group_members gm_requester ON gm_requester.group_id = gm_target.group_id
WHERE gm_target.user_id::text = u.accountid::text
AND gm_requester.user_id::text = $1
AND gm_requester.status = 'ACCEPTED'
AND gm_requester.can_read_data = TRUE
AND gm_target.status = 'ACCEPTED'
AND (
$2::text IS NULL
OR gm_target.granted_permissions::text LIKE '%' || $2::text || '%'
OR gm_target.granted_permissions::text LIKE '%all%'
)
)
)
"""
valid_rows = await conn.fetch(sql_targets, user_id, req_perm)
valid_usernames = [r["username"] for r in valid_rows]
request.state.valid_target_users = valid_usernames
request.state.rate_limit_key = user_id
return user_id
# --- Optional Auth ---
class OptionalAuth(RequireAuth):
"""
Returns None if no token provided (Public Access).
Validates token if provided (Authenticated Access).
"""
async def __call__(
self,
request: Request,
token_header: Optional[str] = Header(None, alias="X-Data-Token"),
token_query: Optional[str] = Query(None, alias="token"),
token_cookie_tok: Optional[str] = Cookie(None, alias="refresh_token"),
) -> Optional[str]:
raw_token = token_header or token_query or token_cookie_tok
if not raw_token:
return None
return await super().__call__(
request,
token_header,
token_query,
token_cookie_tok,
)
# ==============================================================================
# Auth Routes
# ==============================================================================
security = HTTPBearer()
ALGORITHM = "HS256"
EXTENSION_TOKEN_EXPIRE_DAYS = 30
class SyncResponse(BaseModel):
success: bool
token: str
username: str
expires_at: int
@auth_router.post("/extension_sync")
async def extension_sync(
request: Request,
x_extension_client: str = Header(None),
auth: HTTPAuthorizationCredentials = Depends(security)
):
allowed_clients = {"PrunDataExtension", "PrunDataExtension-Chrome", "PrunDataExtension-Firefox"}
if x_extension_client not in allowed_clients:
raise HTTPException(status_code=403, detail="Unauthorized client")
async with request.app.state.db.pool.acquire() as conn:
# Validate the WEB token
user_id, _, error = await validate_token(conn, auth.credentials)
if error or not user_id:
raise HTTPException(status_code=401, detail=error or "Invalid web token")
user_record = await conn.fetchrow("SELECT username FROM users WHERE accountid = $1", user_id)
if not user_record:
raise HTTPException(status_code=404, detail="User not found")
# GENERATE EXTENSION TOKEN (is_website=False)
token, _, expires_at_ts = await generate_auth_tokens(conn, user_id, request, is_website=False)
return {
"success": True,
"token": token,
"username": user_record["username"],
"expires_at": expires_at_ts
}
@auth_router.post("/refresh")
async def refresh_access_token(request: Request, response: Response, refresh_token: Optional[str] = Cookie(None)):
print("\n--- DEBUG REFRESH ENDPOINT ---")
print(f"Incoming Cookies: {request.cookies}")
if not refresh_token:
print("FAILURE: The browser did not send the 'refresh_token' cookie!")
raise HTTPException(status_code=401, detail="Refresh token missing")
async with request.app.state.db.pool.acquire() as conn:
user_id, _, error = await validate_token(conn, refresh_token)
if error or not user_id:
print(f"FAILURE: Database validation failed. Error: {error}")
response.delete_cookie("refresh_token", httponly=True, secure=True, samesite="lax")
response.delete_cookie("refresh_token", httponly=True, secure=False, samesite="lax")
raise HTTPException(status_code=401, detail=f"Invalid token: {error}")
print("SUCCESS: Token is valid. Generating new tokens...")
# Fetch user metadata
import uuid
user_uuid = uuid.UUID(user_id) if isinstance(user_id, str) and len(user_id) == 36 else user_id
user_row = await conn.fetchrow("""
SELECT us.accountid, us.username,
COALESCE(ud.displayname, us.displayname) AS displayname,
cd.companyname, cd.companycode, c.name as corpname, us.is_synchronized
FROM users AS us
LEFT JOIN users_data AS ud ON ud.userid = us.userdataid
LEFT JOIN company_data AS cd ON cd.userdataid = ud.userid
LEFT JOIN corporations c ON c.id = ud.corporationid
WHERE us.accountid = $1;
""", user_uuid)
new_access, new_refresh, expires_at = await generate_auth_tokens(conn, user_id, request, is_website=True)
response.set_cookie(
key="refresh_token",
value=new_refresh,
httponly=True,
secure=True,
samesite="lax",
max_age=30 * 24 * 60 * 60,
)
return {
"success": True,
"token": new_access,
"expires_at": expires_at,
"username": user_row["username"] if user_row else None,
"displayName": user_row["displayname"] if user_row else None,
"companyName": user_row.get("companyname") if user_row else None,
"companyCode": user_row.get("companycode") if user_row else None,
"currentUserId": str(user_row["accountid"]) if user_row else None,
"corpName": user_row.get("corpname") if user_row else None,
"isSynchronized": user_row.get("is_synchronized") if user_row else None
}
@auth_router.post("/logout")
async def logout(response: Response, request: Request, refresh_token: str = Cookie(None)):
"""Clears the cookie and deletes it from the database."""
if refresh_token:
async with request.app.state.db.pool.acquire() as conn:
await conn.execute("DELETE FROM user_tokens WHERE token = $1", refresh_token)
response.delete_cookie("refresh_token", httponly=True, secure=True, samesite="lax")
response.delete_cookie("refresh_token", httponly=True, secure=False, samesite="lax")
return {"success": True, "message": "Logged out successfully"}
@auth_router.post("/register")
async def register(user_data: Dict[str, str], request: Request):
"""
Handles user registration.
Expects JSON with 'username', 'email', and 'password'.
Generates a verification code and sends it to the user's email.
Stores user with 'isverified' as False.
"""
conn = request.app.state.db
username = re.sub(r"[^-a-z0-9_]", "", user_data.get("username", "").lower())
email = user_data.get("email")
password = user_data.get("password")
if not all([username, email, password]):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username, email, and password are required.",
)
# Check if user with this username or email already exists
existing_users = await conn.fetch_rows(
"SELECT username, email FROM users WHERE username = $1 OR email = $2 LIMIT 1;",
username,
email,
)
for record in existing_users:
if record["username"] == username:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Username already exists.",
)
if record["email"] == email:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Email already registered.",
)
password_hash = generate_password_hash(password)
# Add user to the database with isverified=False
user_id = await conn.fetch_rows(
"INSERT INTO users (username, email, password_hash, isverified) VALUES ($1, $2, $3, $4) RETURNING accountid;",
username,
email,
password_hash,
False,
)
if not user_id:
raise Exception("Failed to get user ID after registration.")
# Generate and store verification code
verification_code = generate_verification_code_str()
server_code = generate_verification_code_str()
code_stored_id = await store_verification_code(conn, email, verification_code, server_code)
sent_email = send_verification_email(email, verification_code)
if not sent_email or not code_stored_id:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Registration successful, but failed to send verification email. Please contact support.",
)
print(f"User '{username}' registered with ID: {user_id[0]['accountid']}. Verification email sent to {email}.")
return {
"success": True,
"message": "Registration successful! Please check your email for a verification code.",
}
@auth_router.post("/verify_email")
async def verify_email(verification_data: Dict[str, str], request: Request):
"""
Handles email verification.
Expects JSON with 'email' and 'code'.
Validates the code and updates user's 'is_verified' status.
"""
conn = request.app.state.db
email = verification_data.get("email")
code = verification_data.get("code")
if not all([email, code]):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email and verification code are required.",
)
# Validate the code against the database
code_record_id, validation_error = await get_verification_code_from_db(conn, email, code)
if validation_error:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=validation_error)
# Find the user by email to update their verification status
user_record = await conn.fetch_rows("SELECT accountid, isverified FROM users WHERE email = $1;", email)
if not user_record:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found.")
if user_record[0].get("isverified"):
# User is already verified, delete the code and inform
await delete_verification_code(conn, code_record_id)
return {"success": True, "message": "Email already verified."}
# Update user's is_verified status to True
await conn.execute(
"UPDATE users SET isverified = TRUE WHERE accountid = $1;",
user_record[0]["accountid"],
)
default_web_settings = [
{
"page_context": "DASHBOARD",
"preferences": {"site_data": True, "ships_data": True, "flight_data": True}
},
{
"page_context": "CORP_PAGE",
"preferences": {"storage_data": True, "site_data": True, "production_data": True}
},
{
"page_context": "COOPERATION",
"preferences": {"site_data": True, "storage_data": True, "production_data": True}
}
]
settings_query = """
INSERT INTO user_web_settings (user_id, page_context, preferences, updated_at)
VALUES ($1, $2, $3::jsonb, NOW())
ON CONFLICT DO NOTHING
"""
for setting in default_web_settings:
await conn.execute(
settings_query,
user_record[0]["accountid"],
setting["page_context"],
json.dumps(setting["preferences"])
)
# Delete the used verification code
await delete_verification_code(conn, code_record_id)
print(f"Email '{email}' successfully verified.")
return {"success": True, "message": "Email successfully verified!"}
@auth_router.post("/forget_password")
async def forget_password(forgot_data: Dict[str, str], request: Request):
"""
Handles password reset requests.
Generates a verification code and sends it to the user's email.
"""
conn = request.app.state.db
email = forgot_data.get("email")
if not email:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Email is required.")
# Check if a user with this email exists
user = await conn.fetch_rows("SELECT accountid FROM users WHERE email = $1;", email)
if not user:
return {
"success": True,
"message": "If this email is registered, a verification code has been sent.",
}
# Generate and store a new verification code
verification_code = generate_verification_code_str()
server_code = generate_verification_code_str()
code_stored_id = await store_verification_code(conn, email, verification_code, server_code)
if not code_stored_id:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to store verification code.",
)
send_email = send_password_reset_email(email, verification_code)
if not send_email:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to send verification email. Please contact support.",
)
# Send the email with the code (placeholder, needs real server)
print(f"Password reset code '{verification_code}' sent to {email}.")
return {
"success": True,
"message": "A verification code has been sent to your email.",
}
@auth_router.post("/code_verification")
async def verify_forgot_password_code(verification_data: Dict[str, str], request: Request):
"""
Verifies the code sent for the password reset.
"""
conn = request.app.state.db
code = verification_data.get("code")
if not code:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Verification code is required.",
)
verification_record = await conn.fetch_rows("SELECT * FROM user_verification_codes WHERE code = $1;", code)
if not verification_record:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid verification code.",
)
# Check if the code has expired
if datetime.now() > verification_record[0]["expiresat"]:
await delete_verification_code(conn, str(verification_record[0]["id"]))
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Verification code has expired.",
)
return {"success": True, "message": "Code verified successfully."}
@auth_router.post("/forget_password_set_new_password")
async def set_new_password(password_data: Dict[str, str], request: Request):
"""
Sets a new password after a successful verification code check.
"""
conn = request.app.state.db
code = password_data.get("code")
new_password = password_data.get("newPassword")
if not code or not new_password:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Verification code and new password are required.",
)
# Validate the code one more time and get the email associated with it
verification_record = await conn.fetch_rows(
"SELECT id, email FROM user_verification_codes WHERE code = $1;", code
)
if not verification_record:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid verification code.",
)
email = verification_record[0]["email"]
password_hash = generate_password_hash(new_password)
await conn.execute(
"UPDATE users SET password_hash = $1 WHERE email = $2;",
password_hash,
email,
)
# Delete the used verification code
await delete_verification_code(conn, str(verification_record[0]["id"]))
return {"success": True, "message": "Password updated successfully."}
@auth_router.post("/login")
async def login(login_data: Dict[str, Any], request: Request, response: Response):
raw_identifier = login_data.get("username", "")
password = login_data.get("password")
is_web_raw = login_data.get("isWebsite")
is_web_bool = str(is_web_raw).lower() == "true"
# 1. Sanitize Username/Email
if "@" in raw_identifier:
username = raw_identifier.lower()
else:
username = re.sub(r"[^-a-z0-9_]", "", raw_identifier.lower())
if not username or not password:
raise HTTPException(status_code=400, detail="Username and password are required.")