using System;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Identity
{
///
/// Represents a token provider that generates tokens from a user's security stamp and
/// sends them to the user via their phone number.
///
/// The type encapsulating a user.
public class PhoneNumberTokenProvider : TotpSecurityStampBasedTokenProvider
where TUser : class
{
///
/// Returns a flag indicating whether the token provider can generate a token suitable for two factor authentication token for
/// the specified .
///
/// The that can be used to retrieve user properties.
/// The user a token could be generated for.
///
/// The that represents the asynchronous operation, containing the a flag indicating if a two
/// factor token could be generated by this provider for the specified and .
/// The task will return true if a two factor authentication token could be generated as the user has
/// a telephone number, otherwise false.
///
public override async Task CanGenerateTwoFactorTokenAsync(UserManager manager, TUser user)
{
if (manager == null)
{
throw new ArgumentNullException(nameof(manager));
}
var phoneNumber = await manager.GetPhoneNumberAsync(user);
return !string.IsNullOrWhiteSpace(phoneNumber) && await manager.IsPhoneNumberConfirmedAsync(user);
}
///
/// Returns a constant, provider and user unique modifier used for entropy in generated tokens from user information, as an asynchronous operation.
///
/// The purpose the token will be generated for.
/// The that can be used to retrieve user properties.
/// The user a token should be generated for.
///
/// The that represents the asynchronous operation, containing a constant modifier for the specified
/// and .
///
public override async Task GetUserModifierAsync(string purpose, UserManager manager, TUser user)
{
if (manager == null)
{
throw new ArgumentNullException(nameof(manager));
}
var phoneNumber = await manager.GetPhoneNumberAsync(user);
return "PhoneNumber:" + purpose + ":" + phoneNumber;
}
}
}