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
¶
Source code in unaiverse/networking/node/tokens.py
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")
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
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
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.