add pending enrollment identifier

This commit is contained in:
Gary Sharp
2024-01-24 16:38:50 +11:00
parent 37e2e5a08c
commit f90eda4101
9 changed files with 186 additions and 124 deletions
@@ -81,14 +81,15 @@ namespace Disco.Services.Devices.Enrolment
System.Enum.GetName(EnrolmentType.GetType(), EnrolmentType)
});
}
public static void LogSessionPending(string SessionId, string HostId, EnrolmentTypes EnrolmentType, string Reason)
public static void LogSessionPending(string SessionId, string HostId, EnrolmentTypes EnrolmentType, string Reason, string Identifier)
{
Log(EventTypeIds.SessionPending, new object[]
{
SessionId,
HostId,
System.Enum.GetName(EnrolmentType.GetType(), EnrolmentType),
Reason
Reason,
Identifier
});
}
public static void LogSessionPendingApproved(string SessionId, string Username, string Reason)
@@ -383,7 +384,7 @@ namespace Disco.Services.Devices.Enrolment
Id = (int)EventTypeIds.SessionPending,
ModuleId = _ModuleId,
Name = "Session Pending",
Format = "Pending '{2}' Enrollment for {1} (Session# {0}; Reason: {3})",
Format = "Pending '{2}' Enrollment for {1} (Session# {0}; Reason: {3}; Identifier: {4})",
Severity = 0,
UseLive = true,
UsePersist = true,
@@ -14,6 +14,8 @@ namespace Disco.Services.Devices.Enrolment
{
public static class WindowsDeviceEnrolment
{
private static readonly string pendingIdentifierAlphabet = "23456789ABCDEFGHJKMNPQRSTWXYZ";
private static readonly Random pendingIdentifierRng = new Random();
private static readonly ConcurrentDictionary<string, EnrolResponse> pendingEnrolments = new ConcurrentDictionary<string, EnrolResponse>();
private static void CleanupPendingEnrolments()
@@ -26,6 +28,28 @@ namespace Disco.Services.Devices.Enrolment
pendingEnrolments.TryRemove(expiredEnrolment, out _);
}
private static string GeneratePendingIdentifier()
{
var identifier = default(string);
var chars = new char[4];
var retryAllowed = 100;
while (--retryAllowed > 0)
{
lock (pendingIdentifierRng)
{
for (var i = 0; i < chars.Length; i++)
{
chars[i] = pendingIdentifierAlphabet[pendingIdentifierRng.Next(pendingIdentifierAlphabet.Length)];
}
}
identifier = new string(chars);
if (!GetPendingEnrolments().Any(e => string.Equals(e.PendingIdentifier, identifier, StringComparison.Ordinal)))
break;
}
return identifier;
}
public static List<EnrolResponse> GetPendingEnrolments()
{
var now = DateTimeOffset.Now;
@@ -185,8 +209,9 @@ namespace Disco.Services.Devices.Enrolment
response.PendingAuthorization = Convert.ToBase64String(authBytes);
}
response.PendingTimeout = DateTimeOffset.Now.Add(Database.DiscoConfiguration.Bootstrapper.PendingTimeout);
response.PendingIdentifier = GeneratePendingIdentifier();
EnrolmentLog.LogSessionPending(sessionId, Request.SerialNumber, EnrolmentTypes.Normal, response.PendingReason);
EnrolmentLog.LogSessionPending(sessionId, Request.SerialNumber, EnrolmentTypes.Normal, response.PendingReason, response.PendingIdentifier);
if (pendingEnrolments.TryAdd(sessionId, response))
return response;