mirror of
https://github.com/sstent/FitTrack_GarminSync.git
synced 2026-01-25 16:41:41 +00:00
Adds the technical plan, data model, API contracts, and research for the persisted Garmin authentication feature.
3.0 KiB
3.0 KiB
Quickstart: Implementing Persisted Garmin Authentication
Date: 2025-12-22
This guide provides a high-level overview for developers implementing the Persist Garmin Authentication for Stateless Sync feature.
1. Database Model
- Action: Implement the
GarminAuthenticationStatemodel as defined indata-model.md. - File: Create a new model in
src/models/garmin_auth_state.py. - Details: Ensure the model includes
user_id,session_data,mfa_pending, andlast_validatedfields. Use SQLAlchemy and create a corresponding Alembic migration script.
2. API Endpoints
- Action: Implement the three new endpoints defined in
contracts/garmin_auth_session.json. - Files: Add a new router in
src/api/v1/auth.py. - Logic:
POST /api/v1/garmin/session/login:- Initialize
garth. - Call
garth.login(username, password). - If
garth.MFARequiredis raised, create/update theGarminAuthenticationStaterecord withmfa_pending=Trueand return{"status": "MFA_REQUIRED"}. - If successful,
dumps()the session, save it tosession_data, setmfa_pending=False, and return{"status": "SUCCESS"}.
- Initialize
POST /api/v1/garmin/session/mfa:- Load the pending
garthclient state. - Call
garth.enter_mfa(mfa_code). - On success,
dumps()the completed session and persist it to the database.
- Load the pending
GET /api/v1/garmin/session/status:- Query the
GarminAuthenticationStatefor the current user. - Perform a lightweight validation call with the loaded session (e.g.,
garth.connectapi.get_user_settings()). - Return the status (
VALID,MISSING,EXPIRED,MFA_PENDING).
- Query the
3. Update Background Sync Services
- Action: Modify the existing background sync services (
GarminActivityService,GarminHealthService) to use the persisted session. - Files: Update the relevant service files in
src/services/. - Pattern: Load-Use-Update:
- Load: At the start of the job, fetch the
session_datafrom the database. - Initialize: Call
garth.client.loads(session_data)to initialize the client. - Use: Perform the sync operations.
garthwill handle automatic token refreshes. - Update: Before the job finishes, check if the session was modified (refreshed). If so,
dumps()the new session and update thesession_datafield in the database. This is critical for maintaining a fresh session for the next job.
- Load: At the start of the job, fetch the
4. Error Handling
- Implement logic to catch
garthexceptions for invalid sessions. - If a session is invalid and cannot be refreshed, update the sync job status to
AUTH_EXPIREDand clear thesession_datafrom the database.
5. Testing
- Write unit tests for the new API endpoints and the Load-Use-Update pattern.
- Write integration tests that cover the full login (including MFA) and background sync flow.
- Mock the
garthlibrary to simulate different scenarios: successful login, MFA required, invalid session, and token refresh.