Crate auth_git2

source ·
Expand description

Easy authentication for git2.

Authentication with git2 can be quite difficult to implement correctly. This crate aims to make it easy.

In the simplest case, you can create a GitAuthenticator struct and directly use it for authentication. By default, it will enable all supported authentication mechanisms. You can get a git2::Credentials callback for use with any git operation that requires authentication using the GitAuthenticator::credentials() function. Alternatively, you can use a utility function like GitAuthenticator::clone(), GitAuthenticator::fetch() or GitAuthenticator::push().

Features

  • Small dependency tree.
  • Query the SSH agent for private key authentication.
  • Get SSH keys from files.
  • Prompt the user for passwords for encrypted SSH keys. ** Only supported for OpenSSH private keys.
  • Query the git credential helper for usernames and passwords.
  • Use pre-provided plain usernames and passwords.
  • Use the git askpass helper to ask the user for credentials.
  • Fallback to prompting the user on the terminal if there is no askpass helper.

Example: Clone a repository with authentication

use auth_git2::GitAuthenticator;
use std::path::Path;

let auth = GitAuthenticator::default();
let git_config = git2::Config::open_default()?;
let mut repo_builder = git2::build::RepoBuilder::new();
let mut fetch_options = git2::FetchOptions::new();
let mut remote_callbacks = git2::RemoteCallbacks::new();

remote_callbacks.credentials(auth.credentials(&git_config));
fetch_options.remote_callbacks(remote_callbacks);
repo_builder.fetch_options(fetch_options);

let url = "https://github.com/de-vri-es/auth-git2-rs";
let into = Path::new("/tmp/dyfhxoaj/auth-git2-rs");
let mut repo = repo_builder.clone(url, into);

Structs