Skip to content

unaiverse.networking.node.tokens

Every node proves who it is with a node token, a JSON Web Token that the UNaIVERSE server signs with its private RSA key. A node receives its token from Node.get_node_token together with the public key of the server and attaches the token to everything it sends. Another node checks the token with that public key before it trusts the sender. This module holds the one class that performs the check.

TokenVerifier is used by the SDK and rarely by application code. NodeConn (in connpool) builds one verifier from the public key that the server returned with the token of the node. It then checks the token that every inbound message carries and the token that a peer offers in the handshake of a Session (see Networking).

A node token carries these claims, all written by the server:

Claim Type Meaning
node_id str The id of the node on its account.
cv_hash str The BLAKE2b digest (16 bytes, hex) of the CV of the node. A receiver compares it with the CV in the offered profile (NodeProfile.verify_cv_hash).
p2p_peers list of str or its JSON text The two peer ids of the node, the public one and the world one.
exp int The expiry time in Unix seconds. A token without exp is rejected.
code_hash str World nodes only. The digest of the code that the world declared with code_repo and code_commit. Absent for agents and for worlds that declared nothing.
ip, hostname, port Optional claims that the verifier compares only when the caller asks for it.

TokenVerifier

TokenVerifier(public_key: str | bytes)
Source code in unaiverse/networking/node/tokens.py
def __init__(self, public_key: str | bytes):
    """Initializes the `TokenVerifier` with a public key.

    This key is essential for securely decoding and verifying JSON Web Tokens (JWTs) issued by a corresponding
    private key. The public key can be provided as either a string or a bytes object.

    Args:
        public_key: The public key used for decoding and verification.
    """
    self.public_key = public_key

A TokenVerifier checks node tokens against one public key. The constructor stores the key without validating it. A malformed key therefore makes every later verification fail instead of raising an error.

Argument Type Default Meaning
public_key str or bytes required The RSA public key of the UNaIVERSE server in PEM format. Node.public_key holds this key once the node has obtained its token.

The two verification methods share one decoding path. It checks the RS256 signature and requires an exp claim that lies in the future. Any decoding error (bad signature, expired token, malformed token, unusable key or anything else) counts as a failed verification. Neither method raises.

from unaiverse.networking.node.tokens import TokenVerifier

verifier = TokenVerifier(node.public_key)
node_id, cv_hash = verifier.verify_token(token, p2p_peer=sender_peer_id)
if node_id is None:
    print("Token refused: unsigned, expired or issued to another peer")

public_key instance-attribute

public_key = public_key

The key passed to the constructor as a str or bytes object. The verifier reads it at every call and a new value therefore applies to every later verification.

verify_token

verify_token(token: str | bytes, node_id: str | None = None, ip: str | None = None, hostname: str | None = None, port: int | None = None, p2p_peer: str | None = None)
Source code in unaiverse/networking/node/tokens.py
def verify_token(self, token: str | bytes,
                 node_id: str | None = None, ip: str | None = None,
                 hostname: str | None = None,
                 port: int | None = None,
                 p2p_peer: str | None = None):
    """Verifies a JSON Web Token (JWT) against a set of criteria.

    The token is decoded with the provided public key (RS256), then optional
    checks ensure the payload matches `node_id`, `ip`, `hostname`, `port`, and
    that `p2p_peer` is present in the token's `p2p_peers` list.

    Args:
        token: The JWT to verify, as a string or bytes object.
        node_id: Optional `node_id` to check against the token's payload.
        ip: Optional IP address to check.
        hostname: Optional hostname to check.
        port: Optional port number to check.
        p2p_peer: Optional peer identifier to check within the `p2p_peers` list.

    Returns:
        A tuple `(node_id, cv_hash)` from the token's payload if all checks pass,
        otherwise `(None, None)`.
    """
    payload = self._decode(token, node_id=node_id, ip=ip, hostname=hostname, port=port, p2p_peer=p2p_peer)
    if payload is None:
        return None, None
    return payload.get("node_id", None), payload.get("cv_hash", None)

Verifies a token and returns the two claims that identify the sender.

Argument Type Default Meaning
token str or bytes required The encoded token.
node_id str or None None When given, the node_id claim must be equal to it.
ip str or None None When given, the ip claim must be equal to it.
hostname str or None None When given, the hostname claim must be equal to it.
port int or None None When given, the port claim must be equal to it.
p2p_peer str or None None When given, this peer id must appear in the p2p_peers claim. This is the check that binds a token to the peer that presented it.

The p2p_peers claim may arrive as a JSON string. The verifier decodes it into a tuple of peer ids before the membership test. A string that is not valid JSON fails the verification.

Returns the tuple (node_id, cv_hash) from the claims when every check passes and (None, None) otherwise. A valid token without the node_id claim returns None in the first position and callers treat that result as a failure.

NodeConn.verify_token calls it with p2p_peer equal to the sender of every inbound message. A message whose token fails the check is discarded.

verify_token_claims

verify_token_claims(token: str | bytes, node_id: str | None = None, ip: str | None = None, hostname: str | None = None, port: int | None = None, p2p_peer: str | None = None)
Source code in unaiverse/networking/node/tokens.py
def verify_token_claims(self, token: str | bytes,
                        node_id: str | None = None, ip: str | None = None,
                        hostname: str | None = None,
                        port: int | None = None,
                        p2p_peer: str | None = None):
    """Like `verify_token` but also returns the root-attested `p2p_peers`
    binding, used by the Session handshake (RT) to consume the public<->world
    peer-id binding instead of trusting the offered profile.

    Returns:
        A tuple `(node_id, cv_hash, p2p_peers, exp, code_hash)` if all checks pass,
        otherwise `(None, None, (), 0, None)`. `p2p_peers` is a tuple of the node's
        peer-ids, `exp` is the JWT expiry (unix seconds), used by the in-session
        refresh, and `code_hash` is the root-attested digest of the world's declared
        code (None when the token carries no such claim: agent nodes, or a world
        that declared no source repository).
    """
    payload = self._decode(token, node_id=node_id, ip=ip, hostname=hostname, port=port, p2p_peer=p2p_peer)
    if payload is None:
        return None, None, (), 0, None
    return (payload.get("node_id", None), payload.get("cv_hash", None),
            payload.get("p2p_peers", ()), payload.get("exp", 0),
            payload.get("code_hash", None))

Runs the same checks as verify_token with the same arguments and returns every claim that the handshake of a Session needs.

Returns the tuple (node_id, cv_hash, p2p_peers, exp, code_hash) when every check passes:

Position Type Meaning
node_id str or None The node_id claim.
cv_hash str or None The cv_hash claim.
p2p_peers tuple[str, ...] The peer ids of the node as a tuple decoded from the claim. A world uses them to check that the world-side peer id of a joining agent belongs to the same node as its public peer id.
exp int The expiry time in Unix seconds. The Session uses it to refresh the token before it expires.
code_hash str or None The digest of the declared world code or None when the token has no such claim.

On failure it returns (None, None, (), 0, None). The Sessions of a node call it through NodeConn.verify_token_claims with p2p_peer equal to the transport-verified peer id of the remote side. They hand the result to the decision policy of the node as verified_claims.