From daf506b4dfdd0500f9c54180c2b4b6e79ead1dde Mon Sep 17 00:00:00 2001 From: nikiforovall Date: Sat, 4 May 2024 22:59:34 +0300 Subject: [PATCH] No warnings --- .editorconfig | 2 + .../AuthGettingStarted/ApplicationLogger.cs | 5 +- .../ServiceCollectionExtensions.OpenApi.cs | 4 +- samples/Blazor/Client/Pages/FetchData.razor | 2 +- samples/Blazor/Server/Blazor.Server.csproj | 1 - .../Controllers/WeatherForecastController.cs | 41 ++++----- samples/Blazor/Shared/Blazor.Shared.csproj | 2 + samples/Blazor/Shared/WeatherForecast.cs | 4 +- .../Admin/IKeycloakUserClient.cs | 2 +- .../Admin/KeycloakClient.cs | 24 +++--- .../ErrorResponse.cs | 6 +- .../HttpResponseExtensions.cs | 21 +++-- .../Protection/KeycloakProtectionClient.cs | 13 +-- .../Requests/GetResourcesRequestParameters.cs | 2 +- .../ServiceCollectionExtensions.cs | 84 +++++++++---------- .../Utils/QueryBuilder.cs | 13 --- .../Playground.cs | 2 + .../KeycloakUserClientTests.cs | 2 +- tests/TestWebApi/TestWebApi.csproj | 4 +- 19 files changed, 119 insertions(+), 115 deletions(-) diff --git a/.editorconfig b/.editorconfig index b7d458e4..fd2214dd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -161,6 +161,8 @@ dotnet_diagnostic.IDE0045.severity = suggestion dotnet_style_prefer_conditional_expression_over_return = false:suggestion dotnet_diagnostic.IDE0046.severity = suggestion dotnet_style_prefer_compound_assignment = true:warning + +dotnet_diagnostic.CA1848.severity = suggestion # Null-checking preferences # https://docs.microsoft.com/visualstudio/ide/editorconfig-language-conventions#null-checking-preferences dotnet_style_coalesce_expression = true:warning diff --git a/samples/AuthGettingStarted/ApplicationLogger.cs b/samples/AuthGettingStarted/ApplicationLogger.cs index 146ee766..85e61d45 100644 --- a/samples/AuthGettingStarted/ApplicationLogger.cs +++ b/samples/AuthGettingStarted/ApplicationLogger.cs @@ -7,12 +7,15 @@ public static class ApplicationLogger { public static void ConfigureLogger(this ConfigureHostBuilder host) { +#pragma warning disable CA1305 // Specify IFormatProvider Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Console( outputTemplate: "[{Level:u4}] |{SourceContext,30}({EventId})| {Message:lj}{NewLine}{Exception}", - restrictedToMinimumLevel: LogEventLevel.Debug) + restrictedToMinimumLevel: LogEventLevel.Debug + ) .CreateBootstrapLogger(); +#pragma warning restore CA1305 // Specify IFormatProvider host.UseSerilog(); } diff --git a/samples/AuthorizationGettingStarted/ServiceCollectionExtensions.OpenApi.cs b/samples/AuthorizationGettingStarted/ServiceCollectionExtensions.OpenApi.cs index 925253aa..90a1ab60 100644 --- a/samples/AuthorizationGettingStarted/ServiceCollectionExtensions.OpenApi.cs +++ b/samples/AuthorizationGettingStarted/ServiceCollectionExtensions.OpenApi.cs @@ -31,7 +31,7 @@ IConfiguration configuration Implicit = new OpenApiOAuthFlow { AuthorizationUrl = new Uri( - $"{options.KeycloakUrlRealm}/protocol/openid-connect/auth" + $"{options!.KeycloakUrlRealm}/protocol/openid-connect/auth" ), TokenUrl = new Uri( $"{options.KeycloakUrlRealm}/protocol/openid-connect/token" @@ -55,7 +55,7 @@ IConfiguration configuration { KeycloakAuthenticationOptions options = new(); - configuration.BindKeycloakOptions(options); + configuration.BindKeycloakOptions(options); app.UseSwagger(); app.UseSwaggerUI(s => s.OAuthClientId(options.Resource)); diff --git a/samples/Blazor/Client/Pages/FetchData.razor b/samples/Blazor/Client/Pages/FetchData.razor index a2161894..857f50e2 100644 --- a/samples/Blazor/Client/Pages/FetchData.razor +++ b/samples/Blazor/Client/Pages/FetchData.razor @@ -1,5 +1,5 @@ @page "/fetchdata" -@using Blazor.Shared +@using BlazorSample.Common @using Microsoft.AspNetCore.Authorization @attribute [Authorize] diff --git a/samples/Blazor/Server/Blazor.Server.csproj b/samples/Blazor/Server/Blazor.Server.csproj index 6ce26a97..84cd9925 100644 --- a/samples/Blazor/Server/Blazor.Server.csproj +++ b/samples/Blazor/Server/Blazor.Server.csproj @@ -1,5 +1,4 @@ - diff --git a/samples/Blazor/Server/Controllers/WeatherForecastController.cs b/samples/Blazor/Server/Controllers/WeatherForecastController.cs index ec9dcd2c..77494571 100644 --- a/samples/Blazor/Server/Controllers/WeatherForecastController.cs +++ b/samples/Blazor/Server/Controllers/WeatherForecastController.cs @@ -1,5 +1,6 @@ namespace Blazor.Server.Controllers; -using Blazor.Shared; + +using BlazorSample.Common; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -10,25 +11,27 @@ public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + "Freezing", + "Bracing", + "Chilly", + "Cool", + "Mild", + "Warm", + "Balmy", + "Hot", + "Sweltering", + "Scorching" }; - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - [HttpGet] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } + public IEnumerable Get() => + Enumerable + .Range(1, 5) + .Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); } diff --git a/samples/Blazor/Shared/Blazor.Shared.csproj b/samples/Blazor/Shared/Blazor.Shared.csproj index 50207132..c14f0ea8 100644 --- a/samples/Blazor/Shared/Blazor.Shared.csproj +++ b/samples/Blazor/Shared/Blazor.Shared.csproj @@ -2,6 +2,8 @@ false + BlazorSample + CS7022 diff --git a/samples/Blazor/Shared/WeatherForecast.cs b/samples/Blazor/Shared/WeatherForecast.cs index d9f694e4..b377cc81 100644 --- a/samples/Blazor/Shared/WeatherForecast.cs +++ b/samples/Blazor/Shared/WeatherForecast.cs @@ -1,4 +1,4 @@ -namespace Blazor.Shared; +namespace BlazorSample.Common; public class WeatherForecast { @@ -8,5 +8,5 @@ public class WeatherForecast public string? Summary { get; set; } - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + public int TemperatureF => 32 + (int)(this.TemperatureC / 0.5556); } diff --git a/src/Keycloak.AuthServices.Sdk/Admin/IKeycloakUserClient.cs b/src/Keycloak.AuthServices.Sdk/Admin/IKeycloakUserClient.cs index 77d92e5d..3193842e 100644 --- a/src/Keycloak.AuthServices.Sdk/Admin/IKeycloakUserClient.cs +++ b/src/Keycloak.AuthServices.Sdk/Admin/IKeycloakUserClient.cs @@ -65,7 +65,7 @@ async Task GetUserAsync( return await response.GetResponseAsync(cancellationToken) ?? new(); } - // + /// /// Get representation of a user. /// /// Realm name (not ID). diff --git a/src/Keycloak.AuthServices.Sdk/Admin/KeycloakClient.cs b/src/Keycloak.AuthServices.Sdk/Admin/KeycloakClient.cs index 746fac28..8ec762b2 100644 --- a/src/Keycloak.AuthServices.Sdk/Admin/KeycloakClient.cs +++ b/src/Keycloak.AuthServices.Sdk/Admin/KeycloakClient.cs @@ -1,5 +1,6 @@ namespace Keycloak.AuthServices.Sdk.Admin; +using System.Globalization; using System.Net.Http.Json; using Keycloak.AuthServices.Sdk.Admin.Models; using Keycloak.AuthServices.Sdk.Admin.Requests.Groups; @@ -7,14 +8,14 @@ using Keycloak.AuthServices.Sdk.Utils; /// -/// TBD: +/// Represents a client for interacting with the Keycloak Admin API. /// public partial class KeycloakClient : IKeycloakClient { private readonly HttpClient httpClient; /// - /// TBD: + /// Initializes a new instance of the class. /// /// public KeycloakClient(HttpClient httpClient) => this.httpClient = httpClient; @@ -204,7 +205,7 @@ public async Task ExecuteActionsEmailWithResponseAsync( if (request.Lifespan.HasValue) { - queryBuilder.Add("lifespan", request.Lifespan?.ToString()!); + queryBuilder.Add("lifespan", request.Lifespan?.ToString(CultureInfo.InvariantCulture)!); } var url = path + queryBuilder.ToQueryString(); @@ -233,17 +234,17 @@ public async Task GetUserGroupsWithResponseAsync( parameters ??= new(); if (parameters.BriefRepresentation.HasValue) { - queryBuilder.Add("briefRepresentation", parameters.BriefRepresentation.ToString()); + queryBuilder.Add("briefRepresentation", parameters.BriefRepresentation?.ToString()!); } if (parameters.First.HasValue) { - queryBuilder.Add("first", parameters.First.ToString()); + queryBuilder.Add("first", parameters.First?.ToString(CultureInfo.InvariantCulture)!); } if (parameters.Max.HasValue) { - queryBuilder.Add("max", parameters.Max.ToString()!); + queryBuilder.Add("max", parameters.Max?.ToString(CultureInfo.InvariantCulture)!); } var url = path + queryBuilder.ToQueryString(); @@ -310,22 +311,25 @@ public async Task GetGroupsWithResponseAsync( parameters ??= new(); if (parameters.BriefRepresentation.HasValue) { - queryBuilder.Add("briefRepresentation", parameters.BriefRepresentation.ToString()); + queryBuilder.Add( + "briefRepresentation", + parameters.BriefRepresentation?.ToString(CultureInfo.InvariantCulture)! + ); } if (parameters.First.HasValue) { - queryBuilder.Add("first", parameters.First.ToString()); + queryBuilder.Add("first", parameters.First?.ToString(CultureInfo.InvariantCulture)!); } if (parameters.Exact.HasValue) { - queryBuilder.Add("exact", parameters.Exact.ToString()); + queryBuilder.Add("exact", parameters.Exact?.ToString(CultureInfo.InvariantCulture)!); } if (parameters.Max.HasValue) { - queryBuilder.Add("max", parameters.Max.ToString()!); + queryBuilder.Add("max", parameters.Max?.ToString(CultureInfo.InvariantCulture)!); } if (parameters.Search is not null) diff --git a/src/Keycloak.AuthServices.Sdk/ErrorResponse.cs b/src/Keycloak.AuthServices.Sdk/ErrorResponse.cs index 6a30df5d..3e547895 100644 --- a/src/Keycloak.AuthServices.Sdk/ErrorResponse.cs +++ b/src/Keycloak.AuthServices.Sdk/ErrorResponse.cs @@ -3,18 +3,18 @@ namespace Keycloak.AuthServices.Sdk; using System.Text.Json.Serialization; /// -/// TBD: +/// Represents an error response from Keycloak. /// public sealed record ErrorResponse { /// - /// TBD: + /// Gets or sets the error code. /// [JsonPropertyName("error")] public string Error { get; init; } = default!; /// - /// TBD: + /// Gets or sets the error description. /// [JsonPropertyName("error_description")] public string ErrorDescription { get; init; } = default!; diff --git a/src/Keycloak.AuthServices.Sdk/HttpResponseExtensions.cs b/src/Keycloak.AuthServices.Sdk/HttpResponseExtensions.cs index f935f01d..d0200fb0 100644 --- a/src/Keycloak.AuthServices.Sdk/HttpResponseExtensions.cs +++ b/src/Keycloak.AuthServices.Sdk/HttpResponseExtensions.cs @@ -4,17 +4,17 @@ namespace Keycloak.AuthServices.Sdk; using System.Text.Json; /// -/// TBD: +/// Provides extension methods for handling HTTP responses. /// public static class HttpResponseExtensions { /// - /// TBD: + /// Reads the HTTP response content as JSON and deserializes it into the specified type. /// - /// - /// - /// - /// + /// The type to deserialize the response content into. + /// The HTTP response message. + /// The cancellation token. + /// The deserialized response content. public static async Task GetResponseAsync( this HttpResponseMessage response, CancellationToken cancellationToken = default @@ -30,12 +30,11 @@ public static class HttpResponseExtensions } /// - /// TBD: + /// Ensures that the HTTP response is successful, otherwise throws an exception. /// - /// - /// - /// - /// + /// The HTTP response message. + /// The cancellation token. + /// Thrown when the response is not successful. public static async Task EnsureResponseAsync( this HttpResponseMessage response, CancellationToken cancellationToken = default diff --git a/src/Keycloak.AuthServices.Sdk/Protection/KeycloakProtectionClient.cs b/src/Keycloak.AuthServices.Sdk/Protection/KeycloakProtectionClient.cs index e00a303e..b02efe20 100644 --- a/src/Keycloak.AuthServices.Sdk/Protection/KeycloakProtectionClient.cs +++ b/src/Keycloak.AuthServices.Sdk/Protection/KeycloakProtectionClient.cs @@ -1,21 +1,22 @@ namespace Keycloak.AuthServices.Sdk.Protection; +using System.Globalization; using System.Net.Http.Json; using Keycloak.AuthServices.Sdk.Protection.Models; using Keycloak.AuthServices.Sdk.Protection.Requests; using Keycloak.AuthServices.Sdk.Utils; /// -/// TBD: +/// Represents a client for interacting with Keycloak Protection API services. /// public partial class KeycloakProtectionClient : IKeycloakProtectionClient { private readonly HttpClient httpClient; /// - /// TBD: + /// Initializes a new instance of the class. /// - /// + /// The HTTP client used for making requests. public KeycloakProtectionClient(HttpClient httpClient) => this.httpClient = httpClient; #region IKeycloakProtectedResourceClient @@ -118,7 +119,7 @@ private async Task GetResourcesWithResponseCoreAsync( if (parameters.ExactName.HasValue) { - queryBuilder.Add("exactName", parameters.ExactName.ToString()); + queryBuilder.Add("exactName", parameters.ExactName?.ToString()!); } if (parameters.Uri is not null) @@ -199,12 +200,12 @@ public async Task GetPoliciesWithResponseAsync( parameters ??= new(); if (parameters.First.HasValue) { - queryBuilder.Add("first", parameters.First.ToString()); + queryBuilder.Add("first", parameters.First?.ToString(CultureInfo.InvariantCulture)!); } if (parameters.Max.HasValue) { - queryBuilder.Add("max", parameters.Max.ToString()!); + queryBuilder.Add("max", parameters.Max?.ToString(CultureInfo.InvariantCulture)!); } if (!string.IsNullOrEmpty(parameters.PermissionName)) diff --git a/src/Keycloak.AuthServices.Sdk/Protection/Requests/GetResourcesRequestParameters.cs b/src/Keycloak.AuthServices.Sdk/Protection/Requests/GetResourcesRequestParameters.cs index 62d769ba..ce010597 100644 --- a/src/Keycloak.AuthServices.Sdk/Protection/Requests/GetResourcesRequestParameters.cs +++ b/src/Keycloak.AuthServices.Sdk/Protection/Requests/GetResourcesRequestParameters.cs @@ -1,7 +1,7 @@ namespace Keycloak.AuthServices.Sdk.Protection.Requests; /// -/// Optional parameters for the endpoint. +/// Optional parameters for the endpoint. /// public class GetResourcesRequestParameters { diff --git a/src/Keycloak.AuthServices.Sdk/ServiceCollectionExtensions.cs b/src/Keycloak.AuthServices.Sdk/ServiceCollectionExtensions.cs index d1af6805..4ed06515 100644 --- a/src/Keycloak.AuthServices.Sdk/ServiceCollectionExtensions.cs +++ b/src/Keycloak.AuthServices.Sdk/ServiceCollectionExtensions.cs @@ -13,13 +13,13 @@ namespace Keycloak.AuthServices.Sdk; public static class ServiceCollectionExtensions { /// - /// TBD: + /// Adds an HttpClient for Keycloak Admin API to the service collection. /// - /// - /// - /// - /// - /// + /// The IServiceCollection to add the HttpClient to. + /// The IConfiguration instance to bind the Keycloak options from. + /// An optional action to configure the HttpClient. + /// The name of the configuration section containing the Keycloak client options. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakAdminHttpClient( this IServiceCollection services, IConfiguration configuration, @@ -32,12 +32,12 @@ public static IHttpClientBuilder AddKeycloakAdminHttpClient( ); /// - /// TBD: + /// Adds an HttpClient for Keycloak Admin API to the service collection. /// - /// - /// - /// - /// + /// The IServiceCollection to add the HttpClient to. + /// The IConfigurationSection to bind the Keycloak options from. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakAdminHttpClient( this IServiceCollection services, IConfigurationSection configurationSection, @@ -49,12 +49,12 @@ public static IHttpClientBuilder AddKeycloakAdminHttpClient( ); /// - /// TBD: + /// Adds an HttpClient for Keycloak Admin API to the service collection. /// - /// - /// - /// - /// v + /// The IServiceCollection to add the HttpClient to. + /// An action to configure the Keycloak client options. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakAdminHttpClient( this IServiceCollection services, Action configureKeycloakOptions, @@ -84,12 +84,12 @@ public static IHttpClientBuilder AddKeycloakAdminHttpClient( } /// - /// TBD: + /// Adds an HttpClient for Keycloak Admin API to the service collection. /// - /// - /// - /// - /// v + /// The IServiceCollection to add the HttpClient to. + /// The Keycloak client options. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakAdminHttpClient( this IServiceCollection services, KeycloakAdminClientOptions keycloakOptions, @@ -112,13 +112,13 @@ void configureKeycloakOptions(KeycloakAdminClientOptions options) } /// - /// TBD: + /// Adds an HttpClient for Keycloak Protection API to the service collection. /// - /// - /// - /// - /// - /// + /// The IServiceCollection to add the HttpClient to. + /// The IConfiguration instance to bind the Keycloak options from. + /// An optional action to configure the HttpClient. + /// The name of the configuration section containing the Keycloak client options. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakProtectionHttpClient( this IServiceCollection services, IConfiguration configuration, @@ -131,12 +131,12 @@ public static IHttpClientBuilder AddKeycloakProtectionHttpClient( ); /// - /// TBD: + /// Adds an HttpClient for Keycloak Protection API to the service collection. /// - /// - /// - /// - /// + /// The IServiceCollection to add the HttpClient to. + /// The IConfigurationSection to bind the Keycloak options from. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakProtectionHttpClient( this IServiceCollection services, IConfigurationSection configurationSection, @@ -148,12 +148,12 @@ public static IHttpClientBuilder AddKeycloakProtectionHttpClient( ); /// - /// TBD: + /// Adds an HttpClient for Keycloak Protection API to the service collection. /// - /// - /// - /// - /// v + /// The IServiceCollection to add the HttpClient to. + /// An action to configure the Keycloak client options. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakProtectionHttpClient( this IServiceCollection services, Action configureKeycloakOptions, @@ -186,12 +186,12 @@ public static IHttpClientBuilder AddKeycloakProtectionHttpClient( } /// - /// TBD: + /// Adds an HttpClient for Keycloak Protection API to the service collection. /// - /// - /// - /// - /// v + /// The IServiceCollection to add the HttpClient to. + /// The Keycloak client options. + /// An optional action to configure the HttpClient. + /// The IHttpClientBuilder for further configuration. public static IHttpClientBuilder AddKeycloakProtectionHttpClient( this IServiceCollection services, KeycloakProtectionClientOptions keycloakOptions, diff --git a/src/Keycloak.AuthServices.Sdk/Utils/QueryBuilder.cs b/src/Keycloak.AuthServices.Sdk/Utils/QueryBuilder.cs index f2b4231f..1c2e2c52 100644 --- a/src/Keycloak.AuthServices.Sdk/Utils/QueryBuilder.cs +++ b/src/Keycloak.AuthServices.Sdk/Utils/QueryBuilder.cs @@ -42,19 +42,6 @@ public QueryBuilder(IEnumerable> parameters) ) ) { } - /// - /// Adds a query string token to the instance. - /// - /// The query key. - /// The sequence of query values. - // public void Add(string key, IEnumerable values) - // { - // foreach (var value in values) - // { - // this.@params.Add(new KeyValuePair(key, value)); - // } - // } - /// /// Adds a query string token to the instance. /// diff --git a/tests/Keycloak.AuthServices.IntegrationTests/Playground.cs b/tests/Keycloak.AuthServices.IntegrationTests/Playground.cs index ee3e4c94..c3a3eb3a 100644 --- a/tests/Keycloak.AuthServices.IntegrationTests/Playground.cs +++ b/tests/Keycloak.AuthServices.IntegrationTests/Playground.cs @@ -15,7 +15,9 @@ public class Playground(ITestOutputHelper testOutputHelper) { private static readonly string AppSettings = "appsettings.json"; +#pragma warning disable xUnit1004 // Test methods should not be skipped [Fact(Skip = "Playground Test")] +#pragma warning restore xUnit1004 // Test methods should not be skipped public async Task PlaygroundRequireProtectedResource_Scopes_Verified() { var policyName = "RequireProtectedResource"; diff --git a/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakUserClientTests.cs b/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakUserClientTests.cs index 05f91615..362a95e1 100644 --- a/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakUserClientTests.cs +++ b/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakUserClientTests.cs @@ -32,7 +32,7 @@ public async Task GetUserShouldCallCorrectEndpoint(UserRepresentation userFixtur this.handler.Expect(HttpMethod.Get, $"/admin/realms/master/users/{userId}") .Respond(HttpStatusCode.OK, MediaType, JsonSerializer.Serialize(userFixture)); - var user = await this.keycloakUserClient.GetUserAsync("master", userId.ToString()); + var user = await this.keycloakUserClient.GetUserAsync("master", userId!.ToString()); user.Id.Should().Be(userId.ToString()); this.handler.VerifyNoOutstandingExpectation(); diff --git a/tests/TestWebApi/TestWebApi.csproj b/tests/TestWebApi/TestWebApi.csproj index 375cccf7..3c003374 100644 --- a/tests/TestWebApi/TestWebApi.csproj +++ b/tests/TestWebApi/TestWebApi.csproj @@ -1,3 +1,5 @@ - + + CS7022 +