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::loginreturns a code, not a token. The waiting happens in a task this crate spawns, and the caller comes back toAccount::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
maintainrenews well before expiry, and the store is written atomically — seeFileStore::savefor 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.
- Device
Code - A started device authorization, with the fields Hugging Face omits filled in.
- File
Store - The token file, and the three operations anything has on it.
- Identity
- Who a robot belongs to.
- Login
Code - A started login: what to show somebody, and how long they have to act on it.
- Status
- Everything
Account::statusknows, which is everything a client needs to render a wizard. - Stored
- What is on disk.
- Token
Response - What
POST /oauth/tokenanswers on success.
Enums§
Constants§
- ENDPOINT_
VAR - The variable
huggingface_hubitself 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
Nonewhen 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.