feat: Benutzer-Authentifizierungsmethoden und Validierungslogik hinzugefügt

- `IsValidUser`-Methode in `AuthenticationRouteService` hinzugefügt, um Benutzer über die `/IsValidUser`-Route zu validieren.
- Drei neue `AuthenticateAsync`-Methoden in `WindreamClientService` implementiert, um verschiedene Benutzerauthentifizierungen zu unterstützen (ICredential, Domain/Name/Passwort und Base64-Authorization-Header).
- HttpClient aktualisiert, um Authorization-Header für die Authentifizierung hinzuzufügen.
This commit is contained in:
Developer 02
2024-09-12 20:20:49 +02:00
parent c233ab0ed7
commit 5062930d5b
10 changed files with 99 additions and 7 deletions

View File

@@ -0,0 +1,9 @@
namespace WindreamHub.Legacy.Client.Models.Authentication
{
public class Base64Credential : ICredential
{
public Base64Credential(string authorizationHeader) => AuthorizationHeader = authorizationHeader;
public string AuthorizationHeader { get; }
}
}

View File

@@ -0,0 +1,9 @@
namespace WindreamHub.Legacy.Client.Models.Authentication
{
public class ErrorDetails
{
public ErrorItem Item { get; set; }
public string Message { get; set; }
public int ErrorCode { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
namespace WindreamHub.Legacy.Client.Models.Authentication
{
public class ErrorItem
{
public int Entity { get; set; }
public int Id { get; set; }
public string Location { get; set; }
public string Name { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace WindreamHub.Legacy.Client.Models.Authentication
{
public interface ICredential
{
string AuthorizationHeader { get; }
}
}

View File

@@ -1,15 +1,16 @@
using System;
using System.Text;
using WindreamHub.Legacy.Client.Models.Authentication;
namespace WindreamHub.Legacy.Client.Models
{
public class UserCredential
public class UserCredential : ICredential
{
public readonly string Domain;
public readonly string Name;
public readonly string AuthorizationHeader;
public string AuthorizationHeader { get; }
public UserCredential(string domain, string name, string password)
{
@@ -20,9 +21,9 @@ namespace WindreamHub.Legacy.Client.Models
private static string ConvertToBase64(string domain, string username, string password)
{
string credentials = $"{domain}\\{username}:{password}";
byte[] bytes = Encoding.UTF8.GetBytes(credentials);
return Convert.ToBase64String(bytes);
var credentials = $"{domain}\\{username}:{password}";
var base64Credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
return base64Credentials;
}
}
}

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace WindreamHub.Legacy.Client.Models.Authentication
{
public class ValidationResponse
{
public int UserID { get; set; }
public string UserName { get; set; }
public string FullUserName { get; set; }
public bool IsValidUser { get; set; }
public ErrorDetails Error { get; set; }
public List<ErrorDetails> Errors { get; set; }
public bool HasErrors { get; set; }
}
}

View File

@@ -3,6 +3,7 @@ using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace WindreamHub.Legacy.Client.Models
{
@@ -89,5 +90,7 @@ namespace WindreamHub.Legacy.Client.Models
error?.Invoke(res.Error);
});
}
public static string Stringify(this object model) => JsonConvert.SerializeObject(model);
}
}

View File

@@ -1,6 +1,11 @@
using Microsoft.Extensions.Options;
using DigitalData.Core.Legacy.Client;
using Microsoft.Extensions.Options;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using WindreamHub.Legacy.Client.Models;
using WindreamHub.Legacy.Client.Models.Authentication;
namespace WindreamHub.Legacy.Client.Routes
{
@@ -9,5 +14,10 @@ namespace WindreamHub.Legacy.Client.Routes
public AuthenticationRouteService(HttpClient client, CookieContainer cookieContainer, IOptions<WindreamClientOptions> clientOptions) : base(client, cookieContainer, clientOptions)
{
}
public async Task<SimplifiedResponse<ValidationResponse, object>> IsValidUser()
{
return await FetchAsync(route: "/IsValidUser", HttpMethod.Get).ThenAsync(res => res.Simplify<ValidationResponse, object>());
}
}
}

View File

@@ -2,6 +2,10 @@
using Microsoft.Extensions.Options;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using WindreamHub.Legacy.Client.Models.Authentication;
using WindreamHub.Legacy.Client.Models;
using WindreamHub.Legacy.Client.Route;
using WindreamHub.Legacy.Client.Routes;
@@ -23,5 +27,24 @@ namespace WindreamHub.Legacy.Client
public SystemDetailsRouteService SystemDetails { get; }
public AuthenticationRouteService Authentication { get; }
public async Task<bool> AuthenticateAsync(ICredential credential)
{
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credential.AuthorizationHeader);
var res = await Authentication.IsValidUser();
return res.Ok && res.Data.IsValidUser;
}
public async Task<bool> AuthenticateAsync(string domain, string name, string password)
{
var uCredential = new UserCredential(domain, name, password);
return await AuthenticateAsync(uCredential);
}
public async Task<bool> AuthenticateAsync(string authorizationHeader)
{
var uCredential = new Base64Credential(authorizationHeader);
return await AuthenticateAsync(uCredential);
}
}
}

View File

@@ -83,11 +83,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DIExtensions.cs" />
<Compile Include="Models\Authentication\ErrorDetails.cs" />
<Compile Include="Models\Authentication\ErrorItem.cs" />
<Compile Include="Models\Authentication\ICredential.cs" />
<Compile Include="Models\Authentication\ValidationResponse.cs" />
<Compile Include="Models\Authentication\Base64Credential.cs" />
<Compile Include="Models\ModelExtensions.cs" />
<Compile Include="Models\SimplifiedResponse.cs" />
<Compile Include="Models\SystemDetails.cs" />
<Compile Include="Models\SystemDetailsResponse.cs" />
<Compile Include="Models\UserCredential.cs" />
<Compile Include="Models\Authentication\UserCredential.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Routes\AuthenticationRouteService.cs" />
<Compile Include="Routes\BaseRouteService.cs" />