import time import requests import threading import logging from typing import Any, Callable, Dict, Optional import jwt from kinde_sdk.core.helpers import REQUEST_TIMEOUT class TokenManager: _instances = {} _lock = threading.Lock() # Add a lock for thread safety @classmethod def reset_instances(cls): """Reset all token manager instances - useful for testing""" with cls._lock: cls._instances = {} def __new__(cls, user_id, *args, **kwargs): """ Ensure only one instance per user. """ with cls._lock: if user_id not in cls._instances: cls._instances[user_id] = super(TokenManager, cls).__new__(cls) cls._instances[user_id].__init__(user_id, *args, **kwargs) return cls._instances[user_id] def __init__(self, user_id, client_id, client_secret, token_url): if hasattr(self, "initialized"): # Prevent re-initialization return self.user_id = user_id self.client_id = client_id self.client_secret = client_secret self.token_url = token_url self.tokens = {} # Store tokens (access/refresh) # Re-entrant: get_access_token() holds the lock while it refreshes, # and the refresh stores the new tokens through set_tokens() self.lock = threading.RLock() self.redirect_uri = None # Initialize the redirect_uri attribute self.force_api = False # Initialize force_api setting # Called after a refresh or a code exchange stores new tokens, so the # session that owns this token manager can persist them self.on_tokens_changed: Optional[Callable[[bool], None]] = None # Revision of these tokens in the stored session, kept by UserSession self.revision = None # True while these tokens have not been written to the stored session # (a save failed). Kept by UserSession, which retries the save. self.unsaved = False self.initialized = True def set_force_api(self, force_api: bool): """Set the force_api setting for this token manager.""" self.force_api = force_api def get_force_api(self) -> bool: """Get the force_api setting for this token manager.""" return self.force_api def set_tokens(self, token_data: Dict[str, Any], replace: bool = False): """ Store tokens with expiration. Args: token_data: The token endpoint response. replace: If True, drop every stored token first. Use this for the tokens of a new authorization (a login or a step-up), so that an older refresh or ID token is never kept alongside them. The default merges, which is right for a token refresh. """ with self.lock: # Build the new tokens first. A response that can't be read (an # expires_in that isn't a number, say) then raises before the # tokens already held are touched. new_tokens = { "access_token": token_data.get("access_token"), "expires_at": time.time() + token_data.get("expires_in", 3600), } # Store refresh token if available if "refresh_token" in token_data: new_tokens["refresh_token"] = token_data["refresh_token"] # Decode access token claims if "access_token" in token_data: try: new_tokens["access_token_claims"] = jwt.decode( token_data["access_token"], options={"verify_signature": False} ) except Exception as e: logging.error(f"Failed to decode access token claims: {str(e)}") new_tokens["access_token_claims"] = {} # Store ID token if available if "id_token" in token_data: new_tokens["id_token"] = token_data["id_token"] # Parse claims from ID token try: new_tokens["id_token_claims"] = jwt.decode( token_data["id_token"], options={"verify_signature": False} ) except Exception as e: logging.error(f"Failed to decode ID token claims: {str(e)}") new_tokens["id_token_claims"] = {} # Updated in place: the session keeps a reference to this dict if replace: self.tokens.clear() self.tokens.update(new_tokens) def set_redirect_uri(self, redirect_uri: str): """Set the redirect URI for token exchange.""" self.redirect_uri = redirect_uri async def exchange_code_for_token(self, code: str, code_verifier: Optional[str] = None): """ Exchange an authorization code for an access token. """ if not self.redirect_uri: raise ValueError("Redirect URI is not set") data = { "grant_type": "authorization_code", "code": code, "redirect_uri": self.redirect_uri, "client_id": self.client_id, } # Add client secret if available (for non-PKCE flow) if self.client_secret: data["client_secret"] = self.client_secret # Add code verifier for PKCE flow if code_verifier: data["code_verifier"] = code_verifier response = requests.post(self.token_url, data=data, timeout=REQUEST_TIMEOUT) response.raise_for_status() token_data = response.json() # A code exchange is a new authorization: its tokens replace the stored # ones, so an ID or refresh token from an earlier sign-in is never kept # alongside them. The lock is held until they are saved, so the tokens # and their revision change together. with self.lock: self.set_tokens(token_data, replace=True) self._notify_tokens_changed() return self.tokens["access_token"] def _notify_tokens_changed(self, refreshed: bool = False): """ Let the owning session persist tokens that changed outside a callback. Args: refreshed: True when the new tokens come from refreshing the previous ones, False when they come from a new authorization. """ callback = getattr(self, "on_tokens_changed", None) if callback is None: return try: callback(refreshed) except Exception as e: # Not raised: the new tokens are valid and are kept in memory, and # failing here would sign the user out of a working session. The # session saves them again the next time it is read (see # UserSession._sync_with_storage). logging.warning( "Failed to save updated tokens to the session, will retry: %s", type(e).__name__ ) def get_access_token(self): """ Get a valid access token. Refresh if expired. """ with self.lock: if not self.tokens or "access_token" not in self.tokens: raise ValueError("No access token available") # Check if token is expired # if time.time() >= self.tokens["expires_at"]: if time.time() >= self.tokens.get("expires_at", 0): # Try to refresh token if available if "refresh_token" in self.tokens: return self.refresh_access_token() else: raise ValueError("Access token expired and no refresh token available") return self.tokens["access_token"] def refresh_access_token(self, _retry: bool = True): """ Use the refresh token to get a new access token. """ # The lock is held from reading the refresh token until the result is # saved, so nothing else in this process changes the tokens in between with self.lock: if "refresh_token" not in self.tokens: raise ValueError("No refresh token available") data = { "grant_type": "refresh_token", "refresh_token": self.tokens["refresh_token"], "client_id": self.client_id, } # Add client secret if available if self.client_secret: data["client_secret"] = self.client_secret response = requests.post(self.token_url, data=data, timeout=REQUEST_TIMEOUT) response.raise_for_status() token_data = response.json() self.set_tokens(token_data) # Persist the refreshed tokens: another process serving this session # must not keep refreshing from the token that was just used. The # session may instead replace them with newer stored tokens. self._notify_tokens_changed(refreshed=True) # If the session adopted newer stored tokens instead, they are not # known to be valid: check them, and refresh once more from their # own refresh token if they have expired. adopted = self.tokens.get("access_token") != token_data.get("access_token") if adopted and time.time() >= self.tokens.get("expires_at", 0): if _retry and "refresh_token" in self.tokens: return self.refresh_access_token(_retry=False) raise ValueError("Access token expired and could not be refreshed") return self.tokens["access_token"] def get_id_token(self): """Get the ID token if available.""" return self.tokens.get("id_token") def get_claims(self, token_type: str = "access_token"): """Get the claims from the specified token type. Args: token_type (str): The type of token to get claims from. Valid values are "access_token" or "id_token". Returns: dict: The claims from the specified token, or empty dict if not available. """ # Validate token type valid_token_types = ["access_token", "id_token"] if token_type not in valid_token_types: logging.warning(f"Invalid token_type '{token_type}'. Valid types are: {valid_token_types}") return {} # Use f-string for safer string formatting claims_key = f"{token_type}_claims" claims = self.tokens.get(claims_key, {}) if not claims: logging.warning(f"No claims available for token type: {token_type}") return claims def get_claim(self, key: str, token_type: str = "access_token"): """Get a specific claim from the specified token type. Args: key (str): The claim key to retrieve token_type (str): The type of token to get the claim from. Valid values are "access_token" or "id_token". Returns: dict: Dictionary containing the claim name and value, or empty dict if not found. """ claims = self.get_claims(token_type) value = claims.get(key) return {"name": key, "value": value} def revoke_token(self): """ Revoke the current access token. """ if "access_token" not in self.tokens: return # No token to revoke revoke_url = f"{self.token_url.replace('/token', '/revoke')}" data = { "token": self.tokens["access_token"], "client_id": self.client_id, } # Add client secret if available if self.client_secret: data["client_secret"] = self.client_secret try: response = requests.post(revoke_url, data=data, timeout=REQUEST_TIMEOUT) response.raise_for_status() except Exception: pass # Best effort revocation self.tokens = {} # Clear stored tokens