This refactoring addresses the duplication of _get_token_manager logic across multiple authentication modules and provides a cleaner API for consumers who need direct access to the token manager.
- File:
kinde_sdk/auth/base_auth.py - Purpose: Eliminates code duplication by providing shared
_get_framework()and_get_token_manager()methods - Usage: All auth classes now inherit from
BaseAuth
The following classes now inherit from BaseAuth and have their duplicated methods removed:
FeatureFlags(infeature_flags.py)Claims(inclaims.py)Permissions(inpermissions.py)
- File:
kinde_sdk/auth/tokens.py - Purpose: Provides a clean API for consumers who need direct access to the token manager
- Exported as:
tokenssingleton instance
from kinde_sdk.auth import tokens
# Check authentication status
if tokens.is_authenticated():
print("User is authenticated")
# Get user ID
user_id = tokens.get_user_id()
# Get token manager for direct access
token_manager = tokens.get_token_manager()
if token_manager:
claims = token_manager.get_claims()
access_token = token_manager.tokens.get("access_token")Returns the token manager instance if available, None otherwise.
Returns the current user ID if available, None otherwise.
Returns True if the user is authenticated, False otherwise.
Returns a dictionary with token information:
{
"isAuthenticated": bool,
"userId": Optional[str],
"hasAccessToken": bool,
"hasIdToken": bool,
"hasRefreshToken": bool
}- Eliminated Code Duplication: The
_get_token_managerlogic is now centralized inBaseAuth - Cleaner API: Consumers can access token manager functionality through the
tokenswrapper - Better Separation of Concerns: Internal token manager access logic is abstracted away
- Consistent Interface: All auth modules now follow the same pattern
- Easier Testing: Shared logic can be tested once in
BaseAuth
Existing code using feature_flags, claims, or permissions will continue to work without changes. The refactoring is backward compatible.
If you need direct access to the token manager, use the new tokens wrapper:
# Old approach (exposed internal workings)
from kinde_sdk.auth import feature_flags
# Would need to understand internal structure
# New approach (clean API)
from kinde_sdk.auth import tokens
token_manager = tokens.get_token_manager()New tests have been added:
test_base_auth.py: Tests theBaseAuthclass functionalitytest_tokens.py: Tests the newtokenswrapper
Run tests to ensure everything works correctly:
pytest testv2/testv2_auth/test_base_auth.py
pytest testv2/testv2_auth/test_tokens.py