Credentials callback is called endlessly until the server return Auth Ok (or a definitive error) If auth is denied, it up to us to return a new credential to try different auth method or an error to specify that we have exhausted everything we are able to provide
(
get_credentials: &impl Fn(&str) -> Vec<(CredentialType, Cred)>,
)
| 151 | // If auth is denied, it up to us to return a new credential to try different auth method |
| 152 | // or an error to specify that we have exhausted everything we are able to provide |
| 153 | fn authentication_callback( |
| 154 | get_credentials: &impl Fn(&str) -> Vec<(CredentialType, Cred)>, |
| 155 | ) -> impl FnMut(&str, Option<&str>, CredentialType) -> Result<Cred, Error> + '_ { |
| 156 | let mut current_credentials: (String, Vec<(CredentialType, Cred)>) = ("".into(), vec![]); |
| 157 | |
| 158 | move |remote_url, username_from_url, allowed_types| { |
| 159 | // If we have changed remote, reset our available auth methods |
| 160 | if remote_url != current_credentials.0 { |
| 161 | current_credentials = (remote_url.to_string(), get_credentials(username_from_url.unwrap_or("git"))); |
| 162 | } |
| 163 | let auth_methods = &mut current_credentials.1; |
| 164 | |
| 165 | // Try all the auth method until one match allowed_types |
| 166 | loop { |
| 167 | let (cred_type, credential) = match auth_methods.pop() { |
| 168 | Some(cred) => cred, |
| 169 | None => { |
| 170 | let msg = format!( |
| 171 | "Invalid authentication: Exhausted all available auth method to fetch repository {remote_url}" |
| 172 | ); |
| 173 | let mut error = Error::from_str(msg.as_str()); |
| 174 | error.set_code(Auth); |
| 175 | return Err(error); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | if allowed_types.contains(cred_type) { |
| 180 | return Ok(credential); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fn checkout<'a>(repo: &'a Repository, commit_id: &'a str) -> Result<Object<'a>, Error> { |
| 187 | let obj = repo.revparse_single(commit_id).map_err(|err| { |
no test coverage detected