Skip to content

Commit 3f5130a

Browse files
committed
feat: Refactor authenticate_user to accept only username and password, return a dictionary containing the session token and user email, and simplify the main login endpoint.
Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 0d78417 commit 3f5130a

3 files changed

Lines changed: 26 additions & 47 deletions

File tree

backend/src/dna/prodtrack_providers/prodtrack_provider_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def get_versions_for_playlist(self, playlist_id: int) -> list["Version"]:
8989
raise NotImplementedError("Subclasses must implement this method.")
9090

9191
@staticmethod
92-
def authenticate_user(url: str, login: str, password: str) -> str:
93-
"""Authenticate a user and return a session token."""
92+
def authenticate_user(username: str, password: str) -> dict[str, Any]:
93+
"""Authenticate a user and return a session token and user info."""
9494
raise NotImplementedError("Subclasses must implement this method.")
9595

9696

backend/src/dna/prodtrack_providers/shotgrid.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,37 +578,38 @@ def get_versions_for_playlist(self, playlist_id: int) -> list[Version]:
578578

579579

580580
@staticmethod
581-
def authenticate_user(url: str, login: str, password: str) -> str:
582-
"""Authenticate a user with ShotGrid and return a session token.
581+
def authenticate_user(username: str, password: str) -> dict[str, Any]:
582+
"""Authenticate a user with ShotGrid and return session token and user info.
583583
584584
Args:
585-
url: ShotGrid server URL
586-
login: User login/username
585+
username: User login/username
587586
password: User password
588587
589588
Returns:
590-
Session token string
589+
Dictionary containing 'token' and 'email'
591590
592591
Raises:
593592
ValueError: If authentication fails
594593
"""
594+
url = os.getenv("SHOTGRID_URL")
595+
if not url:
596+
raise ValueError("SHOTGRID_URL not configured")
597+
595598
try:
596-
# Shotgun.authenticate_human_user returns the user object, but we need the session_token.
597-
# However, the standard way to get a token is to just create a connection which validates creds.
598-
# But wait, Shotgun API structure specifically for auth:
599-
# We can use the simple authentication helper or instantiate to get token.
600-
# Actually, standard shotgun_api3 doesn't easily expose 'authenticate_human_user' to get a token string directly
601-
# without internals.
602-
# Let's instantiate a connection to verify and get session_token if available or standard auth flow.
603-
# The pattern usually is: sg = Shotgun(url, login=login, password=password) then sg.get_session_token().
604-
605-
# Note: shotgun_api3 v3.3.0+ supports `login` and `password` in constructor for script-based auth,
606-
# but for human user relying on session token:
607-
608-
sg = Shotgun(url, login=login, password=password)
609-
# This establishes connection. Now implementation detail: how to get the token?
610-
# The 'get_session_token()' method provides it.
611-
return sg.get_session_token()
599+
# Initialize connection to verify credentials and get token
600+
sg = Shotgun(url, login=username, password=password)
601+
token = sg.get_session_token()
602+
603+
# Create a provider instance with the new token to fetch user details
604+
# This reuses the existing entity mapping logic
605+
provider = ShotgridProvider(url=url, session_token=token)
606+
user = provider.get_user_by_login(username)
607+
608+
if not user.email:
609+
raise ValueError("User has no email address configured")
610+
611+
return {"token": token, "email": user.email}
612+
612613
except Exception as e:
613614
raise ValueError(f"Authentication failed: {str(e)}")
614615

backend/src/main.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""FastAPI application entry point."""
22

3-
from functools import lru_cache
3+
44
from typing import Annotated, cast
55

66
from fastapi import Depends, FastAPI, HTTPException, Header
@@ -173,29 +173,7 @@ def get_prodtrack_provider_dep(
173173
async def login(request: LoginRequest):
174174
"""Login to ShotGrid."""
175175
try:
176-
# We need a provider instance to access the static method if we want to keep it clean,
177-
# or just import the class. We imported ShotgridProvider above.
178-
# But we need the URL from the environment or default provider.
179-
# Let's instantiate a default provider to get config, or just use the class method
180-
# and assume env vars are set for URL if not passed?
181-
# The static method requires URL.
182-
183-
# Helper to get base URL
184-
import os
185-
url = os.getenv("SHOTGRID_URL")
186-
if not url:
187-
raise HTTPException(status_code=500, detail="SHOTGRID_URL not configured")
188-
189-
token = ShotgridProvider.authenticate_user(url, request.username, request.password)
190-
191-
# Create a provider with this token to fetch the user details (email)
192-
provider = ShotgridProvider(url=url, session_token=token)
193-
user = provider.get_user_by_login(request.username)
194-
195-
if not user.email:
196-
raise HTTPException(status_code=400, detail="User has no email address configured")
197-
198-
return {"token": token, "email": user.email}
176+
return ShotgridProvider.authenticate_user(request.username, request.password)
199177
except ValueError as e:
200178
raise HTTPException(status_code=401, detail=str(e))
201179

0 commit comments

Comments
 (0)