Skip to content

Commit

Permalink
Refactor: update validation for Pydantic v2
Browse files Browse the repository at this point in the history
Changed @root_validator to @model_validator(mode="after") for checking data after the model is created.
Updated values to use a more flexible type for Pydantic v2.
Accessed values as a dictionary (values["payload"]) instead of using values.payload.
  • Loading branch information
Antonyjin committed Nov 12, 2024
1 parent 9604e70 commit 17cd07e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/aleph/vm/orchestrator/views/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from jwcrypto import jwk
from jwcrypto.jwa import JWA
from nacl.exceptions import BadSignatureError
from pydantic import BaseModel, ValidationError, field_validator, model_validator
from pydantic import (BaseModel, ValidationError, ValidationInfo,
field_validator, model_validator)
from solathon.utils import verify_signature

from aleph.vm.conf import settings
Expand Down Expand Up @@ -103,26 +104,23 @@ def payload_must_be_hex(cls, v: bytes) -> bytes:
return bytes.fromhex(v.decode())

@model_validator(mode="after")
@classmethod
def check_expiry(cls, values) -> dict[str, bytes]:
def check_expiry(values) -> 'SignedPubKeyHeader':
"""Check that the token has not expired"""
payload: bytes = values.payload
payload = values.payload
content = SignedPubKeyPayload.model_validate_json(payload)
if not is_token_still_valid(content.expires):
msg = "Token expired"
raise ValueError(msg)
raise ValueError("Token expired")
return values

@model_validator(mode="after")
@classmethod
def check_signature(cls, values) -> dict[str, bytes]:
def check_signature(values) -> 'SignedPubKeyHeader':
"""Check that the signature is valid"""
signature: list = values.signature
payload: bytes = values.payload
signature = values.signature
payload = values.payload
content = SignedPubKeyPayload.model_validate_json(payload)
check_wallet_signature_or_raise(content.address, content.chain, payload, signature)
return values

@property
def content(self) -> SignedPubKeyPayload:
"""Return the content of the header"""
Expand Down

0 comments on commit 17cd07e

Please sign in to comment.