Skip to content

Commit 0f8dae4

Browse files
authored
Update project resolution and app settings writes in dotnet user-jwts (#48385)
* Fix project resolution logic in user-jwts to close #46218 * Update write stream construction to close #44869 * Address review feedback
1 parent 08a443e commit 0f8dae4

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

src/Tools/dotnet-user-jwts/src/Commands/CreateCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ private static (JwtCreatorOptions, bool, string) ValidateArguments(
109109
CommandOption claimsOption)
110110
{
111111
var isValid = true;
112-
var project = DevJwtCliHelpers.GetProject(projectOption.Value());
112+
var finder = new MsBuildProjectFinder(projectOption.Value() ?? Directory.GetCurrentDirectory());
113+
var project = finder.FindMsBuildProject(projectOption.Value());
113114

114115
if (project == null)
115116
{

src/Tools/dotnet-user-jwts/src/Helpers/DevJwtCliHelpers.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,10 @@ public static string GetOrSetUserSecretsId(string projectFilePath)
2525
return id;
2626
}
2727

28-
public static string GetProject(string projectPath = null)
29-
{
30-
if (projectPath is not null)
31-
{
32-
return projectPath;
33-
}
34-
35-
var csprojFiles = Directory.EnumerateFileSystemEntries(Directory.GetCurrentDirectory(), "*.*proj", SearchOption.TopDirectoryOnly)
36-
.Where(f => !".xproj".Equals(Path.GetExtension(f), StringComparison.OrdinalIgnoreCase))
37-
.ToList();
38-
if (csprojFiles is [var path])
39-
{
40-
return path;
41-
}
42-
return null;
43-
}
44-
4528
public static bool GetProjectAndSecretsId(string projectPath, IReporter reporter, out string project, out string userSecretsId)
4629
{
47-
project = GetProject(projectPath);
30+
var finder = new MsBuildProjectFinder(projectPath ?? Directory.GetCurrentDirectory());
31+
project = finder.FindMsBuildProject(projectPath);
4832
userSecretsId = null;
4933
if (project == null)
5034
{

src/Tools/dotnet-user-jwts/src/Helpers/JwtAuthenticationSchemeSettings.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public void Save(string filePath)
5757
};
5858
}
5959

60-
using var writer = new FileStream(filePath, FileMode.Open, FileAccess.Write);
60+
var streamOptions = new FileStreamOptions { Access = FileAccess.Write, Mode = FileMode.Create };
61+
if (!OperatingSystem.IsWindows())
62+
{
63+
streamOptions.UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite;
64+
}
65+
using var writer = new FileStream(filePath, streamOptions);
6166
JsonSerializer.Serialize(writer, config, _jsonSerializerOptions);
6267
}
6368

src/Tools/dotnet-user-jwts/test/UserJwtsTests.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Globalization;
7-
using System.IO;
8-
using System.Text;
94
using Microsoft.AspNetCore.Testing;
105
using Microsoft.Extensions.Configuration.UserSecrets;
116
using Microsoft.Extensions.Tools.Internal;
12-
using Microsoft.AspNetCore.Authentication.JwtBearer.Tools;
13-
using Xunit;
147
using Xunit.Abstractions;
158
using System.Text.RegularExpressions;
169
using System.Text.Json;
1710
using System.Text.Json.Nodes;
1811
using System.IdentityModel.Tokens.Jwt;
19-
using System.Reflection;
20-
using System.Numerics;
2112

2213
namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Tests;
2314

@@ -80,6 +71,25 @@ public void Create_WritesGeneratedTokenToDisk()
8071
Assert.Contains("dotnet-user-jwts", File.ReadAllText(appsettings));
8172
}
8273

74+
[Fact]
75+
public void Create_CanModifyExistingScheme()
76+
{
77+
var project = Path.Combine(_fixture.CreateProject(), "TestProject.csproj");
78+
var appsettings = Path.Combine(Path.GetDirectoryName(project), "appsettings.Development.json");
79+
var app = new Program(_console);
80+
81+
app.Run(new[] { "create", "--project", project });
82+
Assert.Contains("New JWT saved", _console.GetOutput());
83+
var matches = Regex.Matches(_console.GetOutput(), "New JWT saved with ID '(.*?)'");
84+
var id = matches.SingleOrDefault().Groups[1].Value;
85+
86+
var appSettings = JsonSerializer.Deserialize<JsonObject>(File.ReadAllText(appsettings));
87+
Assert.Equal("dotnet-user-jwts", appSettings["Authentication"]["Schemes"]["Bearer"]["ValidIssuer"].GetValue<string>());
88+
app.Run(new[] { "create", "--project", project, "--issuer", "new-issuer" });
89+
appSettings = JsonSerializer.Deserialize<JsonObject>(File.ReadAllText(appsettings));
90+
Assert.Equal("new-issuer", appSettings["Authentication"]["Schemes"]["Bearer"]["ValidIssuer"].GetValue<string>());
91+
}
92+
8393
[Fact]
8494
public void Print_ReturnsNothingForMissingToken()
8595
{
@@ -594,7 +604,7 @@ public void Create_CanHandleNoProjectOptionProvided_WithNoProjects()
594604
var app = new Program(_console);
595605
app.Run(new[] { "create" });
596606

597-
Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
607+
Assert.Contains($"Could not find a MSBuild project file in '{Directory.GetCurrentDirectory()}'. Specify which project to use with the --project option.", _console.GetOutput());
598608
Assert.DoesNotContain(Resources.CreateCommand_NoAudience_Error, _console.GetOutput());
599609
}
600610

@@ -607,7 +617,7 @@ public void Delete_CanHandleNoProjectOptionProvided_WithNoProjects()
607617
var app = new Program(_console);
608618
app.Run(new[] { "remove", "some-id" });
609619

610-
Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
620+
Assert.Contains($"Could not find a MSBuild project file in '{Directory.GetCurrentDirectory()}'. Specify which project to use with the --project option.", _console.GetOutput());
611621
}
612622

613623
[Fact]
@@ -619,7 +629,7 @@ public void Clear_CanHandleNoProjectOptionProvided_WithNoProjects()
619629
var app = new Program(_console);
620630
app.Run(new[] { "clear" });
621631

622-
Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
632+
Assert.Contains($"Could not find a MSBuild project file in '{Directory.GetCurrentDirectory()}'. Specify which project to use with the --project option.", _console.GetOutput());
623633
}
624634

625635
[Fact]
@@ -631,7 +641,19 @@ public void List_CanHandleNoProjectOptionProvided_WithNoProjects()
631641
var app = new Program(_console);
632642
app.Run(new[] { "list" });
633643

634-
Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
644+
Assert.Contains($"Could not find a MSBuild project file in '{Directory.GetCurrentDirectory()}'. Specify which project to use with the --project option.", _console.GetOutput());
645+
}
646+
647+
[Fact]
648+
public void List_CanHandleProjectOptionAsPath()
649+
{
650+
var projectPath = _fixture.CreateProject();
651+
var project = Path.Combine(projectPath, "TestProject.csproj");
652+
653+
var app = new Program(_console);
654+
app.Run(new[] { "list", "--project", projectPath });
655+
656+
Assert.Contains(Path.Combine(projectPath, project), _console.GetOutput());
635657
}
636658

637659
[ConditionalFact]

0 commit comments

Comments
 (0)