public void SetApiKey_DefaultSource() { using (var testFolder = TestFileSystemUtility.CreateRandomTestFolder()) { var configFile = Path.Combine(testFolder, "nuget.config"); Util.CreateFile(configFile, "<configuration/>"); var testApiKey = Guid.NewGuid().ToString(); // Act var result = CommandRunner.Run( NuGetExePath, testFolder, $"setApiKey {testApiKey} -ConfigFile {configFile}", waitForExit: true); // Assert Assert.True(0 == result.Item1, $"{result.Item2} {result.Item3}"); Assert.Contains($"The API Key '{testApiKey}' was saved for the NuGet gallery (https://www.nuget.org) and the symbol server (https://nuget.smbsrc.net/)", result.Item2); var settings = Configuration.Settings.LoadDefaultSettings( Path.GetDirectoryName(configFile), Path.GetFileName(configFile), null); var actualApiKey = SettingsUtility.GetDecryptedValue(settings, ConfigurationConstants.ApiKeys, NuGetConstants.DefaultGalleryServerUrl); Assert.NotNull(actualApiKey); Assert.Equal(testApiKey, actualApiKey); } }
public static string GetApiKey(ISettings settings, string source, string apiKey) { if (string.IsNullOrEmpty(apiKey)) { apiKey = SettingsUtility.GetDecryptedValue(settings, ConfigurationConstants.ApiKeys, source); } return(apiKey); }
public static string GetApiKey(ISettings settings, string endpoint, string source, string defaultApiKey, bool isSymbolApiKey) { // try searching API key by endpoint first // needed to support config key mappings like 'https://www.nuget.org/api/v2/package' var apiKey = SettingsUtility.GetDecryptedValue(settings, ConfigurationConstants.ApiKeys, endpoint); // if not found try finding it by source url apiKey = apiKey ?? SettingsUtility.GetDecryptedValue(settings, ConfigurationConstants.ApiKeys, source); // fallback for a case of nuget.org source // try to retrieve an api key mapped to a default "gallery" url if (apiKey == null && source.IndexOf(NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase) >= 0) { var defaultConfigKey = isSymbolApiKey ? NuGetConstants.DefaultSymbolServerUrl : NuGetConstants.DefaultGalleryServerUrl; apiKey = SettingsUtility.GetDecryptedValue(settings, ConfigurationConstants.ApiKeys, defaultConfigKey); } // return an API key when found or the default one return(apiKey ?? defaultApiKey); }