Skip to main content

Crate hf_robot_account

Crate hf_robot_account 

Source
Expand description

Sign a browserless device in to a Hugging Face account, and keep it signed in.

A robot that only ever talks to a client on its own LAN needs no account. An account is what makes one reachable from outside the LAN: a rendezvous service resolves a bearer token to an identity, shows a client only its own robots, and the pair of those is the authorisation a session arrives with. This crate owns the credential and nothing else — it neither knows nor cares what the token is later presented to.

§The flow is RFC 8628, because the device has no browser

The obvious alternative, authorization code + PKCE, points a redirect URI at an HTTP server on the robot. That needs a redirect URI registered per hostname, and a browser that can resolve and reach the robot — so logging in requires being on the robot’s network with mDNS working, and the robot is not even the party that authenticates. It merely hosts the landing pad.

The device grant inverts it. The robot asks Hugging Face for a code, says “open hf.co/oauth/device and type M8HJ-FMGN”, and polls. No redirect URI, no hostname, and no requirement that the authorising device can reach the robot at all — a phone on cellular is fine. It is also the only one of the two that yields a refresh token, which is what keeps a robot signed in past its first month.

The cost, stated plainly: somebody types eight characters.

§Three consequences shape this API

  • Account::login returns a code, not a token. The waiting happens in a task this crate spawns, and the caller comes back to Account::status. A login that reported success by holding a connection open would work from a laptop and fail from the device it is for: a phone that opens a browser backgrounds itself, and iOS then tears a GATT link down.
  • The caller displays the code, and the code has to be typed. Hugging Face sends no verification_uri_complete, and its device page ignores ?user_code= — so no URL carries the code, which makes showing it the caller’s whole job. Whether a caller also opens the page is a property of the surface, not a rule: a terminal keeps the code in the scrollback and can open a browser, a phone app must not, because the browser replaces the only screen and the code is gone before it was read.
  • A token expires in 30 days and its refresh token rotates. So maintain renews well before expiry, and the store is written atomically — see FileStore::save for the one window that cannot be closed.

§Using it

use std::sync::Arc;
use hf_robot_account::{Account, Config, FileStore, maintain};

let store = FileStore::at("/etc/robot/hf-token").readable_by_group("robot");
let account = Arc::new(Account::new(store, Config::from_env()));

// Renew the token for as long as this process runs.
tokio::spawn(maintain(Arc::clone(&account)));

// A client asks to sign in: answer with the code, and let it come back to `status`.
let code = account.login(false).await?;
println!("Open {} and type {}", code.verification_uri, code.user_code);

§What is deliberately not here

Any notion of what the token is for. No relay, no rendezvous, no IPC. A daemon wraps these four methods in whatever protocol it serves and maps Error onto whatever error codes it has; this crate has no opinion about either.

Windows. The credential is a file with an owner, a group and a mode; this is a Unix crate, and says so rather than carrying cfg branches nothing compiles.

A TokenStore trait. FileStore takes a path, an optional group and a mode, which covers every difference between boards that actually exists. A robot that wants a TPM or a keyring is a reason to add the trait, and adding it then is a smaller change than carrying the indirection until somebody does.

Structs§

Account
The account, as the four operations on it see it.
Config
Everything about a login that a different robot might want to change.
DeviceCode
A started device authorization, with the fields Hugging Face omits filled in.
FileStore
The token file, and the three operations anything has on it.
Identity
Who a robot belongs to.
LoginCode
A started login: what to show somebody, and how long they have to act on it.
Status
Everything Account::status knows, which is everything a client needs to render a wizard.
Stored
What is on disk.
TokenResponse
What POST /oauth/token answers on success.

Enums§

Error
What can go wrong.
Poll
One poll of the token endpoint.

Constants§

ENDPOINT_VAR
The variable huggingface_hub itself reads to point at a mirror. Config::from_env.
HUGGINGFACE
Hugging Face.
HUGGINGFACE_CLIENT_ID
Hugging Face’s first-party device-code OAuth client.

Functions§

http_client
An HTTP client configured the way every call below expects.
maintain
Keep the token fresh for as long as this process runs.
read_access_token
The access token out of a credential file, or None when the robot belongs to nobody.
refresh
Exchange a refresh token for a new pair.
request_device_code
Ask Hugging Face to start a device authorization.
userinfo
Who a token belongs to.