Merge branch 'mh/credential-oauth-refresh-token-with-wincred'

The wincred credential backend has been taught to support oauth refresh
token the same way as credential-cache and credential-libsecret backends.

* mh/credential-oauth-refresh-token-with-wincred:
  credential/wincred: store oauth_refresh_token
This commit is contained in:
Junio C Hamano 2024-02-08 13:20:34 -08:00
Родитель 6dbc1eb664 f061959efd
Коммит bec9160394
1 изменённых файлов: 40 добавлений и 6 удалений

Просмотреть файл

@ -35,7 +35,7 @@ static void *xmalloc(size_t size)
}
static WCHAR *wusername, *password, *protocol, *host, *path, target[1024],
*password_expiry_utc;
*password_expiry_utc, *oauth_refresh_token;
static void write_item(const char *what, LPCWSTR wbuf, int wlen)
{
@ -140,6 +140,11 @@ static void get_credential(void)
DWORD num_creds;
int i;
CREDENTIAL_ATTRIBUTEW *attr;
WCHAR *secret;
WCHAR *line;
WCHAR *remaining_lines;
WCHAR *part;
WCHAR *remaining_parts;
if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
return;
@ -149,9 +154,24 @@ static void get_credential(void)
if (match_cred(creds[i], 0)) {
write_item("username", creds[i]->UserName,
creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
if (creds[i]->CredentialBlobSize > 0) {
secret = xmalloc(creds[i]->CredentialBlobSize);
wcsncpy_s(secret, creds[i]->CredentialBlobSize, (LPCWSTR)creds[i]->CredentialBlob, creds[i]->CredentialBlobSize / sizeof(WCHAR));
line = wcstok_s(secret, L"\r\n", &remaining_lines);
write_item("password", line, line ? wcslen(line) : 0);
while(line != NULL) {
part = wcstok_s(line, L"=", &remaining_parts);
if (!wcscmp(part, L"oauth_refresh_token")) {
write_item("oauth_refresh_token", remaining_parts, remaining_parts ? wcslen(remaining_parts) : 0);
}
line = wcstok_s(NULL, L"\r\n", &remaining_lines);
}
free(secret);
} else {
write_item("password",
(LPCWSTR)creds[i]->CredentialBlob,
creds[i]->CredentialBlobSize / sizeof(WCHAR));
}
for (int j = 0; j < creds[i]->AttributeCount; j++) {
attr = creds[i]->Attributes + j;
if (!wcscmp(attr->Keyword, L"git_password_expiry_utc")) {
@ -170,16 +190,26 @@ static void store_credential(void)
{
CREDENTIALW cred;
CREDENTIAL_ATTRIBUTEW expiry_attr;
WCHAR *secret;
int wlen;
if (!wusername || !password)
return;
if (oauth_refresh_token) {
wlen = _scwprintf(L"%s\r\noauth_refresh_token=%s", password, oauth_refresh_token);
secret = xmalloc(sizeof(WCHAR) * wlen);
_snwprintf_s(secret, sizeof(WCHAR) * wlen, wlen, L"%s\r\noauth_refresh_token=%s", password, oauth_refresh_token);
} else {
secret = _wcsdup(password);
}
cred.Flags = 0;
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = target;
cred.Comment = L"saved by git-credential-wincred";
cred.CredentialBlobSize = (wcslen(password)) * sizeof(WCHAR);
cred.CredentialBlob = (LPVOID)password;
cred.CredentialBlobSize = wcslen(secret) * sizeof(WCHAR);
cred.CredentialBlob = (LPVOID)_wcsdup(secret);
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
cred.AttributeCount = 0;
cred.Attributes = NULL;
@ -194,6 +224,8 @@ static void store_credential(void)
cred.TargetAlias = NULL;
cred.UserName = wusername;
free(secret);
if (!CredWriteW(&cred, 0))
die("CredWrite failed");
}
@ -265,6 +297,8 @@ static void read_credential(void)
password = utf8_to_utf16_dup(v);
else if (!strcmp(buf, "password_expiry_utc"))
password_expiry_utc = utf8_to_utf16_dup(v);
else if (!strcmp(buf, "oauth_refresh_token"))
oauth_refresh_token = utf8_to_utf16_dup(v);
/*
* Ignore other lines; we don't know what they mean, but
* this future-proofs us when later versions of git do