|
| 1 | +import hashlib |
| 2 | +import hmac |
| 3 | +import jwt |
| 4 | + |
| 5 | +from typing import Union, Dict |
| 6 | +from messagebird.error import ValidationError |
| 7 | + |
| 8 | + |
| 9 | +class RequestValidator: |
| 10 | + """ |
| 11 | + RequestValidator validates request signature signed by MessageBird services. |
| 12 | +
|
| 13 | + See https://developers.messagebird.com/docs/verify-http-requests |
| 14 | + """ |
| 15 | + |
| 16 | + ALLOWED_ALGOS = ["HS256", "HS384", "HS512"] |
| 17 | + |
| 18 | + def __init__(self, signature_key: str, skip_url_validation: bool = False): |
| 19 | + """ |
| 20 | + :param signature_key: customer signature key. Can be retrieved through |
| 21 | + <a href="https://dashboard.messagebird.com/developers/settings">Developer Settings</a>. This is NOT your API key. |
| 22 | + :param skip_url_validation: whether url_hash claim validation should be skipped. |
| 23 | + Note that when true, no query parameters should be trusted. |
| 24 | + """ |
| 25 | + super().__init__() |
| 26 | + self._signature_key = signature_key |
| 27 | + self._skip_url_validation = skip_url_validation |
| 28 | + |
| 29 | + def __str__(self) -> str: |
| 30 | + return super().__str__() |
| 31 | + |
| 32 | + def validate_signature(self, signature: str, url: str, request_body: Union[bytes, bytearray]) -> Dict[str, str]: |
| 33 | + """ |
| 34 | + This method validates provided request signature, which is a JWT token. |
| 35 | + This JWT is signed with a MessageBird account unique secret key, ensuring the request is from MessageBird and |
| 36 | + a specific account. |
| 37 | +
|
| 38 | + The JWT contains the following claims: |
| 39 | +
|
| 40 | + * "url_hash" - the raw URL hashed with SHA256 ensuring the URL wasn't altered. |
| 41 | + * "payload_hash" - the raw payload hashed with SHA256 ensuring the payload wasn't altered. |
| 42 | + * "jti" - a unique token ID to implement an optional non-replay check (NOT validated by default). |
| 43 | + * "nbf" - the not before timestamp. |
| 44 | + * "exp" - the expiration timestamp is ensuring that a request isn't captured and used at a later time. |
| 45 | + * "iss" - the issuer name, always MessageBird. |
| 46 | +
|
| 47 | + :param signature: the actual signature taken from request header "MessageBird-Signature-JWT". |
| 48 | + :param url: the raw url including the protocol, hostname and query string, e.g. "https://example.com/?example=42". |
| 49 | + :param request_body: the raw request body. |
| 50 | + :returns: raw signature payload. |
| 51 | + :raises: ValidationError if signature is invalid. |
| 52 | + """ |
| 53 | + if not signature: |
| 54 | + raise ValidationError("Signature is empty") |
| 55 | + if not self._skip_url_validation and not url: |
| 56 | + raise ValidationError("URL is empty") |
| 57 | + |
| 58 | + try: |
| 59 | + claims = jwt.decode( |
| 60 | + jwt=signature, |
| 61 | + key=self._signature_key, |
| 62 | + algorithms=RequestValidator.ALLOWED_ALGOS, |
| 63 | + options={ |
| 64 | + "require": ["iss", "nbf", "exp"], |
| 65 | + "verify_iat": False, |
| 66 | + }, |
| 67 | + issuer="MessageBird", |
| 68 | + leeway=1 |
| 69 | + ) |
| 70 | + except jwt.InvalidTokenError as err: |
| 71 | + raise ValidationError(str(err)) from err |
| 72 | + |
| 73 | + if not self._skip_url_validation: |
| 74 | + expected_url_hash = hashlib.sha256(url.encode("utf-8")).hexdigest() |
| 75 | + if not hmac.compare_digest(expected_url_hash, claims["url_hash"]): |
| 76 | + raise ValidationError("invalid jwt: claim url_hash is invalid") |
| 77 | + |
| 78 | + payload_hash = claims.get("payload_hash") |
| 79 | + if not request_body and payload_hash: |
| 80 | + raise ValidationError("invalid jwt: claim payload_hash is set but actual payload is missing") |
| 81 | + if request_body and not payload_hash: |
| 82 | + raise ValidationError("invalid jwt: claim payload_hash is not set but payload is present") |
| 83 | + if request_body and not hmac.compare_digest(hashlib.sha256(request_body).hexdigest(), payload_hash): |
| 84 | + raise ValidationError("invalid jwt: claim payload_hash is invalid") |
| 85 | + |
| 86 | + return claims |
0 commit comments