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
//! Whether the conversation model's auth was chosen or filled in, and the
//! auth a session on it starts on.
//!
//! An auth someone chose (`model.auth` in `config.toml`, `--auth`, an
//! agent's pin) is the one a session starts on, signed in or not, so a
//! client asks to sign in to it. An auth nothing chose is only the
//! provider's default; a session starts instead on the provider's auth
//! that has credentials ([`ConversationModel::settle_inferred_auth`]), which every
//! starting point applies: a new session, a draft, a child, a reopen.
use rho_providers::{
model::catalog::SelectionAuthContext,
provider::{self, ProviderDescriptor},
};
use super::ConversationModel;
/// Where [`ConversationModel::auth`] came from.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum AuthOrigin {
/// Someone chose it: a session starts on it even without credentials.
Configured,
/// Filled in because nothing chose one: the provider's default, until
/// a session settles it on the auth with credentials.
#[default]
Inferred,
}
impl ConversationModel {
/// The auth `config.toml` keeps: the chosen one, or none when it was
/// filled in, so a write never pins an auth nobody chose.
pub(crate) fn configured_auth(&self) -> Option<String> {
match self.auth_origin {
AuthOrigin::Configured => Some(self.auth.clone()),
AuthOrigin::Inferred => None,
}
}
/// Runs on `auth`, chosen, which sessions start on as it is.
pub(crate) fn choose_auth(&mut self, auth: impl Into<String>) {
self.auth = auth.into();
self.auth_origin = AuthOrigin::Configured;
}
/// Runs on `auth`, filled in for want of a choice.
pub(crate) fn infer_auth(&mut self, auth: impl Into<String>) {
self.auth = auth.into();
self.auth_origin = AuthOrigin::Inferred;
}
/// Moves an inferred auth to the one a session starts on with
/// `available`, the auths that have credentials (see
/// [`provider_auth_with_credentials`]). A chosen auth stays.
pub(crate) fn settle_inferred_auth(&mut self, available: &[String]) {
if self.auth_origin == AuthOrigin::Inferred {
self.auth = settled_inferred_auth(&self.provider, &self.auth, available);
}
}
}
/// The auth `provider`'s inferred `auth` moves to with `available`, the
/// auths that have credentials (see [`provider_auth_with_credentials`]).
pub(crate) fn settled_inferred_auth(provider: &str, auth: &str, available: &[String]) -> String {
match provider::provider_descriptor(provider) {
Some(descriptor) => {
provider_auth_with_credentials(descriptor, Some(auth), available).into()
}
None => auth.to_owned(),
}
}
/// The auth `descriptor` runs on when nothing chose one: of the provider's
/// modes in `available_auths`, the one the selection rules pick with
/// `preferred_auth`, if any, first, so a stored key wins over keyless
/// access; the provider's default mode when none is available.
pub(crate) fn provider_auth_with_credentials(
descriptor: &'static ProviderDescriptor,
preferred_auth: Option<&str>,
available_auths: &[String],
) -> &'static str {
let modes: Vec<String> = descriptor
.auth_modes()
.map(|mode| mode.id.to_string())
.collect();
let selected = SelectionAuthContext {
current: preferred_auth,
available: available_auths,
}
.select(&modes);
descriptor
.auth_mode(&selected)
.unwrap_or_else(|| descriptor.default_auth())
.id
}
#[cfg(test)]
#[path = "config_auth_tests.rs"]
mod tests;