Add SGU credential provider and authentication broker
This commit is contained in:
1 parent
5e216f42a4
commit
1f43f200b4
50 files changed
+3226
-236
No files matched your search
@@ -0,0 +1,132 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using Lithnet.CredentialProvider;
|
||||
using SGU.AuthBroker.Core.Identity;
|
||||
|
||||
namespace SGU.CredentialProvider;
|
||||
|
||||
internal sealed class SguCredentialTile : CredentialTile2
|
||||
{
|
||||
private TextboxControl userNameControl = null!;
|
||||
private SecurePasswordTextboxControl passwordControl = null!;
|
||||
|
||||
public SguCredentialTile(CredentialProviderBase credentialProvider)
|
||||
: base(credentialProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public SguCredentialTile(CredentialProviderBase credentialProvider, CredentialProviderUser user)
|
||||
: base(credentialProvider, user)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
userNameControl = Controls.GetControl<TextboxControl>(ControlKeys.UserName);
|
||||
passwordControl = Controls.GetControl<SecurePasswordTextboxControl>(ControlKeys.Password);
|
||||
userNameControl.Text = User?.QualifiedUserName ?? string.Empty;
|
||||
}
|
||||
|
||||
protected override CredentialResponseBase GetCredentials()
|
||||
{
|
||||
if (!UserIdentityClassifier.TryParse(userNameControl.Text, out UserIdentity? identity) || identity is null)
|
||||
{
|
||||
return Failure("La clave debe usar DO, AL o AD seguido de seis dígitos.");
|
||||
}
|
||||
|
||||
SecureString securePassword = passwordControl.Password;
|
||||
if (securePassword.Length == 0)
|
||||
{
|
||||
return Failure("La contraseña es requerida.");
|
||||
}
|
||||
|
||||
string plainTextPassword = CopyToManagedString(securePassword);
|
||||
try
|
||||
{
|
||||
ProviderSettings settings;
|
||||
BrokerDecision decision;
|
||||
try
|
||||
{
|
||||
settings = ProviderSettings.Load();
|
||||
using BrokerClient broker = new(settings);
|
||||
decision = broker
|
||||
.AuthenticateAsync(identity.UserName, plainTextPassword, CancellationToken.None)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
settings = TryLoadDomainOnlySettings();
|
||||
decision = BrokerDecision.Unavailable("LOCAL_CONFIGURATION_OR_TLS_ERROR");
|
||||
}
|
||||
|
||||
if (decision.Kind == BrokerDecisionKind.InvalidCredentials)
|
||||
{
|
||||
return Failure("Credenciales institucionales inválidas.");
|
||||
}
|
||||
|
||||
string domain = decision.Kind == BrokerDecisionKind.Authorized
|
||||
? decision.Domain!
|
||||
: settings.DomainNetbios;
|
||||
string userName = decision.Kind == BrokerDecisionKind.Authorized
|
||||
? decision.UserName!
|
||||
: identity.UserName;
|
||||
|
||||
return new CredentialResponseSecure
|
||||
{
|
||||
IsSuccess = true,
|
||||
StatusIcon = decision.Kind == BrokerDecisionKind.Unavailable ? StatusIcon.Warning : StatusIcon.None,
|
||||
StatusText = decision.Kind == BrokerDecisionKind.Unavailable
|
||||
? "Servicio institucional no disponible; Windows validará la última contraseña de dominio registrada."
|
||||
: null,
|
||||
Domain = domain,
|
||||
Username = userName,
|
||||
Password = securePassword
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
// The immutable managed string cannot be zeroed; release our reference immediately.
|
||||
// The unmanaged copy used to create it is zeroed by CopyToManagedString.
|
||||
plainTextPassword = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private static ProviderSettings TryLoadDomainOnlySettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProviderSettings.Load();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// LCI is the configured lab domain. This fallback still delegates the actual
|
||||
// password decision to Windows LSA/cached domain credentials.
|
||||
return new ProviderSettings { DomainNetbios = "LCI" };
|
||||
}
|
||||
}
|
||||
|
||||
private static CredentialResponseSecure Failure(string message) => new()
|
||||
{
|
||||
IsSuccess = false,
|
||||
StatusIcon = StatusIcon.Error,
|
||||
StatusText = message
|
||||
};
|
||||
|
||||
private static string CopyToManagedString(SecureString value)
|
||||
{
|
||||
IntPtr pointer = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
pointer = Marshal.SecureStringToGlobalAllocUnicode(value);
|
||||
return Marshal.PtrToStringUni(pointer, value.Length) ?? string.Empty;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (pointer != IntPtr.Zero)
|
||||
{
|
||||
Marshal.ZeroFreeGlobalAllocUnicode(pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user