-
Notifications
You must be signed in to change notification settings - Fork 0
Created a softball scraper, edited unique sport constraint #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ __pycache__/ | |
| .env | ||
| .envrc | ||
| .DS_Store | ||
| ca-certificate.crt | ||
| ca-certificate.crt | ||
| firebase-service-account-key.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,27 @@ | |
| from src.utils.team_loader import TeamLoader | ||
| from src.database import db | ||
|
|
||
| import os | ||
| import firebase_admin | ||
| from firebase_admin import credentials, auth | ||
|
|
||
| SERVICE_ACCOUNT_PATH = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") | ||
|
|
||
|
|
||
| def initialize_firebase(): | ||
| if not firebase_admin._apps: | ||
| if not SERVICE_ACCOUNT_PATH: | ||
| raise ValueError( | ||
| "GOOGLE_APPLICATION_CREDENTIALS is not set. Set it to your firebase-service-account-key.json path." | ||
| ) | ||
| cred = credentials.Certificate(SERVICE_ACCOUNT_PATH) | ||
| firebase_admin.initialize_app(cred) | ||
| logging.info("Firebase app initialized.") | ||
| return firebase_admin.get_app() | ||
|
|
||
|
|
||
| initialize_firebase() | ||
|
Comment on lines
+26
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check if .env_template includes the Firebase credential variable
echo "=== Checking .env_template ==="
cat .env_template 2>/dev/null || echo ".env_template not found"
echo ""
echo "=== Checking docker-compose.yml for volume mounts ==="
cat docker-compose.yml 2>/dev/null | grep -A5 "volumes:" || echo "No volumes section found"Repository: cuappdev/score-backend Length of output: 498 🏁 Script executed: # Check file size and read app.py to verify all claims
wc -l app.py
echo "=== Full app.py contents ==="
cat -n app.pyRepository: cuappdev/score-backend Length of output: 8592 🏁 Script executed: # Check if Dockerfile exists and examine it
if [ -f Dockerfile ]; then
echo "=== Dockerfile found ==="
cat -n Dockerfile
else
echo "Dockerfile not found"
fiRepository: cuappdev/score-backend Length of output: 391 Deployment configuration incomplete for Firebase. The Firebase initialization requires
This will cause startup failures in containerized deployments. 🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these set in both dev and prod servers @claiireyu ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| app = Flask(__name__) | ||
|
|
||
| # CORS: allow frontend (different origin) to call this API | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ Flask-APScheduler | |
| python-dotenv | ||
| pytz | ||
| gunicorn | ||
| firebase-admin | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,26 @@ | ||||||||||||||||||||||||||||||
| from graphql import GraphQLError | ||||||||||||||||||||||||||||||
| from graphene import Mutation, String, Field | ||||||||||||||||||||||||||||||
| from graphene import Mutation, String | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from firebase_admin import auth as firebase_auth | ||||||||||||||||||||||||||||||
| from flask_jwt_extended import create_access_token, create_refresh_token | ||||||||||||||||||||||||||||||
| from src.database import db | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class LoginUser(Mutation): | ||||||||||||||||||||||||||||||
| class Arguments: | ||||||||||||||||||||||||||||||
| net_id = String(required=True, description="User's net ID (e.g. Cornell netid).") | ||||||||||||||||||||||||||||||
| id_token = String(required=True, description="Firebase ID token from the client.") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| access_token = String() | ||||||||||||||||||||||||||||||
| refresh_token = String() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def mutate(self, info, net_id): | ||||||||||||||||||||||||||||||
| user = db["users"].find_one({"net_id": net_id}) | ||||||||||||||||||||||||||||||
| def mutate(self, info, id_token): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
| decoded = firebase_auth.verify_id_token(id_token) | ||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||
| raise GraphQLError("Invalid or expired token.") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| firebase_uid = decoded["uid"] | ||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception handling for token verification. Two issues flagged by static analysis:
Additionally, accessing Proposed fix def mutate(self, info, id_token):
try:
decoded = firebase_auth.verify_id_token(id_token)
- except Exception:
- raise GraphQLError("Invalid or expired token.")
-
- firebase_uid = decoded["uid"]
+ firebase_uid = decoded["uid"]
+ except (ValueError, KeyError) as err:
+ raise GraphQLError("Invalid or expired token.") from None
+ except Exception as err:
+ raise GraphQLError("Invalid or expired token.") from None
+
user = db["users"].find_one({"firebase_uid": firebase_uid})Alternatively, catch the specific 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.6)[warning] 19-19: Do not catch blind exception: (BLE001) [warning] 20-20: Within an (B904) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| user = db["users"].find_one({"firebase_uid": firebase_uid}) | ||||||||||||||||||||||||||||||
| if not user: | ||||||||||||||||||||||||||||||
| raise GraphQLError("User not found.") | ||||||||||||||||||||||||||||||
| identity = str(user["_id"]) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,38 @@ | ||
| from graphql import GraphQLError | ||
| from graphene import Mutation, String | ||
|
|
||
| from firebase_admin import auth as firebase_auth | ||
| from flask_jwt_extended import create_access_token, create_refresh_token | ||
| from src.database import db | ||
|
|
||
|
|
||
| class SignupUser(Mutation): | ||
| class Arguments: | ||
| net_id = String(required=True, description="User's net ID (e.g. Cornell netid).") | ||
| id_token = String(required=True, description="Firebase ID token from the client.") | ||
| name = String(required=False, description="Display name.") | ||
| email = String(required=False, description="Email address.") | ||
| email = String(required=False, description="Email (overrides token email if provided).") | ||
|
|
||
| access_token = String() | ||
| refresh_token = String() | ||
|
|
||
| def mutate(self, info, net_id, name=None, email=None): | ||
| if db["users"].find_one({"net_id": net_id}): | ||
| raise GraphQLError("Net ID already exists.") | ||
| def mutate(self, info, id_token, name=None, email=None): | ||
| try: | ||
| decoded = firebase_auth.verify_id_token(id_token) | ||
| except Exception: | ||
| raise GraphQLError("Invalid or expired token.") | ||
|
|
||
| firebase_uid = decoded["uid"] | ||
|
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception handling for token verification. Same issues as in
Proposed fix def mutate(self, info, id_token, name=None, email=None):
try:
decoded = firebase_auth.verify_id_token(id_token)
- except Exception:
- raise GraphQLError("Invalid or expired token.")
-
- firebase_uid = decoded["uid"]
+ firebase_uid = decoded["uid"]
+ except (ValueError, KeyError) as err:
+ raise GraphQLError("Invalid or expired token.") from None
+ except Exception as err:
+ raise GraphQLError("Invalid or expired token.") from None
+
if db["users"].find_one({"firebase_uid": firebase_uid}):🧰 Tools🪛 Ruff (0.15.6)[warning] 21-21: Do not catch blind exception: (BLE001) [warning] 22-22: Within an (B904) 🤖 Prompt for AI Agents |
||
| if db["users"].find_one({"firebase_uid": firebase_uid}): | ||
| raise GraphQLError("User already exists.") | ||
|
|
||
| email = email or decoded.get("email") | ||
| user_doc = { | ||
| "net_id": net_id, | ||
| "firebase_uid": firebase_uid, | ||
| "email": email, | ||
| "favorite_game_ids": [], | ||
| } | ||
| if name is not None: | ||
| user_doc["name"] = name | ||
| if email is not None: | ||
| user_doc["email"] = email | ||
| result = db["users"].insert_one(user_doc) | ||
|
Comment on lines
+25
to
36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential race condition in user creation. The check-then-insert pattern ( Consider using a unique index on Proposed fix using unique index + exception handlingFirst, ensure a unique index exists on db["users"].create_index("firebase_uid", unique=True)Then handle the duplicate key error: - if db["users"].find_one({"firebase_uid": firebase_uid}):
- raise GraphQLError("User already exists.")
-
email = email or decoded.get("email")
user_doc = {
"firebase_uid": firebase_uid,
"email": email,
"favorite_game_ids": [],
}
if name is not None:
user_doc["name"] = name
- result = db["users"].insert_one(user_doc)
+ try:
+ result = db["users"].insert_one(user_doc)
+ except pymongo.errors.DuplicateKeyError:
+ raise GraphQLError("User already exists.") from None
identity = str(result.inserted_id)🤖 Prompt for AI Agents |
||
| identity = str(result.inserted_id) | ||
| return SignupUser( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.