The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(oauth): add handoff endpoint for OAuth token exchange
Steven Enamakel2c5c31241949
diff --git a/crates/openhuman-tinyhumans/Cargo.toml b/crates/openhuman-tinyhumans/Cargo.tomlindex c25c016e0f..23f4a0533f 100644--- a/crates/openhuman-tinyhumans/Cargo.toml+++ b/crates/openhuman-tinyhumans/Cargo.toml@@ -41,7 +41,7 @@ scheduler-gate = ["openhuman-embed/scheduler-gate"][dependencies]openhuman-embed = { path = "../openhuman-embed", default-features = false }-# Direct access to `api::transport` / `core::all` — the surfaces embed does+# Direct access to `backend` / `core::all` — the surfaces embed does# not (and should not) re-export. Features unify with embed's forwarding.openhuman-core = { path = "../openhuman-core", package = "openhuman", default-features = false }# The vendored TinyHumans SDK: the only crate in the tree that may depend on@@ -62,8 +62,11 @@ tinyjevclient = { git = "https://github.com/tinyhumansai/tinyjevclient", rev = "# for the managed Telegram/Discord link flows in `hosted::channel_link`. Same# vendored path as the core's, so the types unify. Sync and I/O-free.tinychannels-bus = { path = "../../vendor/tinychannels/crates/tinychannels-bus" }+# AES-256-GCM decryption of the backend OAuth token handoff (hosted::oauth).+aes-gcm = "0.10"anyhow = "1"async-trait = "0.1"+base64 = "0.22"chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }log = "0.4"reqwest = { version = "0.12", default-features = false, features = ["json", "multipart", "rustls-tls"] }diff --git a/crates/openhuman-tinyhumans/src/hosted/oauth/handoff.rs b/crates/openhuman-tinyhumans/src/hosted/oauth/handoff.rsnew file mode 100644index 0000000000..d0edd7b1fd--- /dev/null+++ b/crates/openhuman-tinyhumans/src/hosted/oauth/handoff.rs@@ -0,0 +1,96 @@+//! Decryption of the backend's one-time OAuth token handoff+//! (`POST /auth/integrations/{id}/tokens` returns the tokens AES-256-GCM+//! encrypted under a key the caller supplied).++use anyhow::{Context, Result};+use base64::Engine;++/// AES-256-GCM decrypt compatible with backend `encryptMessageFromString` (IV 16 + tag 16 + ciphertext, base64).+pub fn decrypt_handoff_blob(b64_ciphertext: &str, key_str: &str) -> Result<String> {+ let key = key_bytes_from_string(key_str)?;+ let combined = base64::engine::general_purpose::STANDARD+ .decode(b64_ciphertext.trim())+ .context("base64-decode encrypted payload")?;+ if combined.len() < 32 {+ anyhow::bail!("encrypted payload too short");+ }+ let iv = &combined[0..16];+ let tag = &combined[16..32];+ let ciphertext = &combined[32..];++ // aes-gcm expects ciphertext || tag+ let mut ct_with_tag = Vec::with_capacity(ciphertext.len() + tag.len());+ ct_with_tag.extend_from_slice(ciphertext);+ ct_with_tag.extend_from_slice(tag);++ use aes_gcm::aead::generic_array::typenum::U16;+ use aes_gcm::aead::{Aead, KeyInit};+ use aes_gcm::aes::Aes256;+ use aes_gcm::AesGcm;+ type Aes256Gcm16 = AesGcm<Aes256, U16>;++ let cipher =+ Aes256Gcm16::new_from_slice(&key).map_err(|e| anyhow::anyhow!("invalid AES key: {e}"))?;+ let nonce = aes_gcm::aead::generic_array::GenericArray::from_slice(iv);+ let plain = cipher+ .decrypt(nonce, ct_with_tag.as_ref())+ .map_err(|e| anyhow::anyhow!("AES-GCM decrypt failed: {e}"))?;++ String::from_utf8(plain).context("handoff plaintext is not UTF-8")+}++/// Decode the shared encryption key into 32 raw AES bytes.+///+/// Accepts, in order of preference:+/// 1. base64url without padding — the current backend format (e.g.+/// a 43-char alphanumeric string using `-` / `_`). This must be tried+/// BEFORE standard base64 because `-`/`_` are invalid in the standard+/// alphabet and would fail cleanly, whereas a standard-base64 string+/// never contains `-`/`_` so base64url_no_pad will still decode it+/// correctly as long as there's no padding.+/// 2. base64url with padding.+/// 3. Standard base64 with padding (legacy backend format).+/// 4. Standard base64 without padding.+/// 5. A raw 32-byte ASCII key (len == 32, used as-is).+///+/// NOTE: the key is only decoded locally for AES-GCM key material in+/// `decrypt_handoff_blob`. The key sent back to the backend (in the+/// `{ key: ... }` POST body of `fetch_integration_tokens_handoff`) is the+/// original string — never re-encoded — so base64url keys stay base64url+/// on the wire.+fn key_bytes_from_string(key: &str) -> Result<Vec<u8>> {+ let trimmed = key.trim();++ // Raw 32-byte ASCII key+ if trimmed.len() == 32 && !trimmed.contains(['+', '/', '-', '_', '=']) {+ return Ok(trimmed.as_bytes().to_vec());+ }++ use base64::engine::general_purpose::{STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};++ // `base64::Engine` has generic methods and therefore isn't+ // dyn-compatible, so we unroll the attempts instead of looping over+ // a slice of trait objects.+ macro_rules! try_decode {+ ($engine:expr) => {+ if let Ok(decoded) = $engine.decode(trimmed) {+ if decoded.len() == 32 {+ return Ok(decoded);+ }+ }+ };+ }+ try_decode!(URL_SAFE_NO_PAD);+ try_decode!(URL_SAFE);+ try_decode!(STANDARD);+ try_decode!(STANDARD_NO_PAD);++ anyhow::bail!(+ "encryption key must decode to 32 raw bytes (raw, base64, or base64url accepted; got len={})",+ trimmed.len()+ );+}++#[cfg(test)]+#[path = "handoff_tests.rs"]+mod tests;diff --git a/crates/openhuman-tinyhumans/src/hosted/oauth/handoff_tests.rs b/crates/openhuman-tinyhumans/src/hosted/oauth/handoff_tests.rsnew file mode 100644index 0000000000..52f42cd810--- /dev/null+++ b/crates/openhuman-tinyhumans/src/hosted/oauth/handoff_tests.rs@@ -0,0 +1,48 @@+use super::*;+use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};++#[test]+fn decodes_base64url_no_pad() {+ // A 32-byte key that, when base64url-encoded, contains both `-` and `_`.+ let raw = [+ 0xff_u8, 0xfb, 0xef, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,+ 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,+ 0x0b, 0x0c, 0x0d,+ ];+ let url_key = URL_SAFE_NO_PAD.encode(raw);+ assert!(url_key.contains('-') || url_key.contains('_'));+ let decoded = key_bytes_from_string(&url_key).unwrap();+ assert_eq!(decoded, raw);+}++#[test]+fn decodes_standard_base64() {+ let raw = [0x41_u8; 32];+ let std_key = STANDARD.encode(raw);+ let decoded = key_bytes_from_string(&std_key).unwrap();+ assert_eq!(decoded, raw);+}++#[test]+fn decodes_raw_32_byte_key() {+ let raw = "abcdefghijklmnopqrstuvwxyz012345";+ assert_eq!(raw.len(), 32);+ let decoded = key_bytes_from_string(raw).unwrap();+ assert_eq!(decoded, raw.as_bytes());+}++#[test]+fn trims_whitespace() {+ let raw = [0x42_u8; 32];+ let url_key = format!(" {}\n", URL_SAFE_NO_PAD.encode(raw));+ let decoded = key_bytes_from_string(&url_key).unwrap();+ assert_eq!(decoded, raw);+}++#[test]+fn rejects_wrong_length() {+ let err = key_bytes_from_string("tooshort").unwrap_err();+ assert!(err.to_string().contains("must decode to 32 raw bytes"));+}++use super::user_id_from_profile_payload;diff --git a/crates/openhuman-tinyhumans/src/hosted/oauth/mod.rs b/crates/openhuman-tinyhumans/src/hosted/oauth/mod.rsindex 9427464348..f2870658e3 100644--- a/crates/openhuman-tinyhumans/src/hosted/oauth/mod.rs+++ b/crates/openhuman-tinyhumans/src/hosted/oauth/mod.rs@@ -11,6 +11,7 @@//! from the SDK's public-route registry, so it stays on the core's pre-SDK path//! until the route is added upstream.+mod handoff;mod ops;mod schemas;mod types;diff --git a/crates/openhuman-tinyhumans/src/hosted/oauth/ops.rs b/crates/openhuman-tinyhumans/src/hosted/oauth/ops.rsindex f306f3ea30..d138f42577 100644--- a/crates/openhuman-tinyhumans/src/hosted/oauth/ops.rs+++ b/crates/openhuman-tinyhumans/src/hosted/oauth/ops.rs@@ -7,7 +7,7 @@use serde_json::{json, Value};use tinyhumans_sdk::api::types::IntegrationTokenRequest;-use openhuman_core::api::decrypt_handoff_blob;+use super::handoff::decrypt_handoff_blob;use openhuman_core::config::Config;use openhuman_core::rpc::RpcOutcome;