pub struct GitAuthenticator { /* private fields */ }
Expand description

Configurable authenticator to use with git2.

Implementations§

source§

impl GitAuthenticator

source

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()
source

pub fn new_empty() -> Self

Create a new authenticator with all authentication options disabled.

source

pub fn add_plaintext_credentials( self, username: String, password: String ) -> Self

Add a username + password to try for authentication.

source

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.

source

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.

source

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.

source

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.

source

pub fn try_ssh_agent(self, enable: bool) -> Self

Configure if the SSH agent should be user for public key authentication.

source

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.

source

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"
source

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

source§

fn clone(&self) -> GitAuthenticator

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for GitAuthenticator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for GitAuthenticator

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.