From 57ffa4534a8f4efca65200cb1d12f463aa5dedf7 Mon Sep 17 00:00:00 2001 From: Jason Maldonis Date: Wed, 16 Aug 2023 10:53:28 -0500 Subject: [PATCH 1/3] added regex check for API key --- deepgram/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/deepgram/__init__.py b/deepgram/__init__.py index dcdb5990..8d4d8b95 100644 --- a/deepgram/__init__.py +++ b/deepgram/__init__.py @@ -1,4 +1,5 @@ from typing import Union +import re from ._types import Options from .keys import Keys from .transcription import Transcription @@ -12,6 +13,11 @@ from .errors import DeepgramSetupError, DeepgramApiError +def validate_api_key(api_key: str) -> bool: + pattern = r"^[a-z0-9]{40}$" + re.fullmatch(pattern, api_key) is not None + + class Deepgram: def __init__(self, options: Union[str, Options]) -> None: if not isinstance(options, (str, dict)): @@ -23,6 +29,8 @@ def __init__(self, options: Union[str, Options]) -> None: if "api_key" not in options: raise DeepgramSetupError("API key is required") + if not validate_api_key(options["api_key"]): + raise DeepgramSetupError("Invalid API key") if "api_url" in options and options.get("api_url", None) is None: raise DeepgramSetupError("API URL must be valid or omitted") From a0cdc61269a345c4c23c52cdcd1faf293561d0a8 Mon Sep 17 00:00:00 2001 From: Jason Maldonis Date: Wed, 16 Aug 2023 10:58:10 -0500 Subject: [PATCH 2/3] bugfix! --- deepgram/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deepgram/__init__.py b/deepgram/__init__.py index 8d4d8b95..fcc1f4ac 100644 --- a/deepgram/__init__.py +++ b/deepgram/__init__.py @@ -13,9 +13,9 @@ from .errors import DeepgramSetupError, DeepgramApiError -def validate_api_key(api_key: str) -> bool: +def api_key_is_valid(api_key: str) -> bool: pattern = r"^[a-z0-9]{40}$" - re.fullmatch(pattern, api_key) is not None + return re.match(pattern, api_key) is not None class Deepgram: @@ -29,7 +29,7 @@ def __init__(self, options: Union[str, Options]) -> None: if "api_key" not in options: raise DeepgramSetupError("API key is required") - if not validate_api_key(options["api_key"]): + if not api_key_is_valid(options["api_key"]): raise DeepgramSetupError("Invalid API key") if "api_url" in options and options.get("api_url", None) is None: From 845d447090efb24189eb732010c6f6bc299b0caf Mon Sep 17 00:00:00 2001 From: Jason Maldonis Date: Wed, 16 Aug 2023 13:15:29 -0500 Subject: [PATCH 3/3] uuids only contain a-f letters --- deepgram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepgram/__init__.py b/deepgram/__init__.py index fcc1f4ac..d8658897 100644 --- a/deepgram/__init__.py +++ b/deepgram/__init__.py @@ -14,7 +14,7 @@ def api_key_is_valid(api_key: str) -> bool: - pattern = r"^[a-z0-9]{40}$" + pattern = r"^[a-f0-9]{40}$" return re.match(pattern, api_key) is not None