From 0e9bb04b96025aafe9ed32e1de46cae0c902eae8 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 28 May 2024 06:56:25 +0300 Subject: [PATCH] feat: add setup token --- Endpoint/Common/Interfaces/ISetupToken.cs | 9 ++++++ .../General/SetupTokenService.cs | 28 +++++++++++++++++++ Endpoint/Program.cs | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 Endpoint/Common/Interfaces/ISetupToken.cs create mode 100644 Endpoint/Configuration/General/SetupTokenService.cs diff --git a/Endpoint/Common/Interfaces/ISetupToken.cs b/Endpoint/Common/Interfaces/ISetupToken.cs new file mode 100644 index 0000000..462b759 --- /dev/null +++ b/Endpoint/Common/Interfaces/ISetupToken.cs @@ -0,0 +1,9 @@ +using System; + +namespace Mirea.Api.Endpoint.Common.Interfaces; + +public interface ISetupToken +{ + bool MatchToken(ReadOnlySpan token); + void SetToken(ReadOnlySpan token); +} \ No newline at end of file diff --git a/Endpoint/Configuration/General/SetupTokenService.cs b/Endpoint/Configuration/General/SetupTokenService.cs new file mode 100644 index 0000000..4cc1216 --- /dev/null +++ b/Endpoint/Configuration/General/SetupTokenService.cs @@ -0,0 +1,28 @@ +using Mirea.Api.Endpoint.Common.Interfaces; +using System; + +namespace Mirea.Api.Endpoint.Configuration.General; + +public class SetupTokenService : ISetupToken +{ + public ReadOnlyMemory? Token { get; private set; } + + public bool MatchToken(ReadOnlySpan token) + { + if (Token == null || token.Length != Token.Value.Length) + return false; + + var token2 = Token.Value.Span; + + int result = 0; + for (int i = 0; i < Token.Value.Length; i++) + result |= token2[i] ^ token[i]; + + return result == 0; + } + + public void SetToken(ReadOnlySpan token) + { + Token = token.ToArray(); + } +} \ No newline at end of file diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index 7101c7e..4d79328 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Mirea.Api.DataAccess.Application; using Mirea.Api.DataAccess.Persistence; +using Mirea.Api.Endpoint.Common.Interfaces; using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Configuration; using Mirea.Api.Endpoint.Configuration.General; @@ -50,6 +51,7 @@ public class Program builder.Services.AddPersistence(builder.Configuration.Get()?.DbSettings?.ConnectionStringSql ?? string.Empty); builder.Services.AddControllers(); + builder.Services.AddSingleton(); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy =>