Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions OdectyMVC/BasicAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public BasicAuthenticationHandler(

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.ContainsKey("Authorization"))
if (!Request!.Headers.ContainsKey("Authorization"))
return Task.FromResult(AuthenticateResult.Fail("Missing Authorization Header"));

try
{
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]!);
if (!authHeader.Scheme.Equals("Basic", StringComparison.OrdinalIgnoreCase))
return Task.FromResult(AuthenticateResult.Fail("Invalid scheme"));

var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':');
var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter ?? string.Empty)).Split(':');
var username = credentials[0];
var password = credentials[1];

Expand Down
4 changes: 2 additions & 2 deletions OdectyMVC/DataLayer/GaugeListModelRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public async Task<IActionResult> GetLastPhoto(int id, CancellationToken cancella
private sealed class OdectyStatGaugeDto
{
public int Id { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string? Description { get; set; }
public required string Type { get; set; }
public decimal LastValue { get; set; }
public DateTime? LastMeasurementAt { get; set; }
public bool HasPhoto { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion OdectyMVC/DataLayer/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OdectyMVC.DataLayer
{
public class MessageQueue : IMessageQueue
{
private readonly IChannel model;
private readonly IChannel? model;
private readonly IOptions<RabbitMQSettings> options;

public MessageQueue(RabbitMQProvider rabbitMQProvider, IOptions<RabbitMQSettings> options)
Expand Down
6 changes: 3 additions & 3 deletions OdectyMVC/DataLayer/QueueMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class QueueMapping
{
public string QueueName { get; set; }
public string RoutingKey { get; set; }
public string ExchangeName { get; set; }
public required string QueueName { get; set; }
public string? RoutingKey { get; set; }
public string? ExchangeName { get; set; }
}
29 changes: 18 additions & 11 deletions OdectyMVC/DataLayer/RabbitMQProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OdectyMVC.DataLayer;

public class RabbitMQProvider : IDisposable
{
private readonly IConnection connection;
private readonly IConnection? connection;
private readonly IOptions<RabbitMQSettings> options;
private readonly bool connected = false;
private bool first = true;
Expand All @@ -15,16 +15,15 @@ public class RabbitMQProvider : IDisposable

public RabbitMQProvider(IOptions<RabbitMQSettings> options)
{
this.options = options;
try
{
var factory = new ConnectionFactory();
factory.HostName = options.Value.HostName;
factory.UserName = options.Value.UserName;
factory.Password = options.Value.Password;
factory.VirtualHost = options.Value.VirtualHost;

factory.HostName = this.options.Value.HostName;
factory.UserName = this.options.Value.UserName;
factory.Password = this.options.Value.Password;
factory.VirtualHost = this.options.Value.VirtualHost;
connection = factory.CreateConnectionAsync().Result;
this.options = options;
connected = true;
}
catch
Expand All @@ -33,17 +32,17 @@ public RabbitMQProvider(IOptions<RabbitMQSettings> options)
}
}

public async Task<IChannel> CreateModel()
public async Task<IChannel?> CreateModel()
{
if (connected)
{
if (first)
{
using var model = await connection.CreateChannelAsync();
using var model = await connection!.CreateChannelAsync();
await model.ExchangeDeclareAsync(options.Value.ExchangeName, ExchangeType.Direct, true, false, null);
foreach (var exchange in options.Value.QueueMappings.Select(q => q.ExchangeName).Distinct())
{
if (exchange.StartsWith("amq."))
if (exchange == null || exchange.StartsWith("amq."))
{
continue;
}
Expand All @@ -55,12 +54,20 @@ public async Task<IChannel> CreateModel()
}
foreach (var map in options.Value.QueueMappings)
{
if (map.ExchangeName == null || map.RoutingKey == null)
{
continue;
}
await model.QueueBindAsync(map.QueueName, map.ExchangeName, map.RoutingKey);
}
first = false;
}
}
return await connection.CreateChannelAsync();
if (connection != null)
{
return await connection.CreateChannelAsync();
}
return null;
}

public void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions OdectyMVC/Models/GaugeListModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace OdectyMVC.Models
public class GaugeListModel
{
public int Id { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string? Description { get; set; }
public required string Type { get; set; }
public decimal LastValue { get; set; }
public DateTime? LastMeasurementAt { get; set; }
public bool HasPhoto { get; set; }
Expand Down
20 changes: 10 additions & 10 deletions OdectyMVC/OdectyMVC.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="9.0.16" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.14.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.8" />
<PackageReference Include="Microsoft.Identity.Web" Version="4.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.3" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />
<PackageReference Include="RabbitMQ.Client" Version="7.1.2" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.1" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.1" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.0" />
</ItemGroup>
<PropertyGroup>
<EnableSourceControlManagerQueries>false</EnableSourceControlManagerQueries>
Expand Down
4 changes: 2 additions & 2 deletions OdectyMVC/Options/BasicAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class BasicAuthentication
{
public string Username { get; set; }
public string Password { get; set; }
public required string Username { get; set; }
public required string Password { get; set; }
}
2 changes: 1 addition & 1 deletion OdectyMVC/Options/OdectyStatSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace OdectyMVC.Options;

public class OdectyStatSettings
{
public string BaseUrl { get; set; }
public required string BaseUrl { get; set; }
}
10 changes: 5 additions & 5 deletions OdectyMVC/Options/RabbitMQSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace OdectyMVC.Options;

public class RabbitMQSettings
{
public string HostName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string VirtualHost { get; set; }
public string ExchangeName { get; set; }
public required string HostName { get; set; }
public required string UserName { get; set; }
public required string Password { get; set; }
public required string VirtualHost { get; set; }
public required string ExchangeName { get; set; }
public List<QueueMapping> QueueMappings { get; set; } = new();
}
21 changes: 8 additions & 13 deletions OdectyMVC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;
using OdectyMVC;
using OdectyMVC.Application;
using OdectyMVC.Contracts;
Expand Down Expand Up @@ -99,6 +99,11 @@
{
OnTokenValidated = context =>
{
if (context.Principal == null)
{
context.Fail("Unauthorized access");
return Task.CompletedTask;
}
var email = context.Principal.FindFirst("preferred_username")?.Value
?? context.Principal.FindFirst(ClaimTypes.Email)?.Value;
if (string.IsNullOrEmpty(email) || !allowedEmails.Contains(email.ToLowerInvariant()))
Expand All @@ -117,19 +122,9 @@
Type = SecuritySchemeType.Http,
Scheme = "basic",
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Id = "basic",
Type = ReferenceType.SecurityScheme
}
},
Array.Empty<string>()
}
{ new OpenApiSecuritySchemeReference("basic", document), Array.Empty<string>().ToList() }
});
});
#else
Expand Down
Loading