diff --git a/src/WindreamHub.Legacy.Client/Models/Authentication/Base64Credential.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/Base64Credential.cs new file mode 100644 index 0000000..b4828df --- /dev/null +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/Base64Credential.cs @@ -0,0 +1,9 @@ +namespace WindreamHub.Legacy.Client.Models.Authentication +{ + public class Base64Credential : ICredential + { + public Base64Credential(string authorizationHeader) => AuthorizationHeader = authorizationHeader; + + public string AuthorizationHeader { get; } + } +} \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorDetails.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorDetails.cs new file mode 100644 index 0000000..546c959 --- /dev/null +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorDetails.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorItem.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorItem.cs new file mode 100644 index 0000000..12ad748 --- /dev/null +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/ErrorItem.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Models/Authentication/ICredential.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/ICredential.cs new file mode 100644 index 0000000..d4285ab --- /dev/null +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/ICredential.cs @@ -0,0 +1,7 @@ +namespace WindreamHub.Legacy.Client.Models.Authentication +{ + public interface ICredential + { + string AuthorizationHeader { get; } + } +} \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Models/UserCredential.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/UserCredential.cs similarity index 58% rename from src/WindreamHub.Legacy.Client/Models/UserCredential.cs rename to src/WindreamHub.Legacy.Client/Models/Authentication/UserCredential.cs index 7575c8d..3d8844b 100644 --- a/src/WindreamHub.Legacy.Client/Models/UserCredential.cs +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/UserCredential.cs @@ -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; } } } \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Models/Authentication/ValidationResponse.cs b/src/WindreamHub.Legacy.Client/Models/Authentication/ValidationResponse.cs new file mode 100644 index 0000000..1603a1e --- /dev/null +++ b/src/WindreamHub.Legacy.Client/Models/Authentication/ValidationResponse.cs @@ -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 Errors { get; set; } + public bool HasErrors { get; set; } + } +} diff --git a/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs b/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs index eb17e45..7164799 100644 --- a/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs +++ b/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs @@ -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); } } \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/Routes/AuthenticationRouteService.cs b/src/WindreamHub.Legacy.Client/Routes/AuthenticationRouteService.cs index 6eec24f..36670fe 100644 --- a/src/WindreamHub.Legacy.Client/Routes/AuthenticationRouteService.cs +++ b/src/WindreamHub.Legacy.Client/Routes/AuthenticationRouteService.cs @@ -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 clientOptions) : base(client, cookieContainer, clientOptions) { } + + public async Task> IsValidUser() + { + return await FetchAsync(route: "/IsValidUser", HttpMethod.Get).ThenAsync(res => res.Simplify()); + } } } \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/WindreamClientService.cs b/src/WindreamHub.Legacy.Client/WindreamClientService.cs index ca16b7d..87181dd 100644 --- a/src/WindreamHub.Legacy.Client/WindreamClientService.cs +++ b/src/WindreamHub.Legacy.Client/WindreamClientService.cs @@ -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 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 AuthenticateAsync(string domain, string name, string password) + { + var uCredential = new UserCredential(domain, name, password); + return await AuthenticateAsync(uCredential); + } + + public async Task AuthenticateAsync(string authorizationHeader) + { + var uCredential = new Base64Credential(authorizationHeader); + return await AuthenticateAsync(uCredential); + } } } \ No newline at end of file diff --git a/src/WindreamHub.Legacy.Client/WindreamHub.Legacy.Client.csproj b/src/WindreamHub.Legacy.Client/WindreamHub.Legacy.Client.csproj index 0dcf71b..f652c0d 100644 --- a/src/WindreamHub.Legacy.Client/WindreamHub.Legacy.Client.csproj +++ b/src/WindreamHub.Legacy.Client/WindreamHub.Legacy.Client.csproj @@ -83,11 +83,16 @@ + + + + + - +