mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added UserModel, Added Login and Logout and authorization, Made dark mode default
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
using Novell.Directory.Ldap;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Berufsschule_HAM.Models;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Berufsschule_HAM.Services;
|
||||
public class LdapService : IDisposable
|
||||
public partial class LdapService : IDisposable
|
||||
{
|
||||
private readonly LdapOptions _opts;
|
||||
private readonly LdapConnection _conn;
|
||||
@@ -49,19 +53,30 @@ public class LdapService : IDisposable
|
||||
return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Dictionary<string, string>>> ListUsersAsync(string[] attributes)
|
||||
public async Task<IEnumerable<UserModel>> ListUsersAsync(string[] attributes)
|
||||
{
|
||||
return await ListObjectBy(UsersBaseDn, "", attributes);
|
||||
List<UserModel> returnValue = [];
|
||||
(await ListObjectBy(UsersBaseDn, "", attributes))
|
||||
.ToList()
|
||||
.ForEach(x =>
|
||||
returnValue.Add(
|
||||
new UserModel(x)
|
||||
{
|
||||
Uid = x["uid"]
|
||||
}
|
||||
)
|
||||
);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid)
|
||||
public async Task<UserModel> GetUserByUidAsync(string uid)
|
||||
{
|
||||
return (await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First();
|
||||
return new UserModel((await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First()) {Uid = uid};
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid, string[] attributes)
|
||||
public async Task<UserModel> GetUserByUidAsync(string uid, string[] attributes)
|
||||
{
|
||||
return (await ListObjectBy(UsersBaseDn, $"uid={uid}", attributes)).First();
|
||||
return new UserModel((await ListObjectBy(UsersBaseDn, $"uid={uid}", attributes)).First()) {Uid = uid};
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +101,46 @@ public class LdapService : IDisposable
|
||||
CreateObject(LocationsBaseDn, attributeSet);
|
||||
}
|
||||
|
||||
public async Task<UserAuthenticationResult> AuthenticateUser(string username, string password)
|
||||
{
|
||||
await ConnectAndBind();
|
||||
try
|
||||
{
|
||||
UserModel user = await GetUserByUidAsync(username);
|
||||
if (user.UserPassword is null)
|
||||
{
|
||||
return new() { Success = false, AuthenticationState = UserNotAuthenticatedReason.InvalidCredentials };
|
||||
}
|
||||
if (CompareStringToSha256(password, user.UserPassword))
|
||||
{
|
||||
return new() { Success = true};
|
||||
}
|
||||
return new() { Success = false, AuthenticationState = UserNotAuthenticatedReason.InvalidCredentials };
|
||||
}
|
||||
catch (LdapException)
|
||||
{
|
||||
return new() { Success = false, AuthenticationState = UserNotAuthenticatedReason.InvalidCredentials };
|
||||
}
|
||||
}
|
||||
|
||||
public bool CompareStringToSha256(string sourcePassword, string targetPasswordHashed)
|
||||
{
|
||||
byte[] sourcePasswordBytes = SHA256.HashData(Encoding.UTF8.GetBytes(sourcePassword));
|
||||
byte[] targetPasswordHashedBytes = Convert.FromBase64String(CurlyBracesRemover().Replace(targetPasswordHashed, ""));
|
||||
if (sourcePasswordBytes.Length != targetPasswordHashedBytes.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < sourcePasswordBytes.Length; i++)
|
||||
{
|
||||
if (sourcePasswordBytes[i] != targetPasswordHashedBytes[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private string PrependRDN(string rdn, string dn)
|
||||
{
|
||||
return rdn + "," + dn;
|
||||
@@ -166,4 +221,7 @@ public class LdapService : IDisposable
|
||||
_conn.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"\{.*?\}")]
|
||||
private static partial Regex CurlyBracesRemover();
|
||||
}
|
||||
Reference in New Issue
Block a user