Repository navigation
Expand file tree
/
Copy pathGitRequest.cs
More file actions
245 lines (204 loc) · 8.55 KB
/
Copy pathGitRequest.cs
File metadata and controls
245 lines (204 loc) · 8.55 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace GitCredentialManager
{
/// <summary>
/// Represents the request for a single Git credential helper invocation (get / store / erase).
/// </summary>
/// <remarks>
/// Surfaces the input streamed over standard input from Git, including the
/// protocol, host, and remote repository path, plus any negotiated protocol
/// capabilities.
/// </remarks>
public class GitRequest
{
private readonly IReadOnlyDictionary<string, IList<string>> _dict;
private GitCapabilities? _capabilities;
private IReadOnlyDictionary<string, string> _state;
public GitRequest(IDictionary<string, string> dict)
{
EnsureArgument.NotNull(dict, nameof(dict));
// Transform input from 1:1 to 1:n and store as readonly
_dict = new ReadOnlyDictionary<string, IList<string>>(
dict.ToDictionary(x => x.Key, x => (IList<string>)new[] { x.Value })
);
}
public GitRequest(IDictionary<string, IList<string>> dict)
{
EnsureArgument.NotNull(dict, nameof(dict));
// Wrap the dictionary internally as readonly
_dict = new ReadOnlyDictionary<string, IList<string>>(dict);
}
/// <summary>
/// The set of Git credential protocol capabilities that Git itself advertised
/// it supports on this invocation. Unrecognized capability names are silently
/// discarded per the protocol specification.
/// </summary>
public GitCapabilities Capabilities => _capabilities ??= ParseCapabilities();
public string Protocol => GetArgumentOrDefault("protocol");
public string Host => GetArgumentOrDefault("host");
public string Path => GetArgumentOrDefault("path");
public string AuthType => GetArgumentOrDefault(Constants.CredentialProtocol.AuthTypeKey);
public string Credential => GetArgumentOrDefault(Constants.CredentialProtocol.CredentialKey);
public string UserName => GetArgumentOrDefault(Constants.CredentialProtocol.UserNameKey);
public string Password => GetArgumentOrDefault(Constants.CredentialProtocol.PasswordKey);
public bool IsEphemeral => StringExtensions.IsTruthy(GetArgumentOrDefault(Constants.CredentialProtocol.EphemeralKey));
public IList<string> WwwAuth => GetMultiArgumentOrDefault("wwwauth");
/// <summary>
/// Opaque per-helper state Git is replaying from a previous invocation,
/// gated by the <c>state</c> capability.
/// </summary>
/// <remarks>
/// Only entries with our recognized prefix (<see cref="Constants.CredentialProtocol.GcmStatePrefix"/>)
/// are kept. The prefix is stripped from dictionary keys. Malformed entries
/// (missing <c>=</c>, invalid key/value characters) are silently discarded.
/// </remarks>
public IReadOnlyDictionary<string, string> State => _state ??= ParseState();
public string this[string key]
{
get => GetArgumentOrDefault(key);
}
public string GetArgumentOrDefault(string key)
{
return TryGetArgument(key, out string value) ? value : null;
}
public IList<string> GetMultiArgumentOrDefault(string key)
{
return TryGetMultiArgument(key, out IList<string> values) ? values : Array.Empty<string>();
}
public bool TryGetArgument(string key, out string value)
{
if (_dict.TryGetValue(key, out IList<string> values))
{
value = values.FirstOrDefault();
return value != null;
}
value = null;
return false;
}
public bool TryGetMultiArgument(string key, out IList<string> value)
{
if (_dict.TryGetValue(key, out IList<string> values))
{
value = values;
return true;
}
value = null;
return false;
}
public bool TryGetHostAndPort(out string host, out int? port)
{
host = null;
port = null;
if (Host is null)
{
return false;
}
// Split port number and hostname from host input argument
string[] hostParts = Host.Split(':');
if (hostParts.Length > 0)
{
host = hostParts[0];
}
if (hostParts.Length > 1)
{
if (!int.TryParse(hostParts[1], out int portInt))
{
return false;
}
port = portInt;
}
return true;
}
public Uri GetRemoteUri(bool includeUser = false)
{
if (Protocol is null || Host is null)
{
return null;
}
string[] hostParts = Host.Split(':');
if (hostParts.Length > 0)
{
var ub = new UriBuilder(Protocol, hostParts[0]);
if (hostParts.Length > 1 && int.TryParse(hostParts[1], out int port))
{
ub.Port = port;
}
if (includeUser && !string.IsNullOrEmpty(UserName))
{
ub.UserName = Uri.EscapeDataString(UserName);
}
if (Path != null)
{
string[] pathParts = Path.Split('?', '#');
// We know the first piece is the path
ub.Path = pathParts[0];
switch (pathParts.Length)
{
// If we have 3 items, that means path, query, and fragment
case 3:
ub.Query = pathParts[1];
ub.Fragment = pathParts[2];
break;
// If we have 2 items, we must distinguish between query and fragment
case 2 when Path.Contains('?'):
ub.Query = pathParts[1];
break;
case 2 when Path.Contains('#'):
ub.Fragment = pathParts[1];
break;
}
}
return ub.Uri;
}
return null;
}
private GitCapabilities ParseCapabilities()
{
var caps = GitCapabilities.None;
IList<string> values = GetMultiArgumentOrDefault("capability");
foreach (string name in values)
{
caps |= GitCapabilitiesUtils.ParseName(name);
}
return caps;
}
private IReadOnlyDictionary<string, string> ParseState()
{
const string prefix = Constants.CredentialProtocol.GcmStatePrefix;
var result = new Dictionary<string, string>(StringComparer.Ordinal);
IList<string> values = GetMultiArgumentOrDefault(Constants.CredentialProtocol.StateKey);
foreach (string entry in values)
{
int sep = entry.IndexOf('=');
if (sep <= 0)
{
// Malformed (no '=' or empty key): per the protocol
// "unrecognized attributes are silently discarded".
continue;
}
string rawKey = entry.Substring(0, sep);
string value = entry.Substring(sep + 1);
// Only consume our own namespace; let other helpers' state pass
// through us untouched (we never see it once Git stores it
// per-helper, but the protocol mandates this discipline).
if (!rawKey.StartsWith(prefix, StringComparison.Ordinal))
{
continue;
}
string key = rawKey.Substring(prefix.Length);
// Defensive: validate the post-prefix key and the value.
// Git should never hand us malformed entries, but skip any
// that wouldn't round-trip through our own emitter.
if (!GitStateValidation.IsValidKey(key) || !GitStateValidation.IsValidValue(value))
{
continue;
}
result[key] = value;
}
return new ReadOnlyDictionary<string, string>(result);
}
}
}