From 3e85efe786e25e9d83a12699fa76ff3526f2e941 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Wed, 25 Nov 2026 22:43:34 -0500 Subject: [PATCH] feat(honcho): list the sign-in in Accounts and keep it as an account The resolver sign-in never appeared in a client's Accounts pages, so there was no visible way to sign in. Declare a static service login instead, save its credential as a Honcho account, refresh it through credentials.renew so the rotation lands on that account, and keep reading a saved honcho section when no account is signed in. --- extensions/honcho/src/albedo_ext_honcho.gleam | 2 +- .../honcho/src/albedo_ext_honcho/config.gleam | 27 +++-- .../honcho/src/albedo_ext_honcho/oauth.gleam | 102 +++++++++++++++--- .../src/albedo_ext_honcho/sign_in.gleam | 52 ++------- .../honcho/test/albedo_ext_honcho_test.gleam | 49 +++++---- robot-docs/honcho.md | 13 +-- 6 files changed, 152 insertions(+), 93 deletions(-) diff --git a/extensions/honcho/src/albedo_ext_honcho.gleam b/extensions/honcho/src/albedo_ext_honcho.gleam index 403081a2a..bb12343b5 100644 --- a/extensions/honcho/src/albedo_ext_honcho.gleam +++ b/extensions/honcho/src/albedo_ext_honcho.gleam @@ -92,7 +92,7 @@ fn definition( [], [ extension.SettingsPlugin(config.declaration()), - extension.LoginResolverPlugin(sign_in.resolve, sign_in.keep), + extension.LoginPlugin(sign_in.login()), extension.StartupPlugin(start), ..delivery.plugins() ], diff --git a/extensions/honcho/src/albedo_ext_honcho/config.gleam b/extensions/honcho/src/albedo_ext_honcho/config.gleam index d7e879d6e..8d2824842 100644 --- a/extensions/honcho/src/albedo_ext_honcho/config.gleam +++ b/extensions/honcho/src/albedo_ext_honcho/config.gleam @@ -162,24 +162,33 @@ fn valid_bytes(bytes: BitArray) -> Bool { /// A multi-request operation can outlive a configuration change. Check the /// destination after loading the credential so a queued request for the old -/// account cannot take the new account's credential. A stored OAuth grant -/// refreshes its access token here; a static `apiKey` is used as saved. +/// account cannot take the new account's credential. A signed-in account is +/// used first; otherwise the saved `honcho` section of creds.json. A stored +/// OAuth grant refreshes its access token here; a static `apiKey` is used as +/// saved. pub fn api_key(dest: Destination) -> Result(String, String) { - let missing = "Set honcho.apiKey in creds.json to a valid bearer key" - use stored <- result.try( - credentials.service(settings.home(), "honcho", oauth.decoder()) - |> result.replace_error(missing), - ) + let missing = + "Sign in to Honcho or set honcho.apiKey in creds.json to a valid bearer key" use current <- result.try(discord()) let valid = case dest.audience { Operator -> current == dest Discord -> current.endpoint == dest.endpoint && current.workspace == dest.workspace } - use key <- result.try(case valid { + use _ <- result.try(case valid { + True -> Ok(Nil) False -> Error("Honcho destination changed") - True -> oauth.access(settings.home(), dest.endpoint, stored) }) + let home = settings.home() + let key = case credentials.accounts(home, "honcho", oauth.decoder()) { + Ok([account, ..]) -> oauth.account_access(account, dest.endpoint) + _ -> + case credentials.service(home, "honcho", oauth.decoder()) { + Ok(stored) -> oauth.access(home, dest.endpoint, stored) + Error(_) -> Error(missing) + } + } + use key <- result.try(key) case key != "" && profiles.valid_api_key(key) { True -> Ok(key) False -> Error(missing) diff --git a/extensions/honcho/src/albedo_ext_honcho/oauth.gleam b/extensions/honcho/src/albedo_ext_honcho/oauth.gleam index 2f9fa850c..65f1af676 100644 --- a/extensions/honcho/src/albedo_ext_honcho/oauth.gleam +++ b/extensions/honcho/src/albedo_ext_honcho/oauth.gleam @@ -149,6 +149,19 @@ fn exchange( stored: Stored, grant: Grant, ) -> Result(String, String) { + use next <- result.map(exchange_tokens(endpoint, stored, grant)) + persist(home, next) + next.access +} + +/// Exchange the grant's refresh token for a new credential, without saving +/// it. Callers that hold the credential as an account let `credentials.renew` +/// save the result; the saved-section path persists it itself. +fn exchange_tokens( + endpoint: String, + stored: Stored, + grant: Grant, +) -> Result(Stored, String) { let url = case grant.token_endpoint { "" -> endpoint <> "/oauth/token" saved -> saved @@ -160,27 +173,90 @@ fn exchange( ] case http.post_form(url, [], fields, buffered.Timeouts(15_000, 15_000)) { Ok(#(status, body)) if status >= 200 && status < 300 -> - case renewed(stored, body, now_seconds()) { - Ok(next) -> { - persist(home, next) - Ok(next.access) - } - Error(reason) -> Error(reason) - } + renewed(stored, body, now_seconds()) Ok(#(status, body)) -> case credentials.refused(status, body) { - True -> - Error( - "Honcho sign-in was refused; sign in again and update the honcho credentials in creds.json", - ) + True -> Error("Honcho sign-in was refused; sign in again") False -> - transient( + transient_stored( stored, grant, "Honcho returned HTTP " <> int.to_string(status), ) } - Error(reason) -> transient(stored, grant, reason) + Error(reason) -> transient_stored(stored, grant, reason) + } +} + +fn transient_stored( + stored: Stored, + grant: Grant, + reason: String, +) -> Result(Stored, String) { + case transient(stored, grant, reason) { + Ok(_) -> Ok(stored) + Error(reason) -> Error(reason) + } +} + +/// The bearer token for a signed-in account, refreshing through +/// `credentials.renew` when its access token is stale so the rotation is +/// saved back onto that account. +pub fn account_access( + account: credentials.Stored(Stored), + endpoint: String, +) -> Result(String, String) { + let stored = credentials.value(account) + case stored.grant { + None -> Ok(stored.access) + Some(grant) -> + case fresh(stored.access, grant, now_seconds()) { + True -> Ok(stored.access) + False -> { + let renewed = + credentials.renew( + account, + decoder(), + fn(_) { "honcho" }, + patch, + fn(current) { + case current.grant { + None -> Ok(current) + Some(current_grant) -> + case fresh(current.access, current_grant, now_seconds()) { + // Another refresh landed while this one waited. + True -> Ok(current) + False -> exchange_tokens(endpoint, current, current_grant) + } + } + }, + ) + use updated <- result.map(renewed) + credentials.value(updated).access + } + } + } +} + +/// The fields `credentials.renew` merges onto a saved account. +pub fn patch(stored: Stored) -> json.Json { + case stored.grant { + Some(grant) -> + json.object([ + #("apiKey", json.string(stored.access)), + #( + "oauth", + json.object([ + #("refreshToken", json.string(grant.refresh_token)), + #("expiresAt", json.int(grant.expires_at)), + #("clientId", json.string(grant.client_id)), + #("tokenEndpoint", json.string(grant.token_endpoint)), + #("scope", json.string(grant.scope)), + #("tokenType", json.string(grant.token_type)), + ]), + ), + ]) + None -> json.object([#("apiKey", json.string(stored.access))]) } } diff --git a/extensions/honcho/src/albedo_ext_honcho/sign_in.gleam b/extensions/honcho/src/albedo_ext_honcho/sign_in.gleam index 876e20e21..8c1e126e6 100644 --- a/extensions/honcho/src/albedo_ext_honcho/sign_in.gleam +++ b/extensions/honcho/src/albedo_ext_honcho/sign_in.gleam @@ -2,54 +2,23 @@ //// `hermes-agent` for every surface, and its authorization server offers a //// device grant, so a daemon on another host can sign in: the client shows //// a code, the person confirms it in their browser, and the daemon polls -//// for the grant. The completed credential is kept under `honcho` in -//// creds.json in the shape the OAuth module refreshes, not as a provider -//// account. +//// for the grant. The sign-in is a listed `LoginPlugin`, so the Accounts +//// pages offer it like other service sign-ins. Its credential is saved as +//// an account under `honcho`, carrying the fields the OAuth module +//// refreshes. import albedo_clock import albedo_ext_honcho/config import albedo_ext_honcho/oauth as honcho_auth import albedo_sdk/oauth -import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} -import gleam/dynamic/decode -import gleam/list -import gleam/option.{type Option, None, Some} +import gleam/option.{None} import gleam/result const client_id = "hermes-agent" -/// The sign-in for `provider` when it is Honcho's. -pub fn resolve( - _home: String, - provider: String, -) -> Option(Result(oauth.Login, String)) { - case provider { - "honcho" -> Some(Ok(login())) - _ -> None - } -} - -/// creds.json with a completed Honcho sign-in's credential in place of the -/// saved `honcho` section. Other sections are kept. -pub fn keep(creds: Dynamic, provider: String, credential: Dynamic) -> Dynamic { - case provider { - "honcho" -> { - let document = - decode.run(creds, decode.dict(decode.string, decode.dynamic)) - |> result.unwrap(dict.new()) - let section = - decode.run(credential, decode.dict(decode.string, decode.dynamic)) - |> result.unwrap(dict.new()) - document - |> dict.insert("honcho", to_dynamic(section)) - |> to_dynamic - } - _ -> creds - } -} - -fn login() -> oauth.Login { +/// The Honcho sign-in offered in client Accounts pages. +pub fn login() -> oauth.Login { oauth.Login( "honcho", "sign in to honcho", @@ -101,10 +70,3 @@ fn account(_credential: Dynamic) -> oauth.Account { } oauth.account("honcho", "Honcho", detail, False) } - -fn to_dynamic(values: Dict(String, Dynamic)) -> Dynamic { - values - |> dict.to_list - |> list.map(fn(entry) { #(dynamic.string(entry.0), entry.1) }) - |> dynamic.properties -} diff --git a/extensions/honcho/test/albedo_ext_honcho_test.gleam b/extensions/honcho/test/albedo_ext_honcho_test.gleam index ba169690d..dfaccd67c 100644 --- a/extensions/honcho/test/albedo_ext_honcho_test.gleam +++ b/extensions/honcho/test/albedo_ext_honcho_test.gleam @@ -9,6 +9,7 @@ import gleam/dynamic/decode import gleam/json import gleam/list import gleam/option.{None, Some} +import gleam/string import gleeunit import sqlight @@ -84,29 +85,39 @@ pub fn oauth_granted_builds_a_refreshable_credential_test() -> Nil { Nil } -pub fn sign_in_keep_replaces_the_honcho_section_test() -> Nil { - let creds = - dynamic.properties([ - #(dynamic.string("other"), dynamic.string("kept")), - #( - dynamic.string("honcho"), - dynamic.properties([ - #(dynamic.string("apiKey"), dynamic.string("hch-at-old")), - ]), - ), - ]) +pub fn sign_in_is_a_listed_service_login_test() -> Nil { + let login = sign_in.login() + let assert "honcho" = login.provider + let assert "honcho" = login.store + // The credential a sign-in saves carries a stable account identity, so + // signing in again replaces the same account. let credential = dynamic.properties([ #(dynamic.string("apiKey"), dynamic.string("hch-at-new")), ]) - let kept = sign_in.keep(creds, "honcho", credential) - let assert Ok(section) = - decode.run(kept, decode.at(["honcho", "apiKey"], decode.string)) - let assert "hch-at-new" = section - let assert Ok(other) = decode.run(kept, decode.at(["other"], decode.string)) - let assert "kept" = other - // Other providers are left alone. - let assert True = sign_in.keep(creds, "other", credential) == creds + let account = login.account(credential) + let assert "honcho" = account.id + let assert "Honcho" = account.label + Nil +} + +pub fn oauth_patch_carries_the_rotated_grant_test() -> Nil { + let stored = + oauth.Stored( + "hch-at-new", + Some(oauth.Grant( + "hch-rt-new", + 8600, + "hermes-agent", + "https://api.honcho.dev/oauth/token", + "write", + "Bearer", + )), + ) + let text = json.to_string(oauth.patch(stored)) + let assert True = string.contains(text, "hch-at-new") + let assert True = string.contains(text, "hch-rt-new") + let assert True = string.contains(text, "8600") Nil } diff --git a/robot-docs/honcho.md b/robot-docs/honcho.md index c7164093f..36cea560c 100644 --- a/robot-docs/honcho.md +++ b/robot-docs/honcho.md @@ -23,12 +23,13 @@ self-hosted server to use it in place of the default hosted endpoint. } ``` -Sign in from an Albedo client with the Honcho sign-in, or store a bearer key -under `honcho.apiKey` in `creds.json` by hand. Sign-in uses Honcho's device -grant: the client shows a code, you confirm it in your browser, and the daemon -saves its own grant under `honcho` in `creds.json`. The -[sign-in module](../extensions/honcho/src/albedo_ext_honcho/sign_in.gleam) names -that sign-in for the daemon's sign-in service. The +Sign in to Honcho from a client's Accounts page, or store a bearer key under +`honcho.apiKey` in `creds.json` by hand. Sign-in uses Honcho's device grant: the +client shows a code, you confirm it in your browser, and the daemon saves its +own grant as a Honcho account. The +[sign-in module](../extensions/honcho/src/albedo_ext_honcho/sign_in.gleam) +declares that sign-in. A saved `honcho` section is still read when no account is +signed in. The [OAuth module](../extensions/honcho/src/albedo_ext_honcho/oauth.gleam) refreshes the access token before it expires, writes the rotated grant back under the settings lock, and reports a refused grant as a paused delivery. Refresh tokens -- knotmirror 0.0.1