Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(session): add TLS credential and identity support

Steven Enamakel84a9b85d2371
diff --git a/crates/openhuman-session/src/credential.rs b/crates/openhuman-session/src/credential.rs
new file mode 100644
index 0000000000..b16289f1a7
--- /dev/null
+++ b/crates/openhuman-session/src/credential.rs
@@ -0,0 +1,165 @@
+//! The credential a host hands to the core, and the pure JWT / payload helpers
+//! the session flow needs before it can hand one over.
+
+use chrono::{DateTime, Utc};
+use serde::{Deserialize, Serialize};
+use serde_json::Value;
+
+/// Which kind of backend credential a token is. The `as_str` values are the
+/// wire vocabulary of the core's `auth.set_credential` / `auth.get_state`.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "kebab-case")]
+pub enum CredentialKind {
+ /// A TinyHumans session JWT — `Authorization: Bearer` on every backend route.
+ Session,
+ /// A TinyHumans API key — bearer on managed inference, `x-api-key` on REST.
+ ApiKey,
+ /// The offline local session: a fake JWT whose signature segment is
+ /// literally `local`. Never sent to a backend.
+ Local,
+}
+
+impl CredentialKind {
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::Session => "session",
+ Self::ApiKey => "api-key",
+ Self::Local => "local",
+ }
+ }
+
+ pub fn parse(raw: &str) -> Option<Self> {
+ match raw.trim() {
+ "session" => Some(Self::Session),
+ "api-key" => Some(Self::ApiKey),
+ "local" => Some(Self::Local),
+ _ => None,
+ }
+ }
+}
+
+/// A backend credential together with what is locally knowable about it.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Credential {
+ pub kind: CredentialKind,
+ pub secret: String,
+ /// The JWT `exp` claim, when the token carries one. API keys and local
+ /// sessions never do.
+ pub expires_at: Option<DateTime<Utc>>,
+}
+
+impl Credential {
+ pub fn session(token: impl Into<String>) -> Self {
+ let secret = token.into().trim().to_string();
+ let expires_at = decode_jwt_exp(&secret);
+ Self {
+ kind: CredentialKind::Session,
+ secret,
+ expires_at,
+ }
+ }
+
+ pub fn api_key(key: impl Into<String>) -> Self {
+ Self {
+ kind: CredentialKind::ApiKey,
+ secret: key.into().trim().to_string(),
+ expires_at: None,
+ }
+ }
+
+ pub fn local(token: impl Into<String>) -> Self {
+ Self {
+ kind: CredentialKind::Local,
+ secret: token.into().trim().to_string(),
+ expires_at: None,
+ }
+ }
+
+ /// Classify a bare token the way the core always has: a three-segment
+ /// token whose third segment is literally `local` is the offline session,
+ /// anything else is a session JWT. API keys are never classified from
+ /// shape — a host that holds one knows it does.
+ pub fn classify(token: &str) -> Self {
+ if is_local_session_token(token) {
+ Self::local(token)
+ } else {
+ Self::session(token)
+ }
+ }
+
+ pub fn is_local(&self) -> bool {
+ self.kind == CredentialKind::Local
+ }
+}
+
+/// Whether `token` is the offline local session shape.
+pub fn is_local_session_token(token: &str) -> bool {
+ let trimmed = token.trim();
+ let mut parts = trimmed.split('.');
+ matches!(
+ (parts.next(), parts.next(), parts.next(), parts.next()),
+ (Some(_), Some(_), Some("local"), None)
+ )
+}
+
+/// Best-effort decode of a JWT's `exp` claim. Does not verify the signature —
+/// the backend stays the authority on validity; this only lets a host refuse
+/// to store a token it can already see is dead.
+pub fn decode_jwt_exp(token: &str) -> Option<DateTime<Utc>> {
+ DateTime::<Utc>::from_timestamp(tinyhumans_sdk::jwt::decode_jwt_exp_unix(token)?, 0)
+}
+
+/// `Some(exp)` when the token's `exp` is still in the future at `now`;
+/// `None` for an expired token **or** a token with no decodable `exp`.
+pub fn jwt_is_live(token: &str, now: DateTime<Utc>) -> Option<DateTime<Utc>> {
+ let exp = decode_jwt_exp(token)?;
+ (exp > now).then_some(exp)
+}
+
+/// The subject of a JWT, read from its payload claims without verification.
+/// Checked in order: `sub`, `userId`, `user_id`, `_id`, `id`.
+pub fn user_id_from_jwt_claims(token: &str) -> Option<String> {
+ let claims = tinyhumans_sdk::jwt::decode_jwt_payload(token)?;
+ let obj = claims.as_object()?;
+ ["sub", "userId", "user_id", "_id", "id"]
+ .iter()
+ .find_map(|key| obj.get(*key).and_then(Value::as_str))
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ .map(str::to_string)
+}
+
+fn user_id_from_object(obj: &serde_json::Map<String, Value>) -> Option<String> {
+ for key in ["id", "_id", "userId"] {
+ if let Some(value) = obj.get(key).and_then(Value::as_str) {
+ let trimmed = value.trim();
+ if !trimmed.is_empty() {
+ return Some(trimmed.to_string());
+ }
+ }
+ }
+ None
+}
+
+/// The user id inside a `/auth/me`-shaped payload: top-level `id`/`_id`/
+/// `userId`, or the same under `data` or `user`. Same rules as the core's
+/// `api::rest::user_id_from_profile_payload`.
+pub fn user_id_from_profile_payload(payload: &Value) -> Option<String> {
+ let obj = payload.as_object()?;
+ if let Some(data) = obj.get("data").and_then(Value::as_object) {
+ return user_id_from_object(data).or_else(|| {
+ data.get("user")
+ .and_then(Value::as_object)
+ .and_then(user_id_from_object)
+ });
+ }
+ user_id_from_object(obj).or_else(|| {
+ obj.get("user")
+ .and_then(Value::as_object)
+ .and_then(user_id_from_object)
+ })
+}
+
+#[cfg(test)]
+#[path = "credential_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-session/src/identity.rs b/crates/openhuman-session/src/identity.rs
new file mode 100644
index 0000000000..df212aa175
--- /dev/null
+++ b/crates/openhuman-session/src/identity.rs
@@ -0,0 +1,29 @@
+//! Process-global, synchronous peek at the signed-in user id.
+//!
+//! Sentry `before_send` hooks and similar sync callers cannot await the
+//! manager, so the manager mirrors the current user id into this slot on every
+//! state change. Only the id is kept — never a token, never the profile.
+
+use std::sync::{OnceLock, RwLock};
+
+static USER_ID: OnceLock<RwLock<Option<String>>> = OnceLock::new();
+
+fn slot() -> &'static RwLock<Option<String>> {
+ USER_ID.get_or_init(|| RwLock::new(None))
+}
+
+/// Record the signed-in user id (or clear it with `None`).
+pub fn set_user_id(user_id: Option<String>) {
+ let mut guard = slot().write().unwrap_or_else(|p| p.into_inner());
+ *guard = user_id.map(|id| id.trim().to_string()).filter(|id| !id.is_empty());
+}
+
+/// Forget the signed-in user id.
+pub fn clear() {
+ set_user_id(None);
+}
+
+/// The signed-in user id, if any.
+pub fn peek_user_id() -> Option<String> {
+ slot().read().unwrap_or_else(|p| p.into_inner()).clone()
+}
diff --git a/crates/openhuman-session/src/lib.rs b/crates/openhuman-session/src/lib.rs
new file mode 100644
index 0000000000..d535942ee8
--- /dev/null
+++ b/crates/openhuman-session/src/lib.rs
@@ -0,0 +1,37 @@
+//! TinyHumans login and backend-session ownership for OpenHuman hosts.
+//!
+//! The core (`openhuman`) only *holds and uses* a backend credential — a
+//! session JWT or a TinyHumans API key — and never obtains, validates,
+//! exchanges or refreshes one. Everything that talks to the backend's auth
+//! endpoints lives here instead and is shared by the hosts that drive a user
+//! login: the Tauri shell (over the core's HTTP JSON-RPC) and the TUI (over an
+//! in-process `CoreRuntime`).
+//!
+//! * [`SessionClient`] — the backend calls: login-token exchange and
+//! `GET /auth/me`, with the store-time timeout / retry / transient policy.
+//! * [`CurrentUserCache`] — the last `/auth/me` answer, served fresh or
+//! stale-while-revalidate, with a bounded backoff while the backend is down.
+//! * [`CoreLink`] — how a credential is pushed into *whichever* core the host
+//! owns (`auth.set_credential` / `auth.clear_credential` / `auth.get_state`).
+//! * [`SessionManager`] — the host-facing orchestration on top of the three:
+//! login, store, logout, current user, state and change events.
+//!
+//! No `openhuman` dependency: this crate must stay buildable from the Tauri
+//! shell's own Cargo world and must never pull the core's policy back in.
+
+pub mod cache;
+pub mod client;
+pub mod credential;
+pub mod identity;
+pub mod link;
+pub mod manager;
+mod tls;
+
+pub use cache::{CachedUser, CurrentUserCache};
+pub use client::{ClientHeaders, FetchMeError, SessionClient, SessionClientError};
+pub use credential::{
+ decode_jwt_exp, jwt_is_live, user_id_from_jwt_claims, user_id_from_profile_payload,
+ Credential, CredentialKind,
+};
+pub use link::{CoreAuthState, CoreLink};
+pub use manager::{SessionError, SessionEvent, SessionManager, SessionState};
diff --git a/crates/openhuman-session/src/tls.rs b/crates/openhuman-session/src/tls.rs
new file mode 100644
index 0000000000..2207dfbdc8
--- /dev/null
+++ b/crates/openhuman-session/src/tls.rs
@@ -0,0 +1,13 @@
+//! Platform-conditional TLS backend selection, mirroring the core's
+//! `util::tls::tls_client_builder`: Windows uses schannel (`native-tls`) so the
+//! OS certificate store — including corporate CAs installed by TLS-inspecting
+//! proxies — is honoured; everywhere else uses `rustls` + webpki roots.
+
+pub(crate) fn client_builder() -> reqwest::ClientBuilder {
+ let b = reqwest::Client::builder();
+ #[cfg(target_os = "windows")]
+ let b = b.use_native_tls();
+ #[cfg(not(target_os = "windows"))]
+ let b = b.use_rustls_tls();
+ b
+}