Skip to content

Commit f061959

Browse files
hickfordgitster
authored andcommitted
credential/wincred: store oauth_refresh_token
a5c7656 (credential: new attribute oauth_refresh_token) introduced a new confidential credential attribute and added support to credential-cache. Later 0ce02e2 (credential/libsecret: store new attributes, 2023-06-16) added support in credential-libsecret. To add support in credential-wincred, we encode the new attribute in the CredentialBlob, separated by newline: hunter2 oauth_refresh_token=xyzzy This is extensible and backwards compatible. The credential protocol already assumes that attribute values do not contain newlines. This fixes test "helper (wincred) gets oauth_refresh_token" when t0303-credential-external.sh is run with GIT_TEST_CREDENTIAL_HELPER=wincred. This test was added in a5c7656 (credential: new attribute oauth_refresh_token, 2023-04-21). Alternatives considered: store oauth_refresh_token in a wincred attribute. This would be insecure because wincred assumes attribute values to be non-confidential. Signed-off-by: M Hickford <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit f061959

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

contrib/credential/wincred/git-credential-wincred.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void *xmalloc(size_t size)
3535
}
3636

3737
static WCHAR *wusername, *password, *protocol, *host, *path, target[1024],
38-
*password_expiry_utc;
38+
*password_expiry_utc, *oauth_refresh_token;
3939

4040
static void write_item(const char *what, LPCWSTR wbuf, int wlen)
4141
{
@@ -128,6 +128,11 @@ static void get_credential(void)
128128
DWORD num_creds;
129129
int i;
130130
CREDENTIAL_ATTRIBUTEW *attr;
131+
WCHAR *secret;
132+
WCHAR *line;
133+
WCHAR *remaining_lines;
134+
WCHAR *part;
135+
WCHAR *remaining_parts;
131136

132137
if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
133138
return;
@@ -137,9 +142,24 @@ static void get_credential(void)
137142
if (match_cred(creds[i])) {
138143
write_item("username", creds[i]->UserName,
139144
creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
140-
write_item("password",
141-
(LPCWSTR)creds[i]->CredentialBlob,
142-
creds[i]->CredentialBlobSize / sizeof(WCHAR));
145+
if (creds[i]->CredentialBlobSize > 0) {
146+
secret = xmalloc(creds[i]->CredentialBlobSize);
147+
wcsncpy_s(secret, creds[i]->CredentialBlobSize, (LPCWSTR)creds[i]->CredentialBlob, creds[i]->CredentialBlobSize / sizeof(WCHAR));
148+
line = wcstok_s(secret, L"\r\n", &remaining_lines);
149+
write_item("password", line, line ? wcslen(line) : 0);
150+
while(line != NULL) {
151+
part = wcstok_s(line, L"=", &remaining_parts);
152+
if (!wcscmp(part, L"oauth_refresh_token")) {
153+
write_item("oauth_refresh_token", remaining_parts, remaining_parts ? wcslen(remaining_parts) : 0);
154+
}
155+
line = wcstok_s(NULL, L"\r\n", &remaining_lines);
156+
}
157+
free(secret);
158+
} else {
159+
write_item("password",
160+
(LPCWSTR)creds[i]->CredentialBlob,
161+
creds[i]->CredentialBlobSize / sizeof(WCHAR));
162+
}
143163
for (int j = 0; j < creds[i]->AttributeCount; j++) {
144164
attr = creds[i]->Attributes + j;
145165
if (!wcscmp(attr->Keyword, L"git_password_expiry_utc")) {
@@ -158,16 +178,26 @@ static void store_credential(void)
158178
{
159179
CREDENTIALW cred;
160180
CREDENTIAL_ATTRIBUTEW expiry_attr;
181+
WCHAR *secret;
182+
int wlen;
161183

162184
if (!wusername || !password)
163185
return;
164186

187+
if (oauth_refresh_token) {
188+
wlen = _scwprintf(L"%s\r\noauth_refresh_token=%s", password, oauth_refresh_token);
189+
secret = xmalloc(sizeof(WCHAR) * wlen);
190+
_snwprintf_s(secret, sizeof(WCHAR) * wlen, wlen, L"%s\r\noauth_refresh_token=%s", password, oauth_refresh_token);
191+
} else {
192+
secret = _wcsdup(password);
193+
}
194+
165195
cred.Flags = 0;
166196
cred.Type = CRED_TYPE_GENERIC;
167197
cred.TargetName = target;
168198
cred.Comment = L"saved by git-credential-wincred";
169-
cred.CredentialBlobSize = (wcslen(password)) * sizeof(WCHAR);
170-
cred.CredentialBlob = (LPVOID)password;
199+
cred.CredentialBlobSize = wcslen(secret) * sizeof(WCHAR);
200+
cred.CredentialBlob = (LPVOID)_wcsdup(secret);
171201
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
172202
cred.AttributeCount = 0;
173203
cred.Attributes = NULL;
@@ -182,6 +212,8 @@ static void store_credential(void)
182212
cred.TargetAlias = NULL;
183213
cred.UserName = wusername;
184214

215+
free(secret);
216+
185217
if (!CredWriteW(&cred, 0))
186218
die("CredWrite failed");
187219
}
@@ -253,6 +285,8 @@ static void read_credential(void)
253285
password = utf8_to_utf16_dup(v);
254286
else if (!strcmp(buf, "password_expiry_utc"))
255287
password_expiry_utc = utf8_to_utf16_dup(v);
288+
else if (!strcmp(buf, "oauth_refresh_token"))
289+
oauth_refresh_token = utf8_to_utf16_dup(v);
256290
/*
257291
* Ignore other lines; we don't know what they mean, but
258292
* this future-proofs us when later versions of git do

0 commit comments

Comments
 (0)