2018-04-03 14:31:33 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2018-04-03 14:31:33 -04:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2018-04-03 14:31:33 -04:00
|
|
|
|
using Bit.Core.Utilities.Duo;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Identity
|
|
|
|
|
|
{
|
|
|
|
|
|
public interface IOrganizationDuoWebTokenProvider : IOrganizationTwoFactorTokenProvider { }
|
|
|
|
|
|
|
|
|
|
|
|
public class OrganizationDuoWebTokenProvider : IOrganizationDuoWebTokenProvider
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
|
|
|
|
|
|
|
public OrganizationDuoWebTokenProvider(GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
_globalSettings = globalSettings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<bool> CanGenerateTwoFactorTokenAsync(Organization organization)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (organization == null || !organization.Enabled || !organization.Use2fa)
|
2018-04-03 14:31:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = organization.GetTwoFactorProvider(TwoFactorProviderType.OrganizationDuo);
|
|
|
|
|
|
var canGenerate = organization.TwoFactorProviderIsEnabled(TwoFactorProviderType.OrganizationDuo)
|
|
|
|
|
|
&& HasProperMetaData(provider);
|
|
|
|
|
|
return Task.FromResult(canGenerate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<string> GenerateAsync(Organization organization, User user)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (organization == null || !organization.Enabled || !organization.Use2fa)
|
2018-04-03 14:31:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult<string>(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = organization.GetTwoFactorProvider(TwoFactorProviderType.OrganizationDuo);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!HasProperMetaData(provider))
|
2018-04-03 14:31:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult<string>(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 13:45:20 +01:00
|
|
|
|
var signatureRequest = DuoWeb.SignRequest(provider.MetaData["IKey"].ToString(),
|
|
|
|
|
|
provider.MetaData["SKey"].ToString(), _globalSettings.Duo.AKey, user.Email);
|
2018-04-03 14:31:33 -04:00
|
|
|
|
return Task.FromResult(signatureRequest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<bool> ValidateAsync(string token, Organization organization, User user)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (organization == null || !organization.Enabled || !organization.Use2fa)
|
2018-04-03 14:31:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = organization.GetTwoFactorProvider(TwoFactorProviderType.OrganizationDuo);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!HasProperMetaData(provider))
|
2018-04-03 14:31:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 13:45:20 +01:00
|
|
|
|
var response = DuoWeb.VerifyResponse(provider.MetaData["IKey"].ToString(),
|
|
|
|
|
|
provider.MetaData["SKey"].ToString(), _globalSettings.Duo.AKey, token);
|
2018-04-03 14:31:33 -04:00
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(response == user.Email);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool HasProperMetaData(TwoFactorProvider provider)
|
|
|
|
|
|
{
|
|
|
|
|
|
return provider?.MetaData != null && provider.MetaData.ContainsKey("IKey") &&
|
|
|
|
|
|
provider.MetaData.ContainsKey("SKey") && provider.MetaData.ContainsKey("Host");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|