Skip to content

Commit

Permalink
fix tests and data structure for callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Dec 16, 2024
1 parent dab2323 commit c20ee47
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/edutap/wallet_google/handlers/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ async def handle_callback(request: Request, callback_data: CallbackData):
It is called by Google Wallet API when a user interacts with a pass.
The callback is triggered on save and delete of a pass in the wallet.
"""
callback_message = verified_signed_message(callback_data)
logger.debug(f"Got message {callback_message}")

# get the registered callback handlers
handlers = get_callback_handlers()
if len(handlers) == 0:
Expand All @@ -34,6 +31,10 @@ async def handle_callback(request: Request, callback_data: CallbackData):
status_code=500, detail="No callback handlers were registered."
)

# extract and verify message (given verification is not disabled)
callback_message = verified_signed_message(callback_data)
logger.debug(f"Got message {callback_message}")

# call each handler asynchronously
try:
await asyncio.gather(
Expand Down
19 changes: 10 additions & 9 deletions src/edutap/wallet_google/handlers/validate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .._vendor.google_pay_token_decryption import GooglePayTokenDecryptor
from ..models.callback import CallbackData
from ..models.callback import SignedMessage
from ..settings import Settings
from ..session import session_manager


def _raw_private_key(in_key: str) -> str:
Expand All @@ -23,11 +23,12 @@ def verified_signed_message(data: CallbackData) -> SignedMessage:
Verifies the signature of the callback data.
and returns the parsed SignedMessage
"""
settings = Settings()
decryptor = GooglePayTokenDecryptor(
settings.google_root_signing_public_keys.dict()["keys"],
settings.issuer_id,
_raw_private_key(settings.credentials_info["private_key"]),
)
decryptor.verify_signature(data.model_dump(mode="json"))
return SignedMessage.model_validate(data.signedMessage)
settings = session_manager.settings
if settings.callback_verify_signature:
decryptor = GooglePayTokenDecryptor(
settings.google_root_signing_public_keys.dict()["keys"],
settings.issuer_id,
_raw_private_key(settings.credentials_info["private_key"]),
)
decryptor.verify_signature(data.model_dump(mode="json"))
return SignedMessage.model_validate_json(data.signedMessage)
4 changes: 3 additions & 1 deletion src/edutap/wallet_google/models/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class IntermediateSigningKey(Model):
class SignedMessage(Model):
classId: str
objectId: str
expTimeMillis: int
eventType: EventType
expTimeMillis: int
count: int
nonce: str


class CallbackData(Model):
Expand Down
1 change: 1 addition & 0 deletions src/edutap/wallet_google/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Settings(BaseSettings):
save_url: HttpUrl = HttpUrl(SAVE_URL)
callback_url: HttpUrl | None = None
callback_prefix: str = "/googlewallet"
callback_verify_signature: bool = True

scopes: list[str] = [SCOPE]

Expand Down
3 changes: 2 additions & 1 deletion tests/data/test_wallet_google_plugins/plugins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from edutap.wallet_google.models.callback import CallbackData
from edutap.wallet_google.models.handlers import ImageData


Expand All @@ -15,4 +16,4 @@ class TestCallbackHandler:
Implementation of edutap.wallet_google.protocols.CallbackHandler
"""

async def handle(self, pass_id: str) -> None: ...
async def handle(self, pass_id: CallbackData) -> None: ...
2 changes: 2 additions & 0 deletions tests/test_handler_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"objectId": "2",
"expTimeMillis": 0,
"eventType": "SAVE",
"count": 0,
"nonce": "3",
},
}

Expand Down

0 comments on commit c20ee47

Please sign in to comment.