Skip to content

Add dotnet user-jwts tool and runtime support #41520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Html.A
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RateLimiting", "RateLimiting", "{1D865E78-7A66-4CA9-92EE-2B350E45281F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-user-jwts", "src\Tools\dotnet-user-jwts\src\dotnet-user-jwts.csproj", "{B34CB502-0286-4939-B25F-45998528A802}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnet-user-jwts", "dotnet-user-jwts", "{AB4B9E75-719C-4589-B852-20FBFD727730}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -10247,6 +10251,22 @@ Global
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x64.Build.0 = Release|Any CPU
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x86.ActiveCfg = Release|Any CPU
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x86.Build.0 = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|arm64.ActiveCfg = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|arm64.Build.0 = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x64.ActiveCfg = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x64.Build.0 = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x86.ActiveCfg = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x86.Build.0 = Debug|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|Any CPU.Build.0 = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|arm64.ActiveCfg = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|arm64.Build.0 = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|x64.ActiveCfg = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|x64.Build.0 = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|x86.ActiveCfg = Release|Any CPU
{B34CB502-0286-4939-B25F-45998528A802}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -11094,6 +11114,8 @@ Global
{51D07AA9-6297-4F66-A7BD-71CE7E3F4A3F} = {0F84F170-57D0-496B-8E2C-7984178EF69F}
{487EF7BE-5009-4C70-B79E-45519BDD9603} = {412D4C15-F48F-4DB1-940A-131D1AA87088}
{1D865E78-7A66-4CA9-92EE-2B350E45281F} = {E5963C9F-20A6-4385-B364-814D2581FADF}
{B34CB502-0286-4939-B25F-45998528A802} = {AB4B9E75-719C-4589-B852-20FBFD727730}
{AB4B9E75-719C-4589-B852-20FBFD727730} = {0B200A66-B809-4ED3-A790-CB1C2E80975E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
1 change: 1 addition & 0 deletions eng/Signing.props
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<FileSignInfo Include="dotnet-user-secrets.exe" CertificateName="MicrosoftDotNet500" />
<FileSignInfo Include="dotnet-watch.exe" CertificateName="MicrosoftDotNet500" />
<FileSignInfo Include="dotnet-openapi.exe" CertificateName="MicrosoftDotNet500" />
<FileSignInfo Include="dotnet-user-jwts.exe" CertificateName="MicrosoftDotNet500" />
<FileSignInfo Include="Microsoft.AspNetCore.Blazor.Build.exe" CertificateName="MicrosoftDotNet500" />
<FileSignInfo Include="sni.dll" CertificateName="MicrosoftDotNet500" />

Expand Down
76 changes: 76 additions & 0 deletions src/Shared/CommandLineUtils/CommandLine/ConsoleTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Extensions.CommandLineUtils;

internal sealed class ConsoleTable
{
private readonly List<string> _columns = new();
private readonly List<object[]> _rows = new();

public void AddColumns(params string[] names)
{
_columns.AddRange(names);
}

public void AddRow(params object[] values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}

if (!_columns.Any())
{
throw new Exception("Columns must be set before rows can be added.");
}

if (_columns.Count != values.Length)
{
throw new Exception(
$"The number of columns in the table '{_columns.Count}' does not match the number of columns in the row '{values.Length}'.");
}

_rows.Add(values);
}

public void Write()
{
var builder = new StringBuilder();

var maxColumnLengths = _columns
.Select((t, i) => _rows.Select(x => x[i])
.Concat(new[] { _columns[i] })
.Where(x => x != null)
.Select(x => x!.ToString()!.Length).Max())
.ToList();

var formatRow = Enumerable.Range(0, _columns.Count)
.Select(i => " | {" + i + ", " + maxColumnLengths[i] + "}")
.Aggregate((previousRowColumn, nextRowColumn) => previousRowColumn + nextRowColumn) + " |";

var formattedRows = _rows.Select(row => string.Format(CultureInfo.InvariantCulture, formatRow, row)).ToList();
var columnHeaders = string.Format(CultureInfo.InvariantCulture, formatRow, _columns.ToArray());
var rowDivider = $" {new string('-', columnHeaders.Length - 1)} ";

builder.AppendLine(rowDivider);
builder.AppendLine(columnHeaders);

foreach (var formattedRow in formattedRows)
{
builder.AppendLine(rowDivider);
builder.AppendLine(formattedRow);
}

builder.AppendLine(rowDivider);

Console.WriteLine(builder.ToString());
}
}
3 changes: 2 additions & 1 deletion src/Tools/Tools.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@
"src\\Tools\\Microsoft.dotnet-openapi\\src\\Microsoft.dotnet-openapi.csproj",
"src\\Tools\\Microsoft.dotnet-openapi\\test\\dotnet-microsoft.openapi.Tests.csproj",
"src\\Tools\\dotnet-dev-certs\\src\\dotnet-dev-certs.csproj",
"src\\Tools\\dotnet-user-jwts\\src\\dotnet-user-jwts.csproj",
"src\\Tools\\dotnet-getdocument\\src\\dotnet-getdocument.csproj",
"src\\Tools\\dotnet-sql-cache\\src\\dotnet-sql-cache.csproj",
"src\\Tools\\dotnet-user-secrets\\src\\dotnet-user-secrets.csproj",
"src\\Tools\\dotnet-user-secrets\\test\\dotnet-user-secrets.Tests.csproj",
"src\\WebEncoders\\src\\Microsoft.Extensions.WebEncoders.csproj"
]
}
}
}
67 changes: 67 additions & 0 deletions src/Tools/dotnet-user-jwts/src/Commands/ClearCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools;

internal sealed class ClearCommand
{
public static void Register(ProjectCommandLineApplication app)
{
app.Command("clear", cmd =>
{
cmd.Description = "Delete all issued JWTs for a project";

var forceOption = cmd.Option(
"--force",
"Don't prompt for confirmation before deleting JWTs",
CommandOptionType.NoValue);

cmd.HelpOption("-h|--help");

cmd.OnExecute(() =>
{
return Execute(app.ProjectOption.Value(), forceOption.HasValue());
});
});
}

private static int Execute(string projectPath, bool force)
{
var project = DevJwtCliHelpers.GetProject(projectPath);
if (project == null)
{
Console.WriteLine($"No project found at `-p|--project` path or current directory.");
return 1;
}

var userSecretsId = DevJwtCliHelpers.GetUserSecretsId(project);
var jwtStore = new JwtStore(userSecretsId);

var count = jwtStore.Jwts.Count;

if (count == 0)
{
Console.WriteLine($"There are no JWTs to delete from {project}");
return 0;
}

if (!force)
{
Console.WriteLine($"Are you sure you want to delete {count} JWT(s) for {project}? \n [Y]es / [N]o");
if (Console.ReadKey().Key != ConsoleKey.Y)
{
Console.WriteLine("Cancelled, no JWTs were deleted");
return 0;
}
}

jwtStore.Jwts.Clear();
jwtStore.Save();

Console.WriteLine($"Deleted {count} token(s) from {project} successfully");

return 0;
}
}
Loading