From 61c0ac88d548c41e048a42ee582157005bb4a634 Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Fri, 6 Dec 2024 19:54:11 +0100 Subject: [PATCH] do more at once --- src/edutap/wallet_google/handlers/validate.py | 12 +++++------- tests/test_handler_validate.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/edutap/wallet_google/handlers/validate.py b/src/edutap/wallet_google/handlers/validate.py index 9857b1a..2224496 100644 --- a/src/edutap/wallet_google/handlers/validate.py +++ b/src/edutap/wallet_google/handlers/validate.py @@ -1,6 +1,6 @@ -from .._vendor.google_pay_token_decryption import GooglePayError from .._vendor.google_pay_token_decryption import GooglePayTokenDecryptor from ..models.callback import CallbackData +from ..models.callback import SignedMessage from ..settings import GoogleWalletSettings @@ -18,9 +18,10 @@ def _raw_private_key(inkey: str) -> str: return result -def verify_signature(data: CallbackData) -> bool: +def verified_signed_message(data: CallbackData) -> SignedMessage: """ Verifies the signature of the callback data. + and returns the parsed SignedMessage """ settings = GoogleWalletSettings() decryptor = GooglePayTokenDecryptor( @@ -28,8 +29,5 @@ def verify_signature(data: CallbackData) -> bool: settings.issuer_id, _raw_private_key(settings.credentials_info["private_key"]), ) - try: - decryptor.verify_signature(dict(data)) - except GooglePayError: - return False - return True + decryptor.verify_signature(dict(data)) + return SignedMessage.model_validate(data.signedMessage) diff --git a/tests/test_handler_validate.py b/tests/test_handler_validate.py index e00e6a3..f954fb1 100644 --- a/tests/test_handler_validate.py +++ b/tests/test_handler_validate.py @@ -1,6 +1,8 @@ -from edutap.wallet_google.handlers.validate import verify_signature +from edutap.wallet_google._vendor.google_pay_token_decryption import GooglePayError from edutap.wallet_google.models.callback import CallbackData +import pytest + callbackdata_for_test_failure = { "signature": "foo", @@ -18,6 +20,9 @@ } -def test_handler_validate_valid(): +def test_handler_validate_invalid(): + from edutap.wallet_google.handlers.validate import verified_signed_message + data = CallbackData.model_validate(callbackdata_for_test_failure) - assert verify_signature(data) is False + with pytest.raises(GooglePayError): + verified_signed_message(data)