Struct auth_git2::GitAuthenticator
source · pub struct GitAuthenticator { /* private fields */ }
Expand description
Configurable authenticator to use with git2
.
Implementations§
source§impl GitAuthenticator
impl GitAuthenticator
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new authenticator with all supported options enabled.
This is equavalent to:
GitAuthenticator::new_empty()
.try_cred_helper(true)
.try_password_prompt(3)
.add_default_username()
.try_ssh_agent(true)
.add_default_ssh_keys()
sourcepub fn new_empty() -> Self
pub fn new_empty() -> Self
Create a new authenticator with all authentication options disabled.
sourcepub fn add_plaintext_credentials(
self,
username: String,
password: String
) -> Self
pub fn add_plaintext_credentials( self, username: String, password: String ) -> Self
Add a username + password to try for authentication.
sourcepub fn try_cred_helper(self, enable: bool) -> Self
pub fn try_cred_helper(self, enable: bool) -> Self
Configure if the git credentials helper should be used.
See the git documentation of the credential.helper
configuration options for more details.
sourcepub fn try_password_prompt(self, max_count: u32) -> Self
pub fn try_password_prompt(self, max_count: u32) -> Self
Configure the number of times we should prompt the user for a username/password.
Set to 0
to disable.
sourcepub fn add_username(self, name: String) -> Self
pub fn add_username(self, name: String) -> Self
Add a username to try for authentication.
Some authentication mechanisms need a username, but not all valid git
URLs specify one.
You can add one or more usernames to try in that situation.
sourcepub fn add_default_username(self) -> Self
pub fn add_default_username(self) -> Self
Add the default username to try.
The default username if read from the USER
or USERNAME
environment variable.
sourcepub fn try_ssh_agent(self, enable: bool) -> Self
pub fn try_ssh_agent(self, enable: bool) -> Self
Configure if the SSH agent should be user for public key authentication.
sourcepub fn add_ssh_key_from_file(self, private_key: PathBuf) -> Self
pub fn add_ssh_key_from_file(self, private_key: PathBuf) -> Self
Add a private key to use for public key authentication.
The key will be read from disk by git2
, so it must still exist when the authentication is performed.
A matching .pub
file will also be read if it exists.
For example, if you add the private key "foo/my_ssh_id"
,
then "foo/my_ssh_id.pub"
will be used too, if it exists.
sourcepub fn add_default_ssh_keys(self) -> Self
pub fn add_default_ssh_keys(self) -> Self
Add all default SSH keys for public key authentication.
This will add all of the following files, if they exist:
"$HOME/.ssh/id_rsa"
"$HOME/.ssh"id_ecdsa,"
"$HOME/.ssh"id_ecdsa_sk"
"$HOME/.ssh"id_ed25519"
"$HOME/.ssh"id_ed25519_sk"
"$HOME/.ssh"id_dsa"
sourcepub fn run_operation<F, T>(
&self,
git_config: &Config,
user_operation: F
) -> Result<T, Error>where
F: FnMut(&mut Credentials<'_>) -> Result<T, Error>,
pub fn run_operation<F, T>( &self, git_config: &Config, user_operation: F ) -> Result<T, Error>where F: FnMut(&mut Credentials<'_>) -> Result<T, Error>,
Run a user operation with authentication.
The user operation is a callback that received a git2::Credentials
object (which is technically also a callback).
You should use the provided git2::Credentials
when calling git2
functions.
Note: We may need to call the user operation multiple times to try authentication with different usernames. You should ensure that the provided callback works correctly when called multiple times.
Example: Fetch from a remote with authentication
use auth_git2::GitAuthenticator;
let git_config = repo.config()?;
let mut remote = repo.find_remote("origin")?;
GitAuthenticator::default()
.run_operation(&git_config, |credentials| {
let mut remote_callbacks = git2::RemoteCallbacks::new();
remote_callbacks.credentials(credentials);
let mut fetch_options = git2::FetchOptions::new();
fetch_options.remote_callbacks(remote_callbacks);
remote.fetch(&["main"], Some(&mut fetch_options), None)
})?;
Trait Implementations§
source§impl Clone for GitAuthenticator
impl Clone for GitAuthenticator
source§fn clone(&self) -> GitAuthenticator
fn clone(&self) -> GitAuthenticator
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more