Skip to content

Commit 488d9d5

Browse files
hickfordgitster
authored andcommitted
credential/wincred: store password_expiry_utc
This attribute is important when storing OAuth credentials which may expire after as little as one hour. d208bfd (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27d43aa commit 488d9d5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static void load_cred_funcs(void)
9191
die("failed to load functions");
9292
}
9393

94-
static WCHAR *wusername, *password, *protocol, *host, *path, target[1024];
94+
static WCHAR *wusername, *password, *protocol, *host, *path, target[1024],
95+
*password_expiry_utc;
9596

9697
static void write_item(const char *what, LPCWSTR wbuf, int wlen)
9798
{
@@ -183,6 +184,7 @@ static void get_credential(void)
183184
CREDENTIALW **creds;
184185
DWORD num_creds;
185186
int i;
187+
CREDENTIAL_ATTRIBUTEW *attr;
186188

187189
if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
188190
return;
@@ -195,6 +197,14 @@ static void get_credential(void)
195197
write_item("password",
196198
(LPCWSTR)creds[i]->CredentialBlob,
197199
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+
}
198208
break;
199209
}
200210

@@ -204,6 +214,7 @@ static void get_credential(void)
204214
static void store_credential(void)
205215
{
206216
CREDENTIALW cred;
217+
CREDENTIAL_ATTRIBUTEW expiry_attr;
207218

208219
if (!wusername || !password)
209220
return;
@@ -217,6 +228,14 @@ static void store_credential(void)
217228
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
218229
cred.AttributeCount = 0;
219230
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+
}
220239
cred.TargetAlias = NULL;
221240
cred.UserName = wusername;
222241

@@ -278,6 +297,8 @@ static void read_credential(void)
278297
wusername = utf8_to_utf16_dup(v);
279298
} else if (!strcmp(buf, "password"))
280299
password = utf8_to_utf16_dup(v);
300+
else if (!strcmp(buf, "password_expiry_utc"))
301+
password_expiry_utc = utf8_to_utf16_dup(v);
281302
/*
282303
* Ignore other lines; we don't know what they mean, but
283304
* this future-proofs us when later versions of git do
@@ -292,7 +313,7 @@ int main(int argc, char *argv[])
292313
"usage: git credential-wincred <get|store|erase>\n";
293314

294315
if (!argv[1])
295-
die(usage);
316+
die("%s", usage);
296317

297318
/* git use binary pipes to avoid CRLF-issues */
298319
_setmode(_fileno(stdin), _O_BINARY);

0 commit comments

Comments
 (0)