mirror of
https://github.com/bitwarden/server
synced 2025-12-24 04:03:25 +00:00
refactor(TwoFactorAuthentication): Remove references to old Duo SDK version 2 code and replace them with the Duo SDK version 4 supported library DuoUniversal code. Increased unit test coverage in the Two Factor Authentication code space. We opted to use DI instead of Inheritance for the Duo and OrganizaitonDuo two factor tokens to increase testability, since creating a testing mock of the Duo.Client was non-trivial. Reviewed-by: @JaredSnider-Bitwarden
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.Auth.Enums;
|
|
using Bit.Core.Auth.Models;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Models.Api;
|
|
|
|
namespace Bit.Api.Auth.Models.Response.TwoFactor;
|
|
|
|
public class TwoFactorDuoResponseModel : ResponseModel
|
|
{
|
|
private const string ResponseObj = "twoFactorDuo";
|
|
|
|
public TwoFactorDuoResponseModel(User user)
|
|
: base(ResponseObj)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(user);
|
|
|
|
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
|
|
Build(provider);
|
|
}
|
|
|
|
public TwoFactorDuoResponseModel(Organization organization)
|
|
: base(ResponseObj)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(organization);
|
|
|
|
var provider = organization.GetTwoFactorProvider(TwoFactorProviderType.OrganizationDuo);
|
|
Build(provider);
|
|
}
|
|
|
|
public bool Enabled { get; set; }
|
|
public string Host { get; set; }
|
|
public string ClientSecret { get; set; }
|
|
public string ClientId { get; set; }
|
|
|
|
private void Build(TwoFactorProvider provider)
|
|
{
|
|
if (provider?.MetaData != null && provider.MetaData.Count > 0)
|
|
{
|
|
Enabled = provider.Enabled;
|
|
|
|
if (provider.MetaData.TryGetValue("Host", out var host))
|
|
{
|
|
Host = (string)host;
|
|
}
|
|
if (provider.MetaData.TryGetValue("ClientSecret", out var clientSecret))
|
|
{
|
|
ClientSecret = MaskSecret((string)clientSecret);
|
|
}
|
|
if (provider.MetaData.TryGetValue("ClientId", out var clientId))
|
|
{
|
|
ClientId = (string)clientId;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Enabled = false;
|
|
}
|
|
}
|
|
|
|
private static string MaskSecret(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key) || key.Length <= 6)
|
|
{
|
|
return key;
|
|
}
|
|
|
|
// Mask all but the first 6 characters.
|
|
return string.Concat(key.AsSpan(0, 6), new string('*', key.Length - 6));
|
|
}
|
|
}
|