credential/wincred: store password_expiry_utc

This attribute is important when storing OAuth credentials which may expire after as little as one hour. d208bfdf (credential: new attribute password_expiry_utc, 2023-02-18) added support for this attribute in general so that individual credential backend like wincred can use it. Signed-off-by: M Hickford <mirth.hickford@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

M Hickford committed Apr 3, 2023 at 07:47 UTC 488d9d52be1e100be01447d9168193287dbbfbbf
1 file changed +23 -2
contrib/credential/wincred/git-credential-wincred.c
+23 -2
@@ -91,7 +91,8 @@ static void load_cred_funcs(void)
91 die("failed to load functions");
92 }
93
94 -static WCHAR *wusername, *password, *protocol, *host, *path, target[1024];
94 +static WCHAR *wusername, *password, *protocol, *host, *path, target[1024],
95 + *password_expiry_utc;
96
97 static void write_item(const char *what, LPCWSTR wbuf, int wlen)
98 {
@@ -183,6 +184,7 @@ static void get_credential(void)
184 CREDENTIALW **creds;
185 DWORD num_creds;
186 int i;
187 + CREDENTIAL_ATTRIBUTEW *attr;
188
189 if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
190 return;
@@ -195,6 +197,14 @@ static void get_credential(void)
197 write_item("password",
198 (LPCWSTR)creds[i]->CredentialBlob,
199 creds[i]->CredentialBlobSize / sizeof(WCHAR));
200 + for (int j = 0; j < creds[i]->AttributeCount; j++) {
201 + attr = creds[i]->Attributes + j;
202 + if (!wcscmp(attr->Keyword, L"git_password_expiry_utc")) {
203 + write_item("password_expiry_utc", (LPCWSTR)attr->Value,
204 + attr->ValueSize / sizeof(WCHAR));
205 + break;
206 + }
207 + }
208 break;
209 }
210
@@ -204,6 +214,7 @@ static void get_credential(void)
214 static void store_credential(void)
215 {
216 CREDENTIALW cred;
217 + CREDENTIAL_ATTRIBUTEW expiry_attr;
218
219 if (!wusername || !password)
220 return;
@@ -217,6 +228,14 @@ static void store_credential(void)
228 cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
229 cred.AttributeCount = 0;
230 cred.Attributes = NULL;
231 + if (password_expiry_utc != NULL) {
232 + expiry_attr.Keyword = L"git_password_expiry_utc";
233 + expiry_attr.Value = (LPVOID)password_expiry_utc;
234 + expiry_attr.ValueSize = (wcslen(password_expiry_utc)) * sizeof(WCHAR);
235 + expiry_attr.Flags = 0;
236 + cred.Attributes = &expiry_attr;
237 + cred.AttributeCount = 1;
238 + }
239 cred.TargetAlias = NULL;
240 cred.UserName = wusername;
241
@@ -278,6 +297,8 @@ static void read_credential(void)
297 wusername = utf8_to_utf16_dup(v);
298 } else if (!strcmp(buf, "password"))
299 password = utf8_to_utf16_dup(v);
300 + else if (!strcmp(buf, "password_expiry_utc"))
301 + password_expiry_utc = utf8_to_utf16_dup(v);
302 /*
303 * Ignore other lines; we don't know what they mean, but
304 * this future-proofs us when later versions of git do
@@ -292,7 +313,7 @@ int main(int argc, char *argv[])
313 "usage: git credential-wincred <get|store|erase>\n";
314
315 if (!argv[1])
295 - die(usage);
316 + die("%s", usage);
317
318 /* git use binary pipes to avoid CRLF-issues */
319 _setmode(_fileno(stdin), _O_BINARY);