From 6bec46be4e644cafbff1e5a337160ca4d8773fc6 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Sat, 6 Nov 2021 10:41:33 +0000 Subject: [PATCH] CAPI: Implement a one-shot override of the Access Token This allows you to force use of an expired token so as to test the code paths for that without waiting up to 4 hours for the current one to expire. NB: The Access Token is *only* stored in the headers of the `requests` object. --- EDMarketConnector.py | 11 +++++++++++ companion.py | 6 ++++++ config.py | 1 + 3 files changed, 18 insertions(+) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 3b969d8b..24453a47 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -151,6 +151,12 @@ if __name__ == '__main__': # noqa: C901 action='store_true' ) + parser.add_argument( + '--capi-use-debug-access-token', + help='Load a debug Access Token from disk (from config.app_dir_pathapp_dir_path / access_token.txt)', + action='store_true' + ) + parser.add_argument( '--eddn-url', help='Specify an alternate EDDN upload URL', @@ -170,6 +176,11 @@ if __name__ == '__main__': # noqa: C901 logger.info('Pretending CAPI is down') conf_module.capi_pretend_down = True + if args.capi_use_debug_access_token: + import config as conf_module + with open(conf_module.config.app_dir_path / 'access_token.txt', 'r') as at: + conf_module.capi_debug_access_token = at.readline().strip() + level_to_set: Optional[int] = None if args.trace or args.trace_on: level_to_set = logging.TRACE # type: ignore # it exists diff --git a/companion.py b/companion.py index 18b53402..2682fd8a 100644 --- a/companion.py +++ b/companion.py @@ -759,7 +759,13 @@ class Session(object): if conf_module.capi_pretend_down: raise ServerConnectionError(f'Pretending CAPI down: {capi_endpoint}') + if conf_module.capi_debug_access_token is not None: + self.requests_session.headers['Authorization'] = f'Bearer {conf_module.capi_debug_access_token}' # type: ignore # noqa: E501 + # This is one-shot + conf_module.capi_debug_access_token = None + r = self.requests_session.get(self.server + capi_endpoint, timeout=timeout) # type: ignore + logger.trace_if('capi.worker', '... got result...') r.raise_for_status() # Typically 403 "Forbidden" on token expiry # May also fail here if token expired since response is empty diff --git a/config.py b/config.py index d690da56..8660397f 100644 --- a/config.py +++ b/config.py @@ -46,6 +46,7 @@ debug_senders: List[str] = [] trace_on: List[str] = [] capi_pretend_down: bool = False +capi_debug_access_token: Optional[str] # This must be done here in order to avoid an import cycle with EDMCLogging. # Other code should use EDMCLogging.get_main_logger if os.getenv("EDMC_NO_UI"):