summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-04-16 11:05:37 +0200
committerPatrick Steinhardt <ps@pks.im>2019-04-16 11:58:13 +0200
commitd9351c6556fce79efd6f39e122e9e7ef04e32cf9 (patch)
tree1af9e9e8a104813ce9ac3050f15dfd50a12e993b /examples
parente9aa84799a6417d9404dc72599d04b205d8ff98b (diff)
downloadlibgit2-d9351c6556fce79efd6f39e122e9e7ef04e32cf9.tar.gz
examples: implement SSH key credentials
Implement SSH key credentials. This allows users to use the SSH transport with the lg2 example code.
Diffstat (limited to 'examples')
-rw-r--r--examples/common.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/examples/common.c b/examples/common.c
index aae9f97c1..e90538c4c 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -349,14 +349,28 @@ int cred_acquire_cb(git_cred **out,
unsigned int allowed_types,
void *payload)
{
- char *username = NULL, *password = NULL;
+ char *username = NULL, *password = NULL, *privkey = NULL, *pubkey = NULL;
int error = 1;
UNUSED(url);
UNUSED(username_from_url);
UNUSED(payload);
- if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
+ if (allowed_types & GIT_CREDTYPE_SSH_KEY) {
+ int n;
+
+ if ((error = ask(&username, "Username:")) < 0 ||
+ (error = ask(&privkey, "SSH Key:")) < 0 ||
+ (error = ask(&password, "Password:")) < 0)
+ goto out;
+
+ if ((n = snprintf(NULL, 0, "%s.pub", privkey)) < 0 ||
+ (pubkey = malloc(n + 1)) == NULL ||
+ (n = snprintf(pubkey, n + 1, "%s.pub", privkey)) < 0)
+ goto out;
+
+ error = git_cred_ssh_key_new(out, username, pubkey, privkey, password);
+ } else if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
if ((error = ask(&username, "Username:")) < 0 ||
(error = ask(&password, "Password:")) < 0)
goto out;
@@ -372,5 +386,7 @@ int cred_acquire_cb(git_cred **out,
out:
free(username);
free(password);
+ free(privkey);
+ free(pubkey);
return error;
}