Repository navigation
Expand file tree
/
Copy pathCredentialCacheStore.cs
More file actions
108 lines (87 loc) · 3.26 KB
/
Copy pathCredentialCacheStore.cs
File metadata and controls
108 lines (87 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
namespace GitCredentialManager
{
public class CredentialCacheStore : ICredentialStore
{
readonly IGit _git;
readonly string _options;
public CredentialCacheStore(IGit git, string options)
{
_git = git;
if (string.IsNullOrEmpty(options))
{
_options = string.Empty;
}
else
{
_options = options;
}
}
#region ICredentialStore
public string Name => "Git Credential Cache";
public IList<string> GetAccounts(string service)
{
// Listing accounts is not supported by the credential-cache store so we just attempt to retrieve
// the username from first credential for the given service and return an empty list if it fails.
var input = MakeGitCredentialsEntry(service, null);
var result = _git.InvokeHelperAsync(
$"credential-cache get {_options}",
input
).GetAwaiter().GetResult();
if (result.TryGetValue("username", out string value))
{
return new List<string> { value };
}
return Array.Empty<string>();
}
public ICredential Get(string service, string account)
{
var input = MakeGitCredentialsEntry(service, account);
var result = _git.InvokeHelperAsync(
$"credential-cache get {_options}",
input
).GetAwaiter().GetResult();
if (result.ContainsKey("username") && result.ContainsKey("password"))
{
return new GitCredential(result["username"], result["password"]);
}
return null;
}
public void AddOrUpdate(string service, string account, string secret)
{
var input = MakeGitCredentialsEntry(service, account);
input["password"] = secret;
// per https://git-scm.com/docs/gitcredentials :
// For a store or erase operation, the helper’s output is ignored.
_git.InvokeHelperAsync(
$"credential-cache store {_options}",
input
).GetAwaiter().GetResult();
}
public bool Remove(string service, string account)
{
var input = MakeGitCredentialsEntry(service, account);
// per https://git-scm.com/docs/gitcredentials :
// For a store or erase operation, the helper’s output is ignored.
_git.InvokeHelperAsync(
$"credential-cache erase {_options}",
input
).GetAwaiter().GetResult();
// the credential cache doesn't tell us whether anything was erased
// but we're optimistic sorts
return true;
}
#endregion
private Dictionary<string, string> MakeGitCredentialsEntry(string service, string account)
{
var result = new Dictionary<string, string>();
result["url"] = service;
if (!string.IsNullOrEmpty(account))
{
result["username"] = account;
}
return result;
}
}
}