|
| 1 | +"""Instrumentation for Kinde SDK authentication. |
| 2 | +
|
| 3 | +Patches is_authenticated() to always return True in REPLAY mode, |
| 4 | +allowing tests to bypass authentication checks during replay. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import logging |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from ..base import InstrumentationBase |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class KindeInstrumentation(InstrumentationBase): |
| 18 | + """Instrumentation for the Kinde SDK authentication library. |
| 19 | +
|
| 20 | + Patches OAuth.is_authenticated() to: |
| 21 | + - Return True in REPLAY mode (bypass authentication) |
| 22 | + - Call the original method in RECORD and DISABLED modes |
| 23 | +
|
| 24 | + Since SmartOAuth and AsyncOAuth delegate to OAuth.is_authenticated(), |
| 25 | + patching OAuth covers all authentication entry points. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, enabled: bool = True) -> None: |
| 29 | + super().__init__( |
| 30 | + name="KindeInstrumentation", |
| 31 | + module_name="kinde_sdk.auth.oauth", |
| 32 | + supported_versions="*", |
| 33 | + enabled=enabled, |
| 34 | + ) |
| 35 | + |
| 36 | + def patch(self, module: Any) -> None: |
| 37 | + """Patch the kinde_sdk.auth.oauth module. |
| 38 | +
|
| 39 | + Patches OAuth.is_authenticated() to return True in REPLAY mode. |
| 40 | + """ |
| 41 | + if not hasattr(module, "OAuth"): |
| 42 | + logger.warning("kinde_sdk.auth.oauth.OAuth not found, skipping instrumentation") |
| 43 | + return |
| 44 | + |
| 45 | + original_is_authenticated = module.OAuth.is_authenticated |
| 46 | + |
| 47 | + def patched_is_authenticated(oauth_self) -> bool: |
| 48 | + """Patched is_authenticated method. |
| 49 | +
|
| 50 | + Args: |
| 51 | + oauth_self: OAuth instance |
| 52 | +
|
| 53 | + Returns: |
| 54 | + True in REPLAY mode, otherwise delegates to original method |
| 55 | + """ |
| 56 | + # Lazy imports to avoid circular dependency |
| 57 | + from ...core.drift_sdk import TuskDrift |
| 58 | + from ...core.types import TuskDriftMode |
| 59 | + |
| 60 | + sdk = TuskDrift.get_instance() |
| 61 | + |
| 62 | + # In REPLAY mode, always return True to bypass authentication |
| 63 | + if sdk.mode == TuskDriftMode.REPLAY: |
| 64 | + logger.debug("[KindeInstrumentation] REPLAY mode: returning True for is_authenticated") |
| 65 | + return True |
| 66 | + |
| 67 | + # In RECORD or DISABLED mode, call the original method |
| 68 | + return original_is_authenticated(oauth_self) |
| 69 | + |
| 70 | + module.OAuth.is_authenticated = patched_is_authenticated |
| 71 | + logger.info("kinde_sdk.auth.oauth.OAuth.is_authenticated instrumented") |
0 commit comments