|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.CommandLineUtils; |
| 7 | +using NuGetGallery; |
| 8 | +using NuGetGallery.Infrastructure.Authentication; |
| 9 | + |
| 10 | +namespace GalleryTools.Commands |
| 11 | +{ |
| 12 | + public static class VerifyApiKeyCommand |
| 13 | + { |
| 14 | + public static void Configure(CommandLineApplication config) |
| 15 | + { |
| 16 | + config.Description = "Verify a clear text API key matches a hashed API key."; |
| 17 | + config.HelpOption("-? | -h | --help"); |
| 18 | + |
| 19 | + var plainTextOption = config.Option( |
| 20 | + "--clear-text", |
| 21 | + "The clear text value of the API key.", |
| 22 | + CommandOptionType.SingleValue); |
| 23 | + |
| 24 | + var credentialTypesOption = config.Option( |
| 25 | + "--type", |
| 26 | + "One or more credential types.", |
| 27 | + CommandOptionType.MultipleValue); |
| 28 | + |
| 29 | + var credentialValuesOption = config.Option( |
| 30 | + "--value", |
| 31 | + "One or more credential values.", |
| 32 | + CommandOptionType.MultipleValue); |
| 33 | + |
| 34 | + config.OnExecute(() => Execute( |
| 35 | + plainTextOption, |
| 36 | + credentialTypesOption, |
| 37 | + credentialValuesOption)); |
| 38 | + } |
| 39 | + |
| 40 | + private static int Execute( |
| 41 | + CommandOption clearTextOption, |
| 42 | + CommandOption credentialTypesOption, |
| 43 | + CommandOption credentialValuesOption) |
| 44 | + { |
| 45 | + if (!clearTextOption.HasValue()) |
| 46 | + { |
| 47 | + Console.Error.WriteLine($"The {clearTextOption.Template} option is required."); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + if (!credentialTypesOption.HasValue()) |
| 52 | + { |
| 53 | + Console.Error.WriteLine($"The {credentialTypesOption.Template} option is required."); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + if (!credentialValuesOption.HasValue()) |
| 58 | + { |
| 59 | + Console.Error.WriteLine($"The {credentialValuesOption.Template} option is required."); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + if (credentialTypesOption.Values.Count != credentialValuesOption.Values.Count) |
| 64 | + { |
| 65 | + Console.Error.WriteLine($"The should be the same number of {credentialTypesOption.Template} options as {credentialValuesOption.Template} options."); |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + var credentialValidator = new CredentialValidator(); |
| 70 | + |
| 71 | + Console.WriteLine($"Testing {credentialTypesOption.Values.Count} API key(s)."); |
| 72 | + |
| 73 | + for (var i = 0; i < credentialTypesOption.Values.Count; i++) |
| 74 | + { |
| 75 | + var credential = new Credential |
| 76 | + { |
| 77 | + Type = credentialTypesOption.Values[i], |
| 78 | + Value = credentialValuesOption.Values[i], |
| 79 | + }; |
| 80 | + |
| 81 | + var validCredentials = credentialValidator.GetValidCredentialsForApiKey( |
| 82 | + new[] { credential }.AsQueryable(), |
| 83 | + clearTextOption.Value()); |
| 84 | + |
| 85 | + Console.WriteLine(); |
| 86 | + Console.WriteLine($"API key {i + 1}:"); |
| 87 | + Console.WriteLine($" Type: {credential.Type}"); |
| 88 | + Console.WriteLine($" Value: {credential.Value}"); |
| 89 | + Console.WriteLine($" Matches: {validCredentials.Any().ToString().ToUpperInvariant()}"); |
| 90 | + } |
| 91 | + |
| 92 | + return 0; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments