Skip to content

Commit

Permalink
authenticate using ssh agent
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriakosoikonomakos authored and ashb committed Sep 15, 2014
1 parent 51da3b7 commit 3e87ada
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pygit2/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ def credential_tuple(self):

def __call__(self, _url, _username, _allowed):
return self


class KeypairFromAgent(Keypair):
def __init__(self, username):
super(KeypairFromAgent, self).__init__(username, None, None, None)
3 changes: 3 additions & 0 deletions pygit2/decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ int git_cred_ssh_key_new(
const char *publickey,
const char *privatekey,
const char *passphrase);
int git_cred_ssh_key_from_agent(
git_cred **out,
const char *username);

/*
* git_diff
Expand Down
13 changes: 9 additions & 4 deletions pygit2/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from _pygit2 import Oid
from .errors import check_error, GitError
from .ffi import ffi, C
from .credentials import KeypairFromAgent
from .refspec import Refspec
from .utils import to_bytes, strarray_to_strings, strings_to_strarray

Expand Down Expand Up @@ -455,10 +456,14 @@ def get_credentials(fn, url, username, allowed):
to_bytes(passwd))

elif cred_type == C.GIT_CREDTYPE_SSH_KEY:
name, pubkey, privkey, passphrase = creds.credential_tuple
err = C.git_cred_ssh_key_new(ccred, to_bytes(name), to_bytes(pubkey),
to_bytes(privkey), to_bytes(passphrase))

if isinstance(creds, KeypairFromAgent):
username = creds.credential_tuple[0]
err = C.git_cred_ssh_key_from_agent(ccred, to_bytes(username))
else:
name, pubkey, privkey, passphrase = creds.credential_tuple
err = C.git_cred_ssh_key_new(ccred, to_bytes(name),
to_bytes(pubkey), to_bytes(privkey),
to_bytes(passphrase))
else:
raise TypeError("unsupported credential type")

Expand Down
8 changes: 8 additions & 0 deletions test/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import unittest
import pygit2
from pygit2 import GIT_CREDTYPE_USERPASS_PLAINTEXT
from pygit2 import UserPass, Keypair, KeypairFromAgent
from pygit2 import UserPass, Keypair
from . import utils

Expand Down Expand Up @@ -60,6 +61,13 @@ def test_ssh_key(self):
cred = Keypair(username, pubkey, privkey, passphrase)
self.assertEqual((username, pubkey, privkey, passphrase), cred.credential_tuple)

def test_ssh_agent(self):
username = "git"

cred = KeypairFromAgent(username)
self.assertEqual((username, None, None, None), cred.credential_tuple)


class CredentialCallback(utils.RepoTestCase):
def test_callback(self):
def credentials_cb(url, username, allowed):
Expand Down

0 comments on commit 3e87ada

Please sign in to comment.