Skip to content

Commit 749c2a2

Browse files
cleanups
1 parent f458c95 commit 749c2a2

File tree

8 files changed

+27
-49
lines changed

8 files changed

+27
-49
lines changed

access-token-management/samples/WebClientAssertions/Controllers/HomeController.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@
1111

1212
namespace WebClientAssertions.Controllers;
1313

14-
public class HomeController : Controller
14+
public class HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager tokenManager)
15+
: Controller
1516
{
16-
private readonly IHttpClientFactory _httpClientFactory;
17-
private readonly IUserTokenManager _tokenManager;
18-
19-
public HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager tokenManager)
20-
{
21-
_httpClientFactory = httpClientFactory;
22-
_tokenManager = tokenManager;
23-
}
24-
2517
[AllowAnonymous]
2618
public IActionResult Index() => View();
2719

@@ -34,8 +26,8 @@ public HomeController(IHttpClientFactory httpClientFactory, IUserTokenManager to
3426

3527
public async Task<IActionResult> CallApiAsUserManual()
3628
{
37-
var token = await _tokenManager.GetAccessTokenAsync(User).GetToken();
38-
var client = _httpClientFactory.CreateClient();
29+
var token = await tokenManager.GetAccessTokenAsync(User).GetToken();
30+
var client = httpClientFactory.CreateClient();
3931
client.SetBearerToken(token.AccessToken.ToString()!);
4032

4133
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");
@@ -46,7 +38,7 @@ public async Task<IActionResult> CallApiAsUserManual()
4638

4739
public async Task<IActionResult> CallApiAsUserFactory()
4840
{
49-
var client = _httpClientFactory.CreateClient("user_client");
41+
var client = httpClientFactory.CreateClient("user_client");
5042
var response = await client.GetStringAsync("test");
5143

5244
ViewBag.Json = PrettyPrint(response);
@@ -64,7 +56,7 @@ public async Task<IActionResult> CallApiAsUserFactoryTyped([FromServices] TypedU
6456
[AllowAnonymous]
6557
public async Task<IActionResult> CallApiAsClientFactory()
6658
{
67-
var client = _httpClientFactory.CreateClient("client");
59+
var client = httpClientFactory.CreateClient("client");
6860
var response = await client.GetStringAsync("test");
6961

7062
ViewBag.Json = PrettyPrint(response);

access-token-management/samples/WebClientAssertions/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
var builder = WebApplication.CreateBuilder(args);
2020

21-
builder.Host.UseSerilog((ctx, lc) => lc
21+
builder.Host.UseSerilog((_, lc) => lc
2222
.WriteTo.Console(
2323
outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
2424
theme: AnsiConsoleTheme.Code)

access-token-management/samples/WebClientAssertions/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
119119
internal static WebApplication ConfigurePipeline(this WebApplication app)
120120
{
121121
app.UseSerilogRequestLogging(
122-
options => options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug);
122+
options => options.GetLevel = (_, _, _) => LogEventLevel.Debug);
123123

124124
app.UseDeveloperExceptionPage();
125125
app.UseHttpsRedirection();

access-token-management/samples/WebClientAssertions/TypedClient.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,11 @@
33

44
namespace WebClientAssertions;
55

6-
public abstract class TypedClient
6+
public abstract class TypedClient(HttpClient client)
77
{
8-
private readonly HttpClient _client;
9-
10-
public TypedClient(HttpClient client) => _client = client;
11-
12-
public virtual async Task<string> CallApi() => await _client.GetStringAsync("test");
8+
public virtual async Task<string> CallApi() => await client.GetStringAsync("test");
139
}
1410

15-
public class TypedUserClient : TypedClient
16-
{
17-
public TypedUserClient(HttpClient client) : base(client)
18-
{
19-
}
20-
}
11+
public class TypedUserClient(HttpClient client) : TypedClient(client);
2112

22-
public class TypedClientClient : TypedClient
23-
{
24-
public TypedClientClient(HttpClient client) : base(client)
25-
{
26-
}
27-
}
13+
public class TypedClientClient(HttpClient client) : TypedClient(client);

access-token-management/test/AccessTokenManagement.Tests/ClientTokenManagementTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task Missing_client_id_throw_exception()
6868
public async Task Missing_client_secret_throw_exception()
6969
{
7070

71-
var mockedRequest = _mockHttp.Expect("/connect/token")
71+
_ = _mockHttp.Expect("/connect/token")
7272
.Respond(_ => Some.TokenHttpResponse());
7373

7474
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
@@ -150,7 +150,7 @@ public async Task Token_request_and_response_should_have_expected_values(ClientC
150150
_mockHttp.Expect("/connect/token")
151151
.WithFormData(expectedRequestFormData)
152152
.WithHeaders("Authorization",
153-
"Basic " + IdentityModel.Client.BasicAuthenticationOAuthHeaderValue.EncodeCredential(The.ClientId.ToString(), The.ClientSecret.ToString()))
153+
"Basic " + BasicAuthenticationOAuthHeaderValue.EncodeCredential(The.ClientId.ToString(), The.ClientSecret.ToString()))
154154
.Respond(_ => Some.TokenHttpResponse(Some.Token()));
155155
}
156156

access-token-management/test/AccessTokenManagement.Tests/UserTokenManagementTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class UserTokenManagementTests(ITestOutputHelper output) : IntegrationTes
2424
public async Task Anonymous_user_should_return_user_token_error()
2525
{
2626
await InitializeAsync();
27-
var response = await AppHost.BrowserClient!.GetAsync(AppHost.Url("/user_token_error"), _ct);
27+
var response = await AppHost.BrowserClient.GetAsync(AppHost.Url("/user_token_error"), _ct);
2828
var token = await response.Content.ReadFromJsonAsync<FailedResult>(_ct);
2929

3030
token!.Error.ShouldNotBeNull();
@@ -34,7 +34,7 @@ public async Task Anonymous_user_should_return_user_token_error()
3434
public async Task Anonymous_user_should_return_client_token()
3535
{
3636
await InitializeAsync();
37-
var response = await AppHost.BrowserClient!.GetAsync(AppHost.Url("/client_token"), _ct);
37+
var response = await AppHost.BrowserClient.GetAsync(AppHost.Url("/client_token"), _ct);
3838
var token = await response.Content.ReadFromJsonAsync<ClientCredentialsTokenModel>(_ct);
3939

4040
token!.AccessToken.ShouldNotBeNull();
@@ -80,13 +80,13 @@ Task<ClaimsPrincipal> LocalTransformPrincipalAsync(ClaimsPrincipal principal, Ca
8080
await AppHost.LoginAsync("alice");
8181
// Get a user token. This should trigger a token refresh, which then get's stored and triggers
8282
// the custom token transform
83-
await AppHost.BrowserClient!.GetAsync(AppHost.Url("/user_token"), _ct);
83+
await AppHost.BrowserClient.GetAsync(AppHost.Url("/user_token"), _ct);
8484

8585
// Verify that the transform is used.
8686
transformed.ShouldBeTrue();
8787

8888
// The transformed principal should now be used.
89-
var claims = await AppHost.BrowserClient!.GetFromJsonAsync<Dictionary<string, string>>(AppHost.Url("/user"), _ct);
89+
var claims = await AppHost.BrowserClient.GetFromJsonAsync<Dictionary<string, string>>(AppHost.Url("/user"), _ct);
9090
claims![JwtClaimTypes.Name].ShouldBe("transformed");
9191
}
9292

identity-model-oidc-client/src/IdentityModel.OidcClient/OidcClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public virtual async Task<LoginResult> ProcessResponseAsync(
205205
return new LoginResult(result.Error, result.ErrorDescription);
206206
}
207207

208-
var userInfoClaims = Enumerable.Empty<Claim>();
208+
var userInfoClaims = Array.Empty<Claim>();
209209
if (Options.LoadProfile)
210210
{
211211
var userInfoResult = await GetUserInfoAsync(result.TokenResponse.AccessToken, cancellationToken);
@@ -217,7 +217,7 @@ public virtual async Task<LoginResult> ProcessResponseAsync(
217217
return new LoginResult(error);
218218
}
219219

220-
userInfoClaims = userInfoResult.Claims;
220+
userInfoClaims = userInfoResult.Claims.ToArray();
221221

222222
var userInfoSub = userInfoClaims.FirstOrDefault(c => c.Type == JwtClaimTypes.Subject);
223223
if (userInfoSub == null)
@@ -230,7 +230,7 @@ public virtual async Task<LoginResult> ProcessResponseAsync(
230230

231231
if (result.TokenResponse.IdentityToken != null)
232232
{
233-
if (!string.Equals(userInfoSub.Value, result.User.FindFirst(JwtClaimTypes.Subject).Value))
233+
if (!string.Equals(userInfoSub.Value, result.User.FindFirst(JwtClaimTypes.Subject)?.Value))
234234
{
235235
var error = "sub claim from userinfo endpoint is different than sub claim from identity token.";
236236
_logger.LogError(error);
@@ -430,7 +430,7 @@ internal async Task EnsureProviderInformationAsync(CancellationToken cancellatio
430430

431431
_logger.LogDebug("Successfully loaded discovery document");
432432
_logger.LogDebug("Loaded keyset from {jwks_uri}", disco.JwksUri);
433-
var kids = disco.KeySet?.Keys?.Select(k => k.Kid);
433+
var kids = disco.KeySet?.Keys.Select(k => k.Kid);
434434
if (kids != null)
435435
{
436436
_logger.LogDebug($"Keyset contains the following kids: {string.Join(",", kids)}");
@@ -505,6 +505,6 @@ internal ClaimsPrincipal ProcessClaims(ClaimsPrincipal user, IEnumerable<Claim>
505505
userClaims = combinedClaims.ToList();
506506
}
507507

508-
return new ClaimsPrincipal(new ClaimsIdentity(userClaims, user.Identity.AuthenticationType, user.Identities.First().NameClaimType, user.Identities.First().RoleClaimType));
508+
return new ClaimsPrincipal(new ClaimsIdentity(userClaims, user.Identity?.AuthenticationType, user.Identities.First().NameClaimType, user.Identities.First().RoleClaimType));
509509
}
510510
}

identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/DPoP/DPoPTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task dpop_tokens_should_be_passed_to_api()
9999

100100
ApiHost.ApiInvoked += ctx =>
101101
{
102-
ctx.User.Identity.IsAuthenticated.ShouldBeTrue();
102+
ctx.User.Identity?.IsAuthenticated.ShouldBeTrue();
103103
};
104104

105105
var apiResponse = await apiClient.GetAsync(ApiHost.Url("/api"), _ct);
@@ -129,7 +129,7 @@ public async Task when_nonce_required_nonce_should_be_used_for_api_endpoint()
129129

130130
ApiHost.ApiInvoked += ctx =>
131131
{
132-
ctx.User.Identity.IsAuthenticated.ShouldBeTrue();
132+
ctx.User.Identity?.IsAuthenticated.ShouldBeTrue();
133133
};
134134

135135
var apiResponse = await apiClient.GetAsync(ApiHost.Url("/api"), _ct);
@@ -175,10 +175,10 @@ public async Task when_nonce_required_client_assertion_factory_should_be_called_
175175
});
176176

177177
var assertionCallCount = 0;
178-
Func<Task<ClientAssertion>> factory = () =>
178+
var factory = () =>
179179
{
180180
assertionCallCount++;
181-
return Task.FromResult<ClientAssertion>(new ClientAssertion
181+
return Task.FromResult(new ClientAssertion
182182
{
183183
Type = OidcConstants.ClientAssertionTypes.JwtBearer,
184184
Value = $"assertion_{assertionCallCount}"

0 commit comments

Comments
 (0)