-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathCredentialProvider.cs
More file actions
296 lines (266 loc) · 15.2 KB
/
CredentialProvider.cs
File metadata and controls
296 lines (266 loc) · 15.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Management.Automation;
using System.Text.Json;
using System.Net.Http;
using System.Net;
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
internal static class CredentialProvider
{
private static readonly string _credProviderExe = "CredentialProvider.Microsoft.exe";
private static readonly string _credProviderDll = "CredentialProvider.Microsoft.dll";
private static string FindCredProviderFromPluginsPath()
{
// Get environment variable "NUGET_PLUGIN_PATHS"
// The environment variable NUGET_PLUGIN_PATHS should have the value of the .exe or .dll of the credential provider found in plugins\netfx\CredentialProvider.Microsoft\
// For example, $env:NUGET_PLUGIN_PATHS="my-alternative-location\CredentialProvider.Microsoft.exe".
// OR $env:NUGET_PLUGIN_PATHS="my-alternative-location\CredentialProvider.Microsoft.dll"
return Environment.GetEnvironmentVariable("NUGET_PLUGIN_PATHS", EnvironmentVariableTarget.User) ?? Environment.GetEnvironmentVariable("NUGET_PLUGIN_PATHS", EnvironmentVariableTarget.Machine);
}
private static string FindCredProviderFromDefaultLocation()
{
// Default locations are either:
// $env:UserProfile\.nuget\plugins\netfx\CredentialProvider\CredentialProvider.Microsoft.exe
// OR $env:UserProfile\.nuget\plugins\netcore\CredentialProvider\CredentialProvider.Microsoft.exe (or) CredentialProvider.Microsoft.dll
var credProviderDefaultLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "plugins");
var netCorePath = Path.Combine(credProviderDefaultLocation, "netcore", "CredentialProvider.Microsoft");
var netFxPath = Path.Combine(credProviderDefaultLocation, "netfx", "CredentialProvider.Microsoft");
var credProviderPath = string.Empty;
if (Directory.Exists(netCorePath))
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
credProviderPath = Path.Combine(netCorePath, _credProviderExe);
}
else
{
credProviderPath = Path.Combine(netCorePath, _credProviderDll);
}
}
else if (Directory.Exists(netFxPath) && Environment.OSVersion.Platform == PlatformID.Win32NT)
{
credProviderPath = Path.Combine(netFxPath, _credProviderExe);
}
return credProviderPath;
}
private static string FindCredProviderFromVSLocation(out ErrorRecord error)
{
error = null;
// C:\Program Files\Microsoft Visual Studio\
var visualStudioPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Microsoft Visual Studio");
// "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe"
// "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\CredentialProvider.Microsoft\CredentialProvider.Microsoft.dll"
var credProviderPath = string.Empty;
if (Directory.Exists(visualStudioPath))
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
credProviderPath = VSCredentialProviderFile(visualStudioPath, _credProviderExe, out error);
}
else if (string.IsNullOrEmpty(credProviderPath))
{
credProviderPath = VSCredentialProviderFile(visualStudioPath, _credProviderDll, out error);
}
}
return credProviderPath;
}
private static string VSCredentialProviderFile(string visualStudioPath, string credProviderFile, out ErrorRecord error)
{
error = null;
try
{
// Search for the file in the directory and subdirectories
string[] exeFile = Directory.GetFiles(visualStudioPath, credProviderFile, SearchOption.AllDirectories);
if (exeFile.Length > 0)
{
return exeFile[0];
}
}
catch (UnauthorizedAccessException e)
{
error = new ErrorRecord(
e,
"AccessToCredentialProviderFileDenied",
ErrorCategory.PermissionDenied,
null);
}
catch (Exception ex)
{
error = new ErrorRecord(
ex,
"ErrorRetrievingCredentialProvider",
ErrorCategory.NotSpecified,
null);
}
return string.Empty;
}
internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdletPassedIn)
{
cmdletPassedIn.WriteVerbose("Entering CredentialProvider::GetCredentialsFromProvider");
string credProviderPath = string.Empty;
// Find credential provider
// Option 1. Use env var 'NUGET_PLUGIN_PATHS' to find credential provider.
// See: https://docs.microsoft.com/en-us/nuget/reference/extensibility/nuget-cross-platform-plugins#plugin-installation-and-discovery
// Nuget prioritizes credential providers stored in the NUGET_PLUGIN_PATHS env var
credProviderPath = FindCredProviderFromPluginsPath();
// Option 2. Check default locations ($env:UserProfile\.nuget\plugins)
// .NET Core based plugins should be installed in:
// %UserProfile%/.nuget/plugins/netcore
// .NET Framework based plugins should be installed in:
// %UserProfile%/.nuget/plugins/netfx
if (String.IsNullOrEmpty(credProviderPath))
{
credProviderPath = FindCredProviderFromDefaultLocation();
}
// Option 3. Check Visual Studio installation paths
if (String.IsNullOrEmpty(credProviderPath))
{
credProviderPath = FindCredProviderFromVSLocation(out ErrorRecord error);
if (error != null)
{
cmdletPassedIn.WriteError(error);
return null;
}
}
cmdletPassedIn.WriteVerbose($"Credential provider path is '{credProviderPath}'");
if (string.IsNullOrEmpty(credProviderPath))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new ArgumentNullException("Path to the Azure Artifacts Credential Provider is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
"CredentialProviderPathIsNullOrEmpty",
ErrorCategory.InvalidArgument,
credProviderPath));
return null;
}
if (!File.Exists(credProviderPath))
{
// If the Credential Provider is not found on a Unix machine, try looking for a case insensitive file.
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
FileInfo fileInfo = new FileInfo(credProviderPath);
if (!Utils.TryGetCaseInsensitiveFilePath(fileInfo.Directory.FullName, _credProviderDll, out credProviderPath))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new FileNotFoundException($"Path found '{credProviderPath}' is not a valid Azure Artifact Credential Provider executable. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
"CredentialProviderFileNotFound",
ErrorCategory.ObjectNotFound,
credProviderPath));
}
}
else
{
cmdletPassedIn.WriteError(new ErrorRecord(
new FileNotFoundException($"Path found '{credProviderPath}' is not a valid Azure Artifact Credential Provider executable. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
"CredentialProviderFileNotFound",
ErrorCategory.ObjectNotFound,
credProviderPath));
return null;
}
}
cmdletPassedIn.WriteVerbose($"Credential Provider path found at: '{credProviderPath}'");
string fileName = credProviderPath;
// If running on unix machines, the Credential Provider needs to be called with dotnet cli.
if (credProviderPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
fileName = "dotnet";
}
string arguments = string.Equals(fileName, "dotnet", StringComparison.OrdinalIgnoreCase) ?
$"{credProviderPath} -Uri {uri} -NonInteractive -IsRetry -F Json" :
$"-Uri {uri} -NonInteractive -IsRetry -F Json";
string fullCallingCmd = string.Equals(fileName, "dotnet", StringComparison.OrdinalIgnoreCase) ?
$"dotnet {credProviderPath} -Uri {uri} -NonInteractive -IsRetry -F Json" :
$"{credProviderPath} -Uri {uri} -NonInteractive -IsRetry -F Json";
cmdletPassedIn.WriteVerbose($"Calling Credential Provider with the following: '{fullCallingCmd}'");
using (Process process = new Process())
{
// Windows call should look like: "CredentialProvider.Microsoft.exe -Uri <uri> -NonInteractive -IsRetry -F Json"
// Unix call should look like: "dotnet CredentialProvider.Microsoft.dll -Uri <uri> -NonInteractive -IsRetry -F Json"
process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
var stdError = process.StandardError.ReadToEnd();
// Timeout in milliseconds (e.g., 5000 ms = 5 seconds)
process.WaitForExit(5000);
if (process.ExitCode != 0)
{
if (!string.IsNullOrEmpty(stdError))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new ArgumentException($"Standard error: {stdError}"),
"ProcessStandardError",
ErrorCategory.InvalidResult,
credProviderPath));
}
cmdletPassedIn.WriteError(new ErrorRecord(
new Exception($"Process exited with code {process.ExitCode}"),
"ProcessExitCodeError",
ErrorCategory.InvalidResult,
credProviderPath));
}
else if (string.IsNullOrEmpty(output))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new ArgumentException($"Standard output is empty."),
"ProcessStandardOutputError",
ErrorCategory.InvalidResult,
credProviderPath));
}
string username = string.Empty;
SecureString passwordSecure = new SecureString();
try
{
using (JsonDocument doc = JsonDocument.Parse(output))
{
JsonElement root = doc.RootElement;
if (root.TryGetProperty("Username", out JsonElement usernameToken))
{
username = usernameToken.GetString();
cmdletPassedIn.WriteVerbose("Username retrieved from Credential Provider.");
}
if (String.IsNullOrEmpty(username))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new ArgumentNullException("Credential Provider username is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info."),
"CredentialProviderUserNameIsNullOrEmpty",
ErrorCategory.InvalidArgument,
credProviderPath));
return null;
}
if (root.TryGetProperty("Password", out JsonElement passwordToken))
{
string password = passwordToken.GetString();
if (String.IsNullOrEmpty(password))
{
cmdletPassedIn.WriteError(new ErrorRecord(
new ArgumentNullException("Credential Provider password is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info."),
"CredentialProviderUserNameIsNullOrEmpty",
ErrorCategory.InvalidArgument,
credProviderPath));
return null;
}
passwordSecure = Utils.ConvertToSecureString(password);
cmdletPassedIn.WriteVerbose("Password retrieved from Credential Provider.");
}
}
}
catch (Exception e)
{
cmdletPassedIn.WriteError(new ErrorRecord(
new Exception("Error retrieving credentials from Credential Provider. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info.", e),
"InvalidCredentialProviderResponse",
ErrorCategory.InvalidResult,
credProviderPath));
return null;
}
return new PSCredential(username, passwordSecure);
}
}
}
}