| 1 | use crate::{helper, helper::Cascade, protocol, protocol::Context, Program}; |
| 2 | |
| 3 | impl Default for Cascade { |
| 4 | fn default() -> Self { |
| 5 | Cascade { |
| 6 | programs: Vec::new(), |
| 7 | stderr: true, |
| 8 | use_http_path: false, |
| 9 | query_user_only: false, |
| 10 | } |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | /// Initialization |
| 15 | impl Cascade { |
| 16 | /// Return the programs to run for the current platform. |
| 17 | /// |
| 18 | /// These are typically used as basis for all credential cascade invocations, with configured programs following afterwards. |
| 19 | /// |
| 20 | /// # Note |
| 21 | /// |
| 22 | /// These defaults emulate what typical git installations may use these days, as in fact it's a configurable which comes |
| 23 | /// from installation-specific configuration files which we cannot know (or guess at best). |
| 24 | /// This seems like an acceptable trade-off as helpers are ignored if they fail or are not existing. |
| 25 | pub fn platform_builtin() -> Vec<Program> { |
| 26 | if cfg!(target_os = "macos") { |
| 27 | Some("osxkeychain") |
| 28 | } else if cfg!(target_os = "linux") { |
| 29 | Some("libsecret") |
| 30 | } else if cfg!(target_os = "windows") { |
| 31 | Some("manager-core") |
| 32 | } else { |
| 33 | None |
| 34 | } |
| 35 | .map(|name| vec![Program::from_custom_definition(name)]) |
| 36 | .unwrap_or_default() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Builder |
| 41 | impl Cascade { |
| 42 | /// Extend the list of programs to run `programs`. |
| 43 | pub fn extend(mut self, programs: impl IntoIterator<Item = Program>) -> Self { |
| 44 | self.programs.extend(programs); |
| 45 | self |
| 46 | } |
| 47 | /// If `toggle` is true, http(s) urls will use the path portions of the url to obtain a credential for. |
| 48 | /// |
| 49 | /// Otherwise, they will only take the user name into account. |
| 50 | pub fn use_http_path(mut self, toggle: bool) -> Self { |
| 51 | self.use_http_path = toggle; |
| 52 | self |
| 53 | } |
| 54 | |
| 55 | /// If `toggle` is true, a bogus password will be provided to prevent any helper program from prompting for it, nor will |
| 56 | /// we prompt for the password. The resulting identity will have a bogus password and it's expected to not be used by the |
| 57 | /// consuming transport. |
| 58 | pub fn query_user_only(mut self, toggle: bool) -> Self { |
| 59 | self.query_user_only = toggle; |
| 60 | self |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Finalize |
| 65 | impl Cascade { |
| 66 | /// Invoke the cascade by `invoking` each program with `action`, and configuring potential prompts with `prompt` options. |
| 67 | /// The latter can also be used to disable the prompt entirely when setting the `mode` to [`Disable`][gix_prompt::Mode::Disable];=. |
| 68 | /// |
| 69 | /// When _getting_ credentials, all programs are asked until the credentials are complete, stopping the cascade. |
| 70 | /// When _storing_ or _erasing_ all programs are instructed in order. |
| 71 | #[allow(clippy::result_large_err)] |
| 72 | pub fn invoke(&mut self, mut action: helper::Action, mut prompt: gix_prompt::Options<'_>) -> protocol::Result { |
| 73 | let mut url = action |
| 74 | .context_mut() |
| 75 | .map(|ctx| { |
| 76 | #[allow(clippy::manual_inspect)] /* false positive */ |
| 77 | ctx.destructure_url_in_place(self.use_http_path).map(|ctx| { |
| 78 | if self.query_user_only && ctx.password.is_none() { |
| 79 | ctx.password = Some("".into()); |
| 80 | } |
| 81 | ctx |
| 82 | }) |
| 83 | }) |
| 84 | .transpose()? |
| 85 | .and_then(|ctx| ctx.url.take()); |
| 86 | |
| 87 | for program in &mut self.programs { |
| 88 | program.stderr = self.stderr; |
| 89 | match helper::invoke::raw(program, &action) { |
| 90 | Ok(None) => {} |
| 91 | Ok(Some(stdout)) => { |
| 92 | let Context { |
| 93 | protocol, |
| 94 | host, |
| 95 | path, |
| 96 | username, |
| 97 | password, |
| 98 | oauth_refresh_token, |
| 99 | password_expiry_utc, |
| 100 | url: ctx_url, |
| 101 | quit, |
| 102 | } = Context::from_bytes(&stdout)?; |
| 103 | if let Some(dst_ctx) = action.context_mut() { |
| 104 | if let Some(src) = path { |
| 105 | dst_ctx.path = Some(src); |
| 106 | } |
| 107 | if let Some(src) = password_expiry_utc { |
| 108 | dst_ctx.password_expiry_utc = Some(src); |
| 109 | } |
| 110 | for (src, dst) in [ |
| 111 | (protocol, &mut dst_ctx.protocol), |
| 112 | (host, &mut dst_ctx.host), |
| 113 | (username, &mut dst_ctx.username), |
| 114 | (password, &mut dst_ctx.password), |
| 115 | (oauth_refresh_token, &mut dst_ctx.oauth_refresh_token), |
| 116 | ] { |
| 117 | if let Some(src) = src { |
| 118 | *dst = Some(src); |
| 119 | } |
| 120 | } |
| 121 | if let Some(src) = ctx_url { |
| 122 | dst_ctx.url = Some(src); |
| 123 | url = dst_ctx.destructure_url_in_place(self.use_http_path)?.url.take(); |
| 124 | } |
| 125 | if dst_ctx |
| 126 | .password_expiry_utc |
| 127 | .is_some_and(|expiry_date| expiry_date < gix_date::Time::now_utc().seconds) |
| 128 | { |
| 129 | dst_ctx.password_expiry_utc = None; |
| 130 | dst_ctx.clear_secrets(); |
| 131 | } |
| 132 | if dst_ctx.username.is_some() && dst_ctx.password.is_some() { |
| 133 | break; |
| 134 | } |
| 135 | if quit.unwrap_or_default() { |
| 136 | dst_ctx.quit = quit; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | Err(helper::Error::CredentialsHelperFailed { .. }) => continue, // ignore helpers that we can't call |
| 142 | Err(err) if action.context().is_some() => return Err(err.into()), // communication errors are fatal when getting credentials |
| 143 | Err(_) => {} // for other actions, ignore everything, try the operation |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if prompt.mode != gix_prompt::Mode::Disable { |
| 148 | if let Some(ctx) = action.context_mut() { |
| 149 | ctx.url = url; |
| 150 | if ctx.username.is_none() { |
| 151 | let message = ctx.to_prompt("Username"); |
| 152 | prompt.mode = gix_prompt::Mode::Visible; |
| 153 | ctx.username = gix_prompt::ask(&message, &prompt) |
| 154 | .map_err(|err| protocol::Error::Prompt { |
| 155 | prompt: message, |
| 156 | source: err, |
| 157 | })? |
| 158 | .into(); |
| 159 | } |
| 160 | if ctx.password.is_none() { |
| 161 | let message = ctx.to_prompt("Password"); |
| 162 | prompt.mode = gix_prompt::Mode::Hidden; |
| 163 | ctx.password = gix_prompt::ask(&message, &prompt) |
| 164 | .map_err(|err| protocol::Error::Prompt { |
| 165 | prompt: message, |
| 166 | source: err, |
| 167 | })? |
| 168 | .into(); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | protocol::helper_outcome_to_result( |
| 174 | action.context().map(|ctx| helper::Outcome { |
| 175 | username: ctx.username.clone(), |
| 176 | password: ctx.password.clone(), |
| 177 | oauth_refresh_token: ctx.oauth_refresh_token.clone(), |
| 178 | quit: ctx.quit.unwrap_or(false), |
| 179 | next: ctx.to_owned().into(), |
| 180 | }), |
| 181 | action, |
| 182 | ) |
| 183 | } |
| 184 | } |