Skip to content

Commit d9351c6

Browse files
committed
examples: implement SSH key credentials
Implement SSH key credentials. This allows users to use the SSH transport with the lg2 example code.
1 parent e9aa847 commit d9351c6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/common.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,28 @@ int cred_acquire_cb(git_cred **out,
349349
unsigned int allowed_types,
350350
void *payload)
351351
{
352-
char *username = NULL, *password = NULL;
352+
char *username = NULL, *password = NULL, *privkey = NULL, *pubkey = NULL;
353353
int error = 1;
354354

355355
UNUSED(url);
356356
UNUSED(username_from_url);
357357
UNUSED(payload);
358358

359-
if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
359+
if (allowed_types & GIT_CREDTYPE_SSH_KEY) {
360+
int n;
361+
362+
if ((error = ask(&username, "Username:")) < 0 ||
363+
(error = ask(&privkey, "SSH Key:")) < 0 ||
364+
(error = ask(&password, "Password:")) < 0)
365+
goto out;
366+
367+
if ((n = snprintf(NULL, 0, "%s.pub", privkey)) < 0 ||
368+
(pubkey = malloc(n + 1)) == NULL ||
369+
(n = snprintf(pubkey, n + 1, "%s.pub", privkey)) < 0)
370+
goto out;
371+
372+
error = git_cred_ssh_key_new(out, username, pubkey, privkey, password);
373+
} else if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
360374
if ((error = ask(&username, "Username:")) < 0 ||
361375
(error = ask(&password, "Password:")) < 0)
362376
goto out;
@@ -372,5 +386,7 @@ int cred_acquire_cb(git_cred **out,
372386
out:
373387
free(username);
374388
free(password);
389+
free(privkey);
390+
free(pubkey);
375391
return error;
376392
}

0 commit comments

Comments
 (0)