Skip to content

Commit 8237102

Browse files
committed
ssh key creds
1 parent e9bcab4 commit 8237102

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

git2_cred.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ typedef struct _git2_cred_object {
1111
git_cred *cred;
1212
} git2_cred_object_t;
1313

14+
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_default, 0, 0, 0)
15+
ZEND_END_ARG_INFO()
16+
17+
static PHP_METHOD(Cred, create_default) {
18+
if (zend_parse_parameters_none() == FAILURE) return;
19+
git2_cred_object_t *intern;
20+
21+
object_init_ex(return_value, php_git2_cred_ce);
22+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
23+
int res = git_cred_default_new(&intern->cred);
24+
25+
if (res != 0) {
26+
git2_throw_last_error();
27+
return;
28+
}
29+
}
30+
1431
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_plaintext, 0, 0, 2)
1532
ZEND_ARG_INFO(0, username)
1633
ZEND_ARG_INFO(0, password)
@@ -34,6 +51,75 @@ static PHP_METHOD(Cred, create_plaintext) {
3451
}
3552
}
3653

54+
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_ssh_key, 0, 0, 3)
55+
ZEND_ARG_INFO(0, username)
56+
ZEND_ARG_INFO(0, publickey_file)
57+
ZEND_ARG_INFO(0, privatekey_file)
58+
ZEND_ARG_INFO(0, passphrase)
59+
ZEND_END_ARG_INFO()
60+
61+
static PHP_METHOD(Cred, create_ssh_key) {
62+
char *username, *publickey_file, *privatekey_file, *passphrase = NULL;
63+
size_t username_len, publickey_file_len, privatekey_file_len, passphrase_len;
64+
git2_cred_object_t *intern;
65+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|s", &username, &username_len, &publickey_file, &publickey_file_len, &privatekey_file, &privatekey_file_len, &passphrase, &passphrase_len) == FAILURE)
66+
return;
67+
68+
object_init_ex(return_value, php_git2_cred_ce);
69+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
70+
int res = git_cred_ssh_key_new(&intern->cred, username, publickey_file, privatekey_file, passphrase);
71+
72+
if (res != 0) {
73+
git2_throw_last_error();
74+
return;
75+
}
76+
}
77+
78+
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_ssh_key_memory, 0, 0, 3)
79+
ZEND_ARG_INFO(0, username)
80+
ZEND_ARG_INFO(0, publickey)
81+
ZEND_ARG_INFO(0, privatekey)
82+
ZEND_ARG_INFO(0, passphrase)
83+
ZEND_END_ARG_INFO()
84+
85+
static PHP_METHOD(Cred, create_ssh_key_memory) {
86+
char *username, *publickey, *privatekey, *passphrase = NULL;
87+
size_t username_len, publickey_len, privatekey_len, passphrase_len;
88+
git2_cred_object_t *intern;
89+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|s", &username, &username_len, &publickey, &publickey_len, &privatekey, &privatekey_len, &passphrase, &passphrase_len) == FAILURE)
90+
return;
91+
92+
object_init_ex(return_value, php_git2_cred_ce);
93+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
94+
int res = git_cred_ssh_key_memory_new(&intern->cred, username, publickey, privatekey, passphrase);
95+
96+
if (res != 0) {
97+
git2_throw_last_error();
98+
return;
99+
}
100+
}
101+
102+
ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_ssh_key_from_agent, 0, 0, 1)
103+
ZEND_ARG_INFO(0, username)
104+
ZEND_END_ARG_INFO()
105+
106+
static PHP_METHOD(Cred, create_ssh_key_from_agent) {
107+
char *username;
108+
size_t username_len;
109+
git2_cred_object_t *intern;
110+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &username, &username_len) == FAILURE)
111+
return;
112+
113+
object_init_ex(return_value, php_git2_cred_ce);
114+
intern = (git2_cred_object_t*)Z_OBJ_P(return_value);
115+
int res = git_cred_ssh_key_from_agent(&intern->cred, username);
116+
117+
if (res != 0) {
118+
git2_throw_last_error();
119+
return;
120+
}
121+
}
122+
37123
#define GIT2_CRED_FETCH() git2_cred_object_t *intern = (git2_cred_object_t*)Z_OBJ_P(getThis()); \
38124
if (intern->cred == NULL) { \
39125
git2_throw_exception(0 TSRMLS_CC, "Git2\\Cred object in invalid state"); \
@@ -88,7 +174,11 @@ static void php_git2_cred_free_object(zend_object *object TSRMLS_DC) {
88174
#define PHP_GIT2_CRED_ME_P(_x) PHP_ME(Cred, _x, arginfo_cred_##_x, ZEND_ACC_PUBLIC)
89175

90176
static zend_function_entry git2_cred_methods[] = {
177+
PHP_ME(Cred, create_default, arginfo_cred_create_default, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
91178
PHP_ME(Cred, create_plaintext, arginfo_cred_create_plaintext, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
179+
PHP_ME(Cred, create_ssh_key, arginfo_cred_create_ssh_key, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
180+
PHP_ME(Cred, create_ssh_key_memory, arginfo_cred_create_ssh_key_memory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
181+
PHP_ME(Cred, create_ssh_key_from_agent, arginfo_cred_create_ssh_key_from_agent, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
92182
PHP_GIT2_CRED_ME_P(has_username)
93183
/* PHP_ME(Cred, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */
94184
{ NULL, NULL, NULL }

0 commit comments

Comments
 (0)