| 1 | # ABSTRACT: A libgit2 credential (passed back from acquire callbacks) | ||
| 2 | |||
| 3 | package Git::Native::Credential; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Git::Libgit2 qw( init_lib ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Git::Libgit2::FFI (); | ||
| 9 | use Git::Native::Error qw( check_rc ); | ||
| 10 | |||
| 11 | # Ensure libgit2 FFI is initialised before first use of this module. | ||
| 12 | init_lib(); | ||
| 13 | |||
| 14 | has _handle => ( is => 'rw', required => 1 ); | ||
| 15 | |||
| 16 | # Class-method constructors — each allocates a git_credential* via the | ||
| 17 | # matching libgit2 helper and wraps it. libgit2 takes ownership of the | ||
| 18 | # pointer once the acquire-callback returns 0; before that we own it. | ||
| 19 | |||
| 20 | sub userpass { | ||
| 21 | my ( $class, %args ) = @_; | ||
| 22 | my $user = $args{username} // Carp::croak "userpass: 'username' required"; | ||
| 23 | my $pass = $args{password} // Carp::croak "userpass: 'password' required"; | ||
| 24 | check_rc Git::Libgit2::FFI::git_credential_userpass_plaintext_new( | ||
| 25 | \my $cred, $user, $pass, | ||
| 26 | ); | ||
| 27 | return $class->new( _handle => $cred ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub ssh_key { | ||
| 31 | my ( $class, %args ) = @_; | ||
| 32 | my $user = $args{username} // Carp::croak "ssh_key: 'username' required"; | ||
| 33 | my $private_key = $args{private_key} // Carp::croak "ssh_key: 'private_key' required"; | ||
| 34 | my $public_key = $args{public_key}; # may be undef → libgit2 derives | ||
| 35 | my $passphrase = $args{passphrase} // ''; | ||
| 36 | check_rc Git::Libgit2::FFI::git_credential_ssh_key_new( | ||
| 37 | \my $cred, $user, $public_key, $private_key, $passphrase, | ||
| 38 | ); | ||
| 39 | return $class->new( _handle => $cred ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub ssh_agent { | ||
| 43 | my ( $class, %args ) = @_; | ||
| 44 | my $user = $args{username} // Carp::croak "ssh_agent: 'username' required"; | ||
| 45 | check_rc Git::Libgit2::FFI::git_credential_ssh_key_from_agent( | ||
| 46 | \my $cred, $user, | ||
| 47 | ); | ||
| 48 | return $class->new( _handle => $cred ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub default { | ||
| 52 | my ($class) = @_; | ||
| 53 | check_rc Git::Libgit2::FFI::git_credential_default_new( \my $cred ); | ||
| 54 | return $class->new( _handle => $cred ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub username { | ||
| 58 | my ( $class, %args ) = @_; | ||
| 59 | my $user = $args{username} // Carp::croak "username: 'username' required"; | ||
| 60 | check_rc Git::Libgit2::FFI::git_credential_username_new( \my $cred, $user ); | ||
| 61 | return $class->new( _handle => $cred ); | ||
| 62 | } | ||
| 63 | |||
| 64 | # Internal — called by the credential-acquire thunk after handing the | ||
| 65 | # pointer to libgit2. Prevents DEMOLISH from double-freeing. | ||
| 66 | sub _disown { | ||
| 67 | my $self = shift; | ||
| 68 | my $h = $self->_handle; | ||
| 69 | $self->_handle(undef); | ||
| 70 | return $h; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub DEMOLISH { | ||
| 74 | my $self = shift; | ||
| 75 | if ( my $h = $self->{_handle} ) { | ||
| 76 | Git::Libgit2::FFI::git_credential_free($h); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ | ||