Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions slack_sdk/signature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For python 3.7 and 3.8 this Mapping type is not importable from collections.abc causing

slack_sdk/signature/__init__.py:30: in SignatureVerifier
    headers: Mapping[str, str],
E   TypeError: 'ABCMeta' object is not subscriptable

Importing Mapping as follows should fix the issue 🤔

from typing import TYPE_CHECKING,  Dict, Optional, Union

if TYPE_CHECKING:
    from collections.abc import Mapping
else:
    Mapping = Dict

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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:
Expand Down