mirror of
https://github.com/bitwarden/server
synced 2025-12-23 03:33:35 +00:00
initial commit of source
This commit is contained in:
43
src/Core/Identity/AuthenticatorTokenProvider.cs
Normal file
43
src/Core/Identity/AuthenticatorTokenProvider.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Base32;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Enums;
|
||||
using OtpSharp;
|
||||
|
||||
namespace Bit.Core.Identity
|
||||
{
|
||||
public class AuthenticatorTokenProvider : IUserTokenProvider<User>
|
||||
{
|
||||
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<User> manager, User user)
|
||||
{
|
||||
var canGenerate = user.TwoFactorEnabled
|
||||
&& user.TwoFactorProvider.HasValue
|
||||
&& user.TwoFactorProvider.Value == TwoFactorProvider.Authenticator
|
||||
&& !string.IsNullOrWhiteSpace(user.AuthenticatorKey);
|
||||
|
||||
return Task.FromResult(canGenerate);
|
||||
}
|
||||
|
||||
public Task<string> GetUserModifierAsync(string purpose, UserManager<User> manager, User user)
|
||||
{
|
||||
return Task.FromResult<string>(null);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var otp = new Totp(Base32Encoder.Decode(user.AuthenticatorKey));
|
||||
|
||||
long timeStepMatched;
|
||||
var valid = otp.VerifyTotp(token, out timeStepMatched, new VerificationWindow(2, 2));
|
||||
|
||||
return Task.FromResult(valid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user