Skip to content

Commit 356169c

Browse files
committed
Merge branch 'sc/http-late-auth' into maint
* sc/http-late-auth: Prompt for a username when an HTTP request 401s
2 parents 3b3b9a6 + 42653c0 commit 356169c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

http.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,21 @@ static int http_request(const char *url, void *result, int target, int options)
816816
ret = HTTP_OK;
817817
else if (missing_target(&results))
818818
ret = HTTP_MISSING_TARGET;
819-
else
819+
else if (results.http_code == 401) {
820+
if (user_name) {
821+
ret = HTTP_NOAUTH;
822+
} else {
823+
/*
824+
* git_getpass is needed here because its very likely stdin/stdout are
825+
* pipes to our parent process. So we instead need to use /dev/tty,
826+
* but that is non-portable. Using git_getpass() can at least be stubbed
827+
* on other platforms with a different implementation if/when necessary.
828+
*/
829+
user_name = xstrdup(git_getpass("Username: "));
830+
init_curl_http_auth(slot->curl);
831+
ret = HTTP_REAUTH;
832+
}
833+
} else
820834
ret = HTTP_ERROR;
821835
} else {
822836
error("Unable to start HTTP request for %s", url);
@@ -832,7 +846,11 @@ static int http_request(const char *url, void *result, int target, int options)
832846

833847
int http_get_strbuf(const char *url, struct strbuf *result, int options)
834848
{
835-
return http_request(url, result, HTTP_REQUEST_STRBUF, options);
849+
int http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
850+
if (http_ret == HTTP_REAUTH) {
851+
http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
852+
}
853+
return http_ret;
836854
}
837855

838856
/*

http.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ extern void end_url_with_slash(struct strbuf *buf, const char *url);
127127
#define HTTP_MISSING_TARGET 1
128128
#define HTTP_ERROR 2
129129
#define HTTP_START_FAILED 3
130+
#define HTTP_REAUTH 4
131+
#define HTTP_NOAUTH 5
130132

131133
/*
132134
* Requests an url and stores the result in a strbuf.

remote-curl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ static struct discovery* discover_refs(const char *service)
132132
case HTTP_MISSING_TARGET:
133133
die("%s not found: did you run git update-server-info on the"
134134
" server?", refs_url);
135+
case HTTP_NOAUTH:
136+
die("Authentication failed");
135137
default:
136138
http_error(refs_url, http_ret);
137139
die("HTTP request failed");

0 commit comments

Comments
 (0)