Skip to content

Commit 070b4dd

Browse files
Michael J Grubergitster
authored andcommitted
http: use hostname in credential description
Until now, a request for an http password looked like: Username: Password: Now it will look like: Username for 'example.com': Password for 'example.com': Picked-from: Jeff King <[email protected]> Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d677ed commit 070b4dd

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

http.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static long curl_low_speed_time = -1;
4242
static int curl_ftp_no_epsv;
4343
static const char *curl_http_proxy;
4444
static const char *curl_cookie_file;
45-
static char *user_name, *user_pass;
45+
static char *user_name, *user_pass, *description;
4646
static const char *user_agent;
4747

4848
#if LIBCURL_VERSION_NUM >= 0x071700
@@ -139,6 +139,27 @@ static void process_curl_messages(void)
139139
}
140140
#endif
141141

142+
static char *git_getpass_with_description(const char *what, const char *desc)
143+
{
144+
struct strbuf prompt = STRBUF_INIT;
145+
char *r;
146+
147+
if (desc)
148+
strbuf_addf(&prompt, "%s for '%s': ", what, desc);
149+
else
150+
strbuf_addf(&prompt, "%s: ", what);
151+
/*
152+
* NEEDSWORK: for usernames, we should do something less magical that
153+
* actually echoes the characters. However, we need to read from
154+
* /dev/tty and not stdio, which is not portable (but getpass will do
155+
* it for us). http.c uses the same workaround.
156+
*/
157+
r = git_getpass(prompt.buf);
158+
159+
strbuf_release(&prompt);
160+
return xstrdup(r);
161+
}
162+
142163
static int http_options(const char *var, const char *value, void *cb)
143164
{
144165
if (!strcmp("http.sslverify", var)) {
@@ -214,7 +235,7 @@ static void init_curl_http_auth(CURL *result)
214235
if (user_name) {
215236
struct strbuf up = STRBUF_INIT;
216237
if (!user_pass)
217-
user_pass = xstrdup(git_getpass("Password: "));
238+
user_pass = xstrdup(git_getpass_with_description("Password", description));
218239
strbuf_addf(&up, "%s:%s", user_name, user_pass);
219240
curl_easy_setopt(result, CURLOPT_USERPWD,
220241
strbuf_detach(&up, NULL));
@@ -229,7 +250,7 @@ static int has_cert_password(void)
229250
return 0;
230251
/* Only prompt the user once. */
231252
ssl_cert_password_required = -1;
232-
ssl_cert_password = git_getpass("Certificate Password: ");
253+
ssl_cert_password = git_getpass_with_description("Certificate Password", description);
233254
if (ssl_cert_password != NULL) {
234255
ssl_cert_password = xstrdup(ssl_cert_password);
235256
return 1;
@@ -307,7 +328,7 @@ static CURL *get_curl_handle(void)
307328

308329
static void http_auth_init(const char *url)
309330
{
310-
char *at, *colon, *cp, *slash;
331+
const char *at, *colon, *cp, *slash, *host;
311332

312333
cp = strstr(url, "://");
313334
if (!cp)
@@ -323,16 +344,22 @@ static void http_auth_init(const char *url)
323344
at = strchr(cp, '@');
324345
colon = strchr(cp, ':');
325346
slash = strchrnul(cp, '/');
326-
if (!at || slash <= at)
327-
return; /* No credentials */
328-
if (!colon || at <= colon) {
347+
if (!at || slash <= at) {
348+
/* No credentials, but we may have to ask for some later */
349+
host = cp;
350+
}
351+
else if (!colon || at <= colon) {
329352
/* Only username */
330353
user_name = url_decode_mem(cp, at - cp);
331354
user_pass = NULL;
355+
host = at + 1;
332356
} else {
333357
user_name = url_decode_mem(cp, colon - cp);
334358
user_pass = url_decode_mem(colon + 1, at - (colon + 1));
359+
host = at + 1;
335360
}
361+
362+
description = url_decode_mem(host, slash - host);
336363
}
337364

338365
static void set_from_env(const char **var, const char *envname)
@@ -828,7 +855,7 @@ static int http_request(const char *url, void *result, int target, int options)
828855
* but that is non-portable. Using git_getpass() can at least be stubbed
829856
* on other platforms with a different implementation if/when necessary.
830857
*/
831-
user_name = xstrdup(git_getpass("Username: "));
858+
user_name = xstrdup(git_getpass_with_description("Username", description));
832859
init_curl_http_auth(slot->curl);
833860
ret = HTTP_REAUTH;
834861
}

t/t5550-http-fetch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ test_expect_success 'setup askpass helpers' '
5151
GIT_ASKPASS="$PWD/askpass" &&
5252
export GIT_ASKPASS &&
5353
>askpass-expect-none &&
54-
echo "askpass: Password: " >askpass-expect-pass &&
55-
{ echo "askpass: Username: " &&
54+
echo "askpass: Password for '\''$HTTPD_DEST'\'': " >askpass-expect-pass &&
55+
{ echo "askpass: Username for '\''$HTTPD_DEST'\'': " &&
5656
cat askpass-expect-pass
5757
} >askpass-expect-both
5858
'

0 commit comments

Comments
 (0)