-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bf23154
Add dotnet dev-jwts tool
captainsafia 6961df5
Add dotnet dev-jwts tool
captainsafia 212cf04
Address feedback from review
captainsafia 3ae682e
Rename project file
captainsafia 5f5c040
Write auth config to app settings
captainsafia 2675c50
Address more feedback
captainsafia c37a2a6
:seal:
captainsafia 212b42f
Apply suggestions from code review
captainsafia ef45270
Address more feedback
captainsafia 751c1d7
Add framework support for authentication changes
captainsafia 6562366
Add tests for user-jwts CLI and react to feedback
captainsafia 672fb64
Move ConsoleTable implementation to avoid conflicts in ProjectTemplates
captainsafia bd19796
Update existing auth tests and fix middleware registration
captainsafia 1f3a990
Update AzureAdB2C tests and auth app builder
captainsafia 23e9b0c
Fix build and move registration check
captainsafia f0aa386
Fix up resolution for Certificate test sources
captainsafia 98b504f
Fix write stream configuration for writing key material
captainsafia 50a3cda
Fix handling missing config section when processing options
captainsafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
} | ||
|
||
if (!force) | ||
{ | ||
Console.WriteLine($"Are you sure you want to delete {count} JWT(s) for {project}? \n [Y]es / [N]o"); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Console.ReadKey().Key != ConsoleKey.Y) | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Console.WriteLine("Cancelled, no JWTs were deleted"); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
} | ||
} | ||
|
||
jwtStore.Jwts.Clear(); | ||
jwtStore.Save(); | ||
|
||
Console.WriteLine($"Deleted {count} token(s) from {project} successfully"); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.