fix: reload approved OAuth API keys from DB on startup#104
Open
piyushdotcomm wants to merge 1 commit intosugarlabs:mainfrom
Open
fix: reload approved OAuth API keys from DB on startup#104piyushdotcomm wants to merge 1 commit intosugarlabs:mainfrom
piyushdotcomm wants to merge 1 commit intosugarlabs:mainfrom
Conversation
After a server restart, settings.API_KEYS was only populated from the API_KEYS environment variable via sync_env_keys_to_db(). OAuth-approved keys stored in the database were not reloaded, causing all OAuth users to receive 401 Invalid API key on every restart. This fix queries all approved and active API keys from the database at startup and loads them into settings.API_KEYS, ensuring OAuth users retain access across restarts and redeployments. Fixes: sugarlabs#102
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After a server restart,
settings.API_KEYSis only repopulated from theAPI_KEYSenvironment variable via sync_env_keys_to_db(). OAuth-approvedkeys stored in the database are never reloaded into memory.
Since verify_api_key() validates all requests against
settings.API_KEYS(in-memory only), every OAuth user receives a
401 Invalid API keyerrorafter any server restart or redeployment — even if their key is valid in
the database.
Fix
In startup_event() in main.py, after calling sync_env_keys_to_db(db),
query all approved and active API keys from the database and load them into
settings.API_KEYS:This ensures all OAuth-issued keys are always available in memory after
startup, regardless of whether the server was restarted.
Fixes #102