|
| 1 | +from enum import Enum |
| 2 | +import json |
| 3 | +from dataclasses import asdict, dataclass |
| 4 | +from typing import List |
| 5 | + |
| 6 | + |
| 7 | +class AuthenticatorType(str, Enum): |
| 8 | + AllOf = "AllOf" |
| 9 | + AnyOf = "AnyOf" |
| 10 | + SignatureVerification = "SignatureVerification" |
| 11 | + MessageFilter = "MessageFilter" |
| 12 | + SubaccountFilter = "SubaccountFilter" |
| 13 | + ClobPairIdFilter = "ClobPairIdFilter" |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class Authenticator: |
| 18 | + type: AuthenticatorType |
| 19 | + config: bytes |
| 20 | + |
| 21 | + # helpers to create Authenticator instances |
| 22 | + @classmethod |
| 23 | + def signature_verification(cls, pub_key: bytes) -> "Authenticator": |
| 24 | + """Enables authentication via a specific key.""" |
| 25 | + return Authenticator( |
| 26 | + AuthenticatorType.SignatureVerification, |
| 27 | + pub_key, |
| 28 | + ) |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def message_filter(cls, msg_type: str) -> "Authenticator": |
| 32 | + """Restricts authentication to certain message types.""" |
| 33 | + return Authenticator( |
| 34 | + AuthenticatorType.MessageFilter, |
| 35 | + msg_type.encode(), |
| 36 | + ) |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def subaccount_filter(cls, subaccounts: List[int]) -> "Authenticator": |
| 40 | + """Restricts authentication to a specific subaccount.""" |
| 41 | + config = ",".join(map(str, subaccounts)) |
| 42 | + return Authenticator( |
| 43 | + AuthenticatorType.SubaccountFilter, |
| 44 | + config.encode(), |
| 45 | + ) |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def clob_pair_id_filter(cls, clob_pair_ids: List[int]) -> "Authenticator": |
| 49 | + """Restricts authentication to a specific clob pair id.""" |
| 50 | + config = ",".join(map(str, clob_pair_ids)) |
| 51 | + return Authenticator( |
| 52 | + AuthenticatorType.ClobPairIdFilter, |
| 53 | + config.encode(), |
| 54 | + ) |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def compose( |
| 58 | + cls, auth_type: AuthenticatorType, sub_authenticators: list["Authenticator"] |
| 59 | + ) -> "Authenticator": |
| 60 | + """Combines multiple sub-authenticators into a single one.""" |
| 61 | + composed_config = json.dumps( |
| 62 | + [sa.todict() for sa in sub_authenticators], |
| 63 | + separators=(",", ":"), |
| 64 | + ) |
| 65 | + return Authenticator( |
| 66 | + auth_type, |
| 67 | + composed_config.encode(), |
| 68 | + ) |
| 69 | + |
| 70 | + def todict(self) -> dict: |
| 71 | + """Prepare object for composition.""" |
| 72 | + dicls = asdict(self) |
| 73 | + dicls["config"] = list(dicls["config"]) |
| 74 | + return dicls |
| 75 | + |
| 76 | + |
| 77 | +def validate_authenticator(authenticator: Authenticator) -> bool: |
| 78 | + """Validate the authenticator.""" |
| 79 | + if authenticator.config.startswith(b"["): |
| 80 | + decoded_config = json.loads(authenticator.config.decode()) |
| 81 | + else: |
| 82 | + decoded_config = authenticator.config |
| 83 | + |
| 84 | + return check_authenticator(dict(type=authenticator.type, config=decoded_config)) |
| 85 | + |
| 86 | + |
| 87 | +def check_authenticator(auth: dict) -> bool: |
| 88 | + """ |
| 89 | + Check if the authenticator is safe to use. |
| 90 | + Parameters: |
| 91 | + - auth is a decoded authenticator object. |
| 92 | + """ |
| 93 | + if not is_authenticator_alike(auth): |
| 94 | + return False |
| 95 | + |
| 96 | + if auth["type"] == AuthenticatorType.SignatureVerification: |
| 97 | + # SignatureVerification authenticator is considered safe |
| 98 | + return True |
| 99 | + |
| 100 | + if not isinstance(auth["config"], list): |
| 101 | + return False |
| 102 | + |
| 103 | + if auth["type"] == AuthenticatorType.AnyOf: |
| 104 | + # ANY_OF is safe only if ALL sub-authenticators return true |
| 105 | + return all(check_authenticator(sub_auth) for sub_auth in auth["config"]) |
| 106 | + |
| 107 | + if auth["type"] == AuthenticatorType.AllOf: |
| 108 | + # ALL_OF is safe if at least one sub-authenticator returns true |
| 109 | + return any(check_authenticator(sub_auth) for sub_auth in auth["config"]) |
| 110 | + |
| 111 | + # If it's a base-case authenticator but not SignatureVerification, it's unsafe |
| 112 | + return False |
| 113 | + |
| 114 | + |
| 115 | +def is_authenticator_alike(auth: dict) -> bool: |
| 116 | + return isinstance(auth, dict) and auth.get("type") and auth.get("config") |
0 commit comments