Skip to content

Commit 216165e

Browse files
committed
transports: use GIT_EAUTH for authentication failures
When the failure is clearly an auth failure (as opposed to possibly an auth failure), use the error code GIT_EAUTH instead of GIT_ERROR. While we're here, fix a typo and improve an error message. Fixes libgit2#5389.
1 parent d4d26e8 commit 216165e

File tree

8 files changed

+20
-19
lines changed

8 files changed

+20
-19
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ v0.28 + 1
6767
is now hidden, and a new `git_cred_get_username` function has been provided.
6868

6969
* Some errors of class `GIT_ERROR_NET` now have class `GIT_ERROR_HTTP`.
70+
Most authentication failures now have error code `GIT_EAUTH` instead of `GIT_ERROR`.
7071

7172
### Breaking CMake configuration changes
7273

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
@@ -260,7 +260,7 @@ static int negotiate_init_context(
260260

261261
if (!ctx->oid) {
262262
git_error_set(GIT_ERROR_NET, "negotiate authentication is not supported");
263-
return -1;
263+
return GIT_EAUTH;
264264
}
265265

266266
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
@@ -84,7 +84,7 @@ static int ntlm_next_token(
8484
git_buf input_buf = GIT_BUF_INIT;
8585
const unsigned char *msg;
8686
size_t challenge_len, msg_len;
87-
int error = -1;
87+
int error = GIT_EAUTH;
8888

8989
assert(buf && ctx && ctx->ntlm);
9090

src/transports/http.c

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

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

165165
if (!error)
@@ -176,7 +176,7 @@ GIT_INLINE(int) handle_remote_auth(
176176

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

182182
/* Otherwise, prompt for credentials. */
@@ -198,7 +198,7 @@ GIT_INLINE(int) handle_proxy_auth(
198198

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

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

262262
if (response->status != GIT_HTTP_STATUS_OK) {
@@ -413,7 +413,7 @@ static int http_stream_read(
413413

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

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

src/transports/httpclient.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ static int apply_credentials(
585585
if (auth->connection_affinity)
586586
free_auth_context(server);
587587
} else if (!token.size) {
588-
git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challange");
589-
error = -1;
588+
git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challenge");
589+
error = GIT_EAUTH;
590590
goto done;
591591
}
592592

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;
@@ -805,7 +805,7 @@ static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *use
805805
/* either error, or the remote accepts NONE auth, which is bizarre, let's punt */
806806
if (list == NULL && !libssh2_userauth_authenticated(session)) {
807807
ssh_error(session, "Failed to retrieve list of SSH authentication methods");
808-
return -1;
808+
return GIT_EAUTH;
809809
}
810810

811811
ptr = list;

src/transports/winhttp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int apply_userpass_credentials(HINTERNET request, DWORD target, int mecha
146146
native_scheme = WINHTTP_AUTH_SCHEME_BASIC;
147147
} else {
148148
git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
149-
error = -1;
149+
error = GIT_EAUTH;
150150
goto done;
151151
}
152152

@@ -185,7 +185,7 @@ static int apply_default_credentials(HINTERNET request, DWORD target, int mechan
185185
native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
186186
} else {
187187
git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
188-
return -1;
188+
return GIT_EAUTH;
189189
}
190190

191191
/*
@@ -602,7 +602,7 @@ static int parse_unauthorized_response(
602602
*/
603603
if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
604604
git_error_set(GIT_ERROR_OS, "failed to parse supported auth schemes");
605-
return -1;
605+
return GIT_EAUTH;
606606
}
607607

608608
if (WINHTTP_AUTH_SCHEME_NTLM & supported) {
@@ -972,7 +972,7 @@ static int winhttp_stream_read(
972972
/* Enforce a reasonable cap on the number of replays */
973973
if (replay_count++ >= GIT_HTTP_REPLAY_MAX) {
974974
git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
975-
return -1;
975+
return GIT_ERROR; /* not GIT_EAUTH because the exact cause is not clear */
976976
}
977977

978978
/* Connect if necessary */

0 commit comments

Comments
 (0)