Skip to content

Commit 1829338

Browse files
authored
Merge pull request libgit2#5395 from josharian/http-use-eauth
Use error code GIT_EAUTH for authentication failures
2 parents 78cd762 + 9937967 commit 1829338

File tree

9 files changed

+27
-23
lines changed

9 files changed

+27
-23
lines changed

docs/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ with v0.28.0.
509509
The breaking change is that the `username` member of the underlying struct
510510
is now hidden, and a new `git_cred_get_username` function has been provided.
511511

512+
* Some errors of class `GIT_ERROR_NET` now have class `GIT_ERROR_HTTP`.
513+
Most authentication failures now have error code `GIT_EAUTH` instead of `GIT_ERROR`.
514+
512515
### Breaking CMake configuration changes
513516

514517
* The CMake option to use a system http-parser library, instead of the

include/git2/errors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ typedef enum {
4242
GIT_ECONFLICT = -13, /**< Checkout conflicts prevented operation */
4343
GIT_ELOCKED = -14, /**< Lock file prevented operation */
4444
GIT_EMODIFIED = -15, /**< Reference value does not match expected */
45-
GIT_EAUTH = -16, /**< Authentication error */
46-
GIT_ECERTIFICATE = -17, /**< Server certificate is invalid */
45+
GIT_EAUTH = -16, /**< Authentication error */
46+
GIT_ECERTIFICATE = -17, /**< Server certificate is invalid */
4747
GIT_EAPPLIED = -18, /**< Patch/merge has already been applied */
48-
GIT_EPEEL = -19, /**< The requested peel operation is not possible */
49-
GIT_EEOF = -20, /**< Unexpected EOF */
50-
GIT_EINVALID = -21, /**< Invalid operation or input */
48+
GIT_EPEEL = -19, /**< The requested peel operation is not possible */
49+
GIT_EEOF = -20, /**< Unexpected EOF */
50+
GIT_EINVALID = -21, /**< Invalid operation or input */
5151
GIT_EUNCOMMITTED = -22, /**< Uncommitted changes in index prevented operation */
52-
GIT_EDIRECTORY = -23, /**< The operation is not valid for a directory */
52+
GIT_EDIRECTORY = -23, /**< The operation is not valid for a directory */
5353
GIT_EMERGECONFLICT = -24, /**< A merge conflict exists and cannot continue */
5454

5555
GIT_PASSTHROUGH = -30, /**< A user-configured callback refused to act */

src/transports/auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int basic_next_token(
1818
{
1919
git_credential_userpass_plaintext *cred;
2020
git_buf raw = GIT_BUF_INIT;
21-
int error = -1;
21+
int error = GIT_EAUTH;
2222

2323
GIT_UNUSED(ctx);
2424

src/transports/auth_negotiate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static int negotiate_init_context(
267267

268268
if (!ctx->oid) {
269269
git_error_set(GIT_ERROR_NET, "negotiate authentication is not supported");
270-
return -1;
270+
return GIT_EAUTH;
271271
}
272272

273273
git_buf_puts(&ctx->target, "HTTP@");

src/transports/auth_ntlm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static int ntlm_next_token(
8585
git_buf input_buf = GIT_BUF_INIT;
8686
const unsigned char *msg;
8787
size_t challenge_len, msg_len;
88-
int error = -1;
88+
int error = GIT_EAUTH;
8989

9090
GIT_ASSERT_ARG(buf);
9191
GIT_ASSERT_ARG(ctx);

src/transports/http.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int handle_auth(
162162

163163
if (error > 0) {
164164
git_error_set(GIT_ERROR_HTTP, "%s authentication required but no callback set", server_type);
165-
error = -1;
165+
error = GIT_EAUTH;
166166
}
167167

168168
if (!error)
@@ -179,7 +179,7 @@ GIT_INLINE(int) handle_remote_auth(
179179

180180
if (response->server_auth_credtypes == 0) {
181181
git_error_set(GIT_ERROR_HTTP, "server requires authentication that we do not support");
182-
return -1;
182+
return GIT_EAUTH;
183183
}
184184

185185
/* Otherwise, prompt for credentials. */
@@ -201,7 +201,7 @@ GIT_INLINE(int) handle_proxy_auth(
201201

202202
if (response->proxy_auth_credtypes == 0) {
203203
git_error_set(GIT_ERROR_HTTP, "proxy requires authentication that we do not support");
204-
return -1;
204+
return GIT_EAUTH;
205205
}
206206

207207
/* Otherwise, prompt for credentials. */
@@ -259,7 +259,7 @@ static int handle_response(
259259
} else if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED ||
260260
response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
261261
git_error_set(GIT_ERROR_HTTP, "unexpected authentication failure");
262-
return -1;
262+
return GIT_EAUTH;
263263
}
264264

265265
if (response->status != GIT_HTTP_STATUS_OK) {
@@ -416,7 +416,7 @@ static int http_stream_read(
416416

417417
if (stream->state == HTTP_STATE_SENDING_REQUEST) {
418418
git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
419-
error = -1;
419+
error = GIT_ERROR; /* not GIT_EAUTH, because the exact cause is unclear */
420420
goto done;
421421
}
422422

@@ -554,7 +554,7 @@ static int http_stream_write(
554554
if (stream->state == HTTP_STATE_NONE) {
555555
git_error_set(GIT_ERROR_HTTP,
556556
"too many redirects or authentication replays");
557-
error = -1;
557+
error = GIT_ERROR; /* not GIT_EAUTH because the exact cause is unclear */
558558
goto done;
559559
}
560560

src/transports/httpclient.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ static int apply_credentials(
597597
free_auth_context(server);
598598
} else if (!token.size) {
599599
git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challenge");
600+
error = GIT_EAUTH;
600601
error = -1;
601602
goto done;
602603
}

src/transports/ssh.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,13 @@ static int request_creds(git_credential **out, ssh_subtransport *t, const char *
461461

462462
if (no_callback) {
463463
git_error_set(GIT_ERROR_SSH, "authentication required but no callback set");
464-
return -1;
464+
return GIT_EAUTH;
465465
}
466466

467467
if (!(cred->credtype & auth_methods)) {
468468
cred->free(cred);
469-
git_error_set(GIT_ERROR_SSH, "callback returned unsupported credentials type");
470-
return -1;
469+
git_error_set(GIT_ERROR_SSH, "authentication callback returned unsupported credentials type");
470+
return GIT_EAUTH;
471471
}
472472

473473
*out = cred;
@@ -840,7 +840,7 @@ static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *use
840840
/* either error, or the remote accepts NONE auth, which is bizarre, let's punt */
841841
if (list == NULL && !libssh2_userauth_authenticated(session)) {
842842
ssh_error(session, "Failed to retrieve list of SSH authentication methods");
843-
return -1;
843+
return GIT_EAUTH;
844844
}
845845

846846
ptr = list;

src/transports/winhttp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int apply_userpass_credentials(HINTERNET request, DWORD target, int mecha
154154
native_scheme = WINHTTP_AUTH_SCHEME_BASIC;
155155
} else {
156156
git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
157-
error = -1;
157+
error = GIT_EAUTH;
158158
goto done;
159159
}
160160

@@ -193,7 +193,7 @@ static int apply_default_credentials(HINTERNET request, DWORD target, int mechan
193193
native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
194194
} else {
195195
git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
196-
return -1;
196+
return GIT_EAUTH;
197197
}
198198

199199
/*
@@ -616,7 +616,7 @@ static int parse_unauthorized_response(
616616
*/
617617
if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
618618
git_error_set(GIT_ERROR_OS, "failed to parse supported auth schemes");
619-
return -1;
619+
return GIT_EAUTH;
620620
}
621621

622622
if (WINHTTP_AUTH_SCHEME_NTLM & supported) {
@@ -1040,7 +1040,7 @@ static int winhttp_stream_read(
10401040
/* Enforce a reasonable cap on the number of replays */
10411041
if (replay_count++ >= GIT_HTTP_REPLAY_MAX) {
10421042
git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
1043-
return -1;
1043+
return GIT_ERROR; /* not GIT_EAUTH because the exact cause is not clear */
10441044
}
10451045

10461046
/* Connect if necessary */

0 commit comments

Comments
 (0)