-
Notifications
You must be signed in to change notification settings - Fork 853
fix: Improve type annotations in SignatureVerifier
#1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,13 @@ | |
| import hashlib | ||
| import hmac | ||
| from time import time | ||
| from typing import Dict, Optional, Union | ||
| from typing import Dict, Optional, Union, TYPE_CHECKING | ||
|
|
||
| # Fallback to Dict for Python 3.7/3.8 compatibility (safe to remove once these versions are no longer supported) | ||
| if TYPE_CHECKING: | ||
| from collections.abc import Mapping | ||
| else: | ||
| Mapping = Dict | ||
|
|
||
|
|
||
| class Clock: | ||
|
|
@@ -26,23 +32,23 @@ def __init__(self, signing_secret: str, clock: Clock = Clock()): | |
| def is_valid_request( | ||
| self, | ||
| body: Union[str, bytes], | ||
| headers: Dict[str, str], | ||
| headers: Mapping[str, str], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For python 3.7 and 3.8 this slack_sdk/signature/__init__.py:30: in SignatureVerifier
headers: Mapping[str, str],
E TypeError: 'ABCMeta' object is not subscriptableImporting from typing import TYPE_CHECKING, Dict, Optional, Union
if TYPE_CHECKING:
from collections.abc import Mapping
else:
Mapping = Dict
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we go this route, it might be a good idea to also include a comment about how this is for python 3.7 and 3.8 backwards compatibility
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh, old Pythons strike again! But I like your proposed solution - Will push it with a comment shortly. |
||
| ) -> bool: | ||
| """Verifies if the given signature is valid""" | ||
| if headers is None: | ||
| return False | ||
| normalized_headers = {k.lower(): v for k, v in headers.items()} | ||
| return self.is_valid( | ||
| body=body, | ||
| timestamp=normalized_headers.get("x-slack-request-timestamp", None), # type: ignore[arg-type] | ||
| signature=normalized_headers.get("x-slack-signature", None), # type: ignore[arg-type] | ||
| timestamp=normalized_headers.get("x-slack-request-timestamp", None), | ||
| signature=normalized_headers.get("x-slack-signature", None), | ||
| ) | ||
|
|
||
| def is_valid( | ||
| self, | ||
| body: Union[str, bytes], | ||
| timestamp: str, | ||
| signature: str, | ||
| timestamp: Optional[str], | ||
| signature: Optional[str], | ||
| ) -> bool: | ||
| """Verifies if the given signature is valid""" | ||
| if timestamp is None or signature is None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇