Skip to content

Commit 6a6d6fb

Browse files
bk2204gitster
authored andcommitted
credential: add a field for pre-encoded credentials
At the moment, our credential code wants to find a username and password for access, which, for HTTP, it will pass to libcurl to encode and process. However, many users want to use authentication schemes that libcurl doesn't support, such as Bearer authentication. In these schemes, the secret is not a username and password pair, but some sort of token that meets the production for authentication data in the RFC. In fact, in general, it's useful to allow our credential helper to have knowledge about what specifically to put in the protocol header. Thus, add a field, credential, which contains data that's preencoded to be suitable for the protocol in question. If we have such data, we need neither a username nor a password, so make that adjustment as well. It is in theory possible to reuse the password field for this. However, if we do so, we must know whether the credential helper supports our new scheme before sending it data, which necessitates some sort of capability inquiry, because otherwise an uninformed credential helper would store our preencoded data as a password, which would fail the next time we attempted to connect to the remote server. This design is substantially simpler, and we can hint to the credential helper that we support this approach with a simple new field instead of needing to query it first. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d01c76f commit 6a6d6fb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

credential.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void credential_clear(struct credential *c)
2525
free(c->path);
2626
free(c->username);
2727
free(c->password);
28+
free(c->credential);
2829
free(c->oauth_refresh_token);
2930
free(c->authtype);
3031
string_list_clear(&c->helpers, 0);
@@ -234,6 +235,9 @@ int credential_read(struct credential *c, FILE *fp)
234235
} else if (!strcmp(key, "password")) {
235236
free(c->password);
236237
c->password = xstrdup(value);
238+
} else if (!strcmp(key, "credential")) {
239+
free(c->credential);
240+
c->credential = xstrdup(value);
237241
} else if (!strcmp(key, "protocol")) {
238242
free(c->protocol);
239243
c->protocol = xstrdup(value);
@@ -291,6 +295,7 @@ void credential_write(const struct credential *c, FILE *fp)
291295
credential_write_item(fp, "path", c->path, 0);
292296
credential_write_item(fp, "username", c->username, 0);
293297
credential_write_item(fp, "password", c->password, 0);
298+
credential_write_item(fp, "credential", c->credential, 0);
294299
credential_write_item(fp, "oauth_refresh_token", c->oauth_refresh_token, 0);
295300
if (c->password_expiry_utc != TIME_MAX) {
296301
char *s = xstrfmt("%"PRItime, c->password_expiry_utc);
@@ -366,7 +371,7 @@ void credential_fill(struct credential *c)
366371
{
367372
int i;
368373

369-
if (c->username && c->password)
374+
if ((c->username && c->password) || c->credential)
370375
return;
371376

372377
credential_apply_config(c);
@@ -379,15 +384,15 @@ void credential_fill(struct credential *c)
379384
/* Reset expiry to maintain consistency */
380385
c->password_expiry_utc = TIME_MAX;
381386
}
382-
if (c->username && c->password)
387+
if ((c->username && c->password) || c->credential)
383388
return;
384389
if (c->quit)
385390
die("credential helper '%s' told us to quit",
386391
c->helpers.items[i].string);
387392
}
388393

389394
credential_getpass(c);
390-
if (!c->username && !c->password)
395+
if (!c->username && !c->password && !c->credential)
391396
die("unable to get password from user");
392397
}
393398

@@ -397,7 +402,7 @@ void credential_approve(struct credential *c)
397402

398403
if (c->approved)
399404
return;
400-
if (!c->username || !c->password || c->password_expiry_utc < time(NULL))
405+
if (((!c->username || !c->password) && !c->credential) || c->password_expiry_utc < time(NULL))
401406
return;
402407

403408
credential_apply_config(c);
@@ -418,6 +423,7 @@ void credential_reject(struct credential *c)
418423

419424
FREE_AND_NULL(c->username);
420425
FREE_AND_NULL(c->password);
426+
FREE_AND_NULL(c->credential);
421427
FREE_AND_NULL(c->oauth_refresh_token);
422428
c->password_expiry_utc = TIME_MAX;
423429
c->approved = 0;

credential.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ struct credential {
138138

139139
char *username;
140140
char *password;
141+
char *credential;
141142
char *protocol;
142143
char *host;
143144
char *path;

0 commit comments

Comments
 (0)