Commit 635ec366 by Patrick Steinhardt

examples: honor allowed credential types when prompting user

Credential callback are being passed a bitset that indicates
which credential types are allowed in the current context. In our
examples code, we completely ignore that field and always return
username/password credentials, which doesn't necessarily make
sense e.g. when only SSH keys are allowed.

Refactor the code and only return username/password credentials
in the case where `USERPASS_PLAINTEXT` credentials are allowed.
Otherwise, return a positive error code to indicate that no
credentials could be acquired.
parent b106620d
...@@ -330,6 +330,19 @@ error: ...@@ -330,6 +330,19 @@ error:
return error; return error;
} }
static int ask(char **out, const char *prompt)
{
printf("%s ", prompt);
fflush(stdout);
if (!readline(out)) {
fprintf(stderr, "Could not read response: %s", strerror(errno));
return -1;
}
return 0;
}
int cred_acquire_cb(git_cred **out, int cred_acquire_cb(git_cred **out,
const char *url, const char *url,
const char *username_from_url, const char *username_from_url,
...@@ -337,31 +350,22 @@ int cred_acquire_cb(git_cred **out, ...@@ -337,31 +350,22 @@ int cred_acquire_cb(git_cred **out,
void *payload) void *payload)
{ {
char *username = NULL, *password = NULL; char *username = NULL, *password = NULL;
int error; int error = 1;
UNUSED(url); UNUSED(url);
UNUSED(username_from_url); UNUSED(username_from_url);
UNUSED(allowed_types);
UNUSED(payload); UNUSED(payload);
printf("Username: "); if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
if (readline(&username) < 0) { if ((error = ask(&username, "Username:")) < 0 ||
fprintf(stderr, "Unable to read username: %s", strerror(errno)); (error = ask(&password, "Password:")) < 0)
return -1; goto out;
}
/* Yup. Right there on your terminal. Careful where you copy/paste output. */ error = git_cred_userpass_plaintext_new(out, username, password);
printf("Password: ");
if (readline(&password) < 0) {
fprintf(stderr, "Unable to read password: %s", strerror(errno));
free(username);
return -1;
} }
error = git_cred_userpass_plaintext_new(out, username, password); out:
free(username); free(username);
free(password); free(password);
return error; return error;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment