murtaza-2007 commited on
Commit ·
c00c685
1
Parent(s): 408707f
Allow multiple email domains for sign-in (altafhvali.com, gmail.com)
Browse files- RAG_Products/api.py +9 -5
- RAG_Products/config.py +7 -3
- RAG_Products/static/config.js +3 -3
- RAG_Products/static/index.html +4 -3
RAG_Products/api.py
CHANGED
|
@@ -18,7 +18,7 @@ from fastapi.responses import FileResponse
|
|
| 18 |
from fastapi.staticfiles import StaticFiles
|
| 19 |
from pydantic import BaseModel, Field
|
| 20 |
|
| 21 |
-
from RAG_Products.config import SIMILAR_K, OPENROUTER_API_KEY, ALLOWED_ORIGINS,
|
| 22 |
from RAG_Products.graph import get_graph
|
| 23 |
from RAG_Products.similar import find_similar, doc_to_dict
|
| 24 |
from RAG_Products.models import llm_groq
|
|
@@ -120,14 +120,18 @@ def _resolve_user(authorization, fallback):
|
|
| 120 |
"""Prefer the verified Firebase uid from the Bearer token; else the
|
| 121 |
client-supplied id (used in local/no-auth mode).
|
| 122 |
|
| 123 |
-
Enforces
|
| 124 |
-
|
|
|
|
| 125 |
"""
|
| 126 |
if authorization and authorization.lower().startswith("bearer "):
|
| 127 |
uid, email = storage.verify_token(authorization[7:].strip())
|
| 128 |
if uid:
|
| 129 |
-
if
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
return uid
|
| 132 |
return fallback or "default"
|
| 133 |
|
|
|
|
| 18 |
from fastapi.staticfiles import StaticFiles
|
| 19 |
from pydantic import BaseModel, Field
|
| 20 |
|
| 21 |
+
from RAG_Products.config import SIMILAR_K, OPENROUTER_API_KEY, ALLOWED_ORIGINS, ALLOWED_EMAIL_DOMAINS
|
| 22 |
from RAG_Products.graph import get_graph
|
| 23 |
from RAG_Products.similar import find_similar, doc_to_dict
|
| 24 |
from RAG_Products.models import llm_groq
|
|
|
|
| 120 |
"""Prefer the verified Firebase uid from the Bearer token; else the
|
| 121 |
client-supplied id (used in local/no-auth mode).
|
| 122 |
|
| 123 |
+
Enforces ALLOWED_EMAIL_DOMAINS: a signed-in user whose email isn't on one
|
| 124 |
+
of those domains is rejected outright (403), never silently downgraded to
|
| 125 |
+
anonymous.
|
| 126 |
"""
|
| 127 |
if authorization and authorization.lower().startswith("bearer "):
|
| 128 |
uid, email = storage.verify_token(authorization[7:].strip())
|
| 129 |
if uid:
|
| 130 |
+
if ALLOWED_EMAIL_DOMAINS and not any(
|
| 131 |
+
(email or "").lower().endswith("@" + d) for d in ALLOWED_EMAIL_DOMAINS
|
| 132 |
+
):
|
| 133 |
+
allowed = ", ".join("@" + d for d in ALLOWED_EMAIL_DOMAINS)
|
| 134 |
+
raise HTTPException(403, f"Sign-in is restricted to {allowed} accounts.")
|
| 135 |
return uid
|
| 136 |
return fallback or "default"
|
| 137 |
|
RAG_Products/config.py
CHANGED
|
@@ -57,9 +57,13 @@ FIREBASE_CREDENTIALS_JSON = os.getenv("FIREBASE_CREDENTIALS_JSON")
|
|
| 57 |
FIREBASE_PROJECT_ID = os.getenv("FIREBASE_PROJECT_ID")
|
| 58 |
CHATS_LOCAL_PATH = BASE_DIR / "Data" / "chats.json" # fallback store
|
| 59 |
|
| 60 |
-
# Only emails on
|
| 61 |
-
# email/password).
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# CORS: comma-separated origins allowed to call the API (the Vercel frontend).
|
| 65 |
# "*" is fine since there are no cookies; restrict to your domain for safety.
|
|
|
|
| 57 |
FIREBASE_PROJECT_ID = os.getenv("FIREBASE_PROJECT_ID")
|
| 58 |
CHATS_LOCAL_PATH = BASE_DIR / "Data" / "chats.json" # fallback store
|
| 59 |
|
| 60 |
+
# Only emails on these domains may sign in (any Firebase provider — Google or
|
| 61 |
+
# email/password). Comma-separated; empty string disables the restriction.
|
| 62 |
+
ALLOWED_EMAIL_DOMAINS = [
|
| 63 |
+
d.strip().lower() for d in
|
| 64 |
+
os.getenv("ALLOWED_EMAIL_DOMAINS", "altafhvali.com,gmail.com").split(",")
|
| 65 |
+
if d.strip()
|
| 66 |
+
]
|
| 67 |
|
| 68 |
# CORS: comma-separated origins allowed to call the API (the Vercel frontend).
|
| 69 |
# "*" is fine since there are no cookies; restrict to your domain for safety.
|
RAG_Products/static/config.js
CHANGED
|
@@ -14,6 +14,6 @@ window.FIREBASE_CONFIG = {
|
|
| 14 |
appId: "1:201860800907:web:26114586af1b4a42704f8e"
|
| 15 |
};
|
| 16 |
|
| 17 |
-
// Only
|
| 18 |
-
// backend — this is just a fast client-side check, the backend enforces it).
|
| 19 |
-
window.
|
|
|
|
| 14 |
appId: "1:201860800907:web:26114586af1b4a42704f8e"
|
| 15 |
};
|
| 16 |
|
| 17 |
+
// Only these email domains may sign in (must match ALLOWED_EMAIL_DOMAINS on
|
| 18 |
+
// the backend — this is just a fast client-side check, the backend enforces it).
|
| 19 |
+
window.ALLOWED_EMAIL_DOMAINS = ["altafhvali.com", "gmail.com"];
|
RAG_Products/static/index.html
CHANGED
|
@@ -583,10 +583,11 @@
|
|
| 583 |
fbAuth = firebase.auth();
|
| 584 |
gateSign(); renderAuth(null);
|
| 585 |
fbAuth.onAuthStateChanged(async (user) => {
|
| 586 |
-
const
|
| 587 |
-
|
|
|
|
| 588 |
await fbAuth.signOut();
|
| 589 |
-
gateSign(`Sign-in is restricted to
|
| 590 |
return;
|
| 591 |
}
|
| 592 |
authUser = user;
|
|
|
|
| 583 |
fbAuth = firebase.auth();
|
| 584 |
gateSign(); renderAuth(null);
|
| 585 |
fbAuth.onAuthStateChanged(async (user) => {
|
| 586 |
+
const domains = window.ALLOWED_EMAIL_DOMAINS || [];
|
| 587 |
+
const email = (user && user.email || '').toLowerCase();
|
| 588 |
+
if (user && domains.length && !domains.some(d => email.endsWith('@' + d.toLowerCase()))) {
|
| 589 |
await fbAuth.signOut();
|
| 590 |
+
gateSign(`Sign-in is restricted to ${domains.map(d => '@' + d).join(', ')} accounts.`);
|
| 591 |
return;
|
| 592 |
}
|
| 593 |
authUser = user;
|