1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 05:28:15 -05:00

yubico otp token provider

This commit is contained in:
Kyle Spearrin 2017-06-12 23:16:38 -04:00
parent 314d591f36
commit e712b2ded0

View File

@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Bit.Core.Models.Table;
using Bit.Core.Enums;
using YubicoDotNetClient;
using System.Linq;
namespace Bit.Core.Identity
{
public class YubicoOtpTokenProvider : IUserTwoFactorTokenProvider<User>
{
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<User> manager, User user)
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.YubiKey);
var canGenerate = user.TwoFactorProviderIsEnabled(TwoFactorProviderType.YubiKey)
&& user.TwoFactorProvider.HasValue
&& user.TwoFactorProvider.Value == TwoFactorProviderType.YubiKey
&& (provider?.MetaData.Values.Any(v => !string.IsNullOrWhiteSpace(v)) ?? false);
return Task.FromResult(canGenerate);
}
public Task<string> GenerateAsync(string purpose, UserManager<User> manager, User user)
{
return Task.FromResult<string>(null);
}
public Task<bool> ValidateAsync(string purpose, string token, UserManager<User> manager, User user)
{
if(token.Length != 44)
{
return Task.FromResult(false);
}
var id = token.Substring(0, 12);
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.YubiKey);
if(!provider.MetaData.ContainsValue(id))
{
return Task.FromResult(false);
}
var client = new YubicoClient("TODO", "TODO");
var response = client.Verify(token);
return Task.FromResult(response.Status == YubicoResponseStatus.Ok);
}
}
}