Skip to content

Commit 649303f

Browse files
committed
Add tool to verify the clear-text of an API key (#6383)
1 parent cd198b0 commit 649303f

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

src/GalleryTools/GalleryTools.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="Commands\BackfillRepositoryMetadataCommand.cs" />
4848
<Compile Include="Commands\HashCommand.cs" />
4949
<Compile Include="Commands\ReflowCommand.cs" />
50+
<Compile Include="Commands\VerifyApiKeyCommand.cs" />
5051
<Compile Include="Program.cs" />
5152
<Compile Include="Properties\AssemblyInfo.cs" />
5253
</ItemGroup>

src/GalleryTools/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static int Main(params string[] args)
1717
commandLineApplication.Command("hash", HashCommand.Configure);
1818
commandLineApplication.Command("reflow", ReflowCommand.Configure);
1919
commandLineApplication.Command("fillrepodata", BackfillRepositoryMetadataCommand.Configure);
20+
commandLineApplication.Command("verifyapikey", VerifyApiKeyCommand.Configure);
2021

2122
try
2223
{

0 commit comments

Comments
 (0)