1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! `rho login` and `rho acp --login`: signing in from a terminal.
//!
//! `rho login` is a client of the rho daemon, which saves the credential
//! and runs interactive logins for it (#1662), starting the daemon when
//! none runs (`daemon_client.rs`). `rho acp --login`, which an ACP host runs
//! in a terminal, signs in the same way. Before the first sign-in, it asks
//! which credential store to keep it in (`store_choice.rs`).
mod daemon_client;
pub(super) mod drive;
pub(super) mod relay_wire;
mod secret;
pub(super) mod store_choice;
use rho_providers::{
auth::login_dispatch::{AuthenticationError, AuthenticationMethod, ProviderAuthentication},
model::catalog,
provider::{provider_descriptor, resolve_auth_mode, ProviderAuthKind},
};
use crate::cli::Command;
/// A sign-in someone asked for from a terminal.
#[derive(Clone, Copy, Debug)]
pub(super) enum LoginCommand<'a> {
/// `rho login <provider>`, with `--device-auth`.
Cli {
provider: &'a str,
device_auth: bool,
},
/// `rho acp --login <auth>`, which an ACP host runs in a terminal and
/// whose zero exit status the host takes as signed in: an API key the
/// daemon checked and saved, or a login the provider confirmed. A login
/// whose approval nothing reports is refused (see
/// [`confirms_sign_in`]).
AcpTerminal { auth: &'a str },
}
/// The sign-in `command` asks for; `None` for every other command.
pub(super) fn command(command: &Option<Command>) -> Option<LoginCommand<'_>> {
match command {
Some(Command::Login {
provider,
device_auth,
}) => Some(LoginCommand::Cli {
provider,
device_auth: *device_auth,
}),
Some(Command::Acp(args)) => args
.login
.as_deref()
.map(|auth| LoginCommand::AcpTerminal { auth }),
_ => None,
}
}
/// Signs `command` in through the daemon, which saves the credential under
/// its own config.
pub(super) async fn run(command: LoginCommand<'_>) -> anyhow::Result<()> {
match command {
LoginCommand::Cli {
provider,
device_auth,
} => daemon_client::run(target(provider)?, device_auth).await,
LoginCommand::AcpTerminal { auth } => {
let target = target(auth)?;
if !confirms_sign_in(&target.auth) {
anyhow::bail!(
"'{auth}' signs in without reporting when the sign-in is approved, so an ACP host cannot run it; run `rho login {auth}` in a terminal instead"
);
}
daemon_client::run(target, /*device_auth*/ false).await
}
}
}
/// Whether the daemon's sign-in to auth profile `auth` finishes only once
/// the provider approved it, or a key it checked is saved. An Ollama Cloud
/// device key is saved as soon as the user is asked to approve it at
/// ollama.com, and nothing reports the approval, so an ACP host, which
/// takes a finished sign-in as signed in, does not offer it, and a TUI
/// following a daemon session asks for it only once the user says they
/// approved (`tui/daemon_login.rs`).
pub(crate) fn confirms_sign_in(auth: &str) -> bool {
!matches!(
resolve_auth_mode(auth).map(|(_, mode)| mode.auth_kind),
Some(ProviderAuthKind::OllamaDeviceKey { .. })
)
}
/// An auth profile that takes a credential.
struct Target {
/// The auth profile's id, such as `openai-codex`. The daemon looks up
/// the provider it signs in to itself.
auth: String,
sign_in: SignIn,
}
/// How a [`Target`] signs in.
#[derive(Clone, Copy)]
enum SignIn {
/// With an API key, read hidden from the terminal.
ApiKey { entry_label: &'static str },
/// With the provider's interactive login.
Interactive { provider_label: &'static str },
}
/// The auth profile `provider`, a provider or auth profile id, signs in to.
fn target(provider: &str) -> anyhow::Result<Target> {
if provider_descriptor(provider).is_some_and(|descriptor| descriptor.is_keyless()) {
anyhow::bail!("provider '{provider}' does not require login");
}
let Some(target) = catalog::login_target_for_provider(provider) else {
if let Err(error @ AuthenticationError::AmbiguousProvider { .. }) =
ProviderAuthentication::method(provider)
{
anyhow::bail!("{error}");
}
let options = catalog::login_targets()
.into_iter()
.map(|target| target.auth)
.collect::<Vec<_>>()
.join(", ");
anyhow::bail!("unsupported login provider '{provider}'. Use one of: {options}");
};
let sign_in = match ProviderAuthentication::method(&target.auth)? {
AuthenticationMethod::None => {
anyhow::bail!("provider '{provider}' does not require login")
}
AuthenticationMethod::ApiKey { entry_label } => SignIn::ApiKey { entry_label },
AuthenticationMethod::Interactive { provider_label } => {
SignIn::Interactive { provider_label }
}
};
Ok(Target {
auth: target.auth,
sign_in,
})
}
#[cfg(test)]
#[path = "login_tests.rs"]
mod tests;