-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
PhoneNumberTokenProvider.cs
57 lines (49 loc) · 2.75 KB
/
PhoneNumberTokenProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Shared;
namespace Microsoft.AspNetCore.Identity;
/// <summary>
/// Represents a token provider that generates tokens from a user's security stamp and
/// sends them to the user via their phone number.
/// </summary>
/// <typeparam name="TUser">The type encapsulating a user.</typeparam>
public class PhoneNumberTokenProvider<TUser> : TotpSecurityStampBasedTokenProvider<TUser>
where TUser : class
{
/// <summary>
/// Returns a flag indicating whether the token provider can generate a token suitable for two-factor authentication token for
/// the specified <paramref name="user"/>.
/// </summary>
/// <param name="manager">The <see cref="UserManager{TUser}"/> that can be used to retrieve user properties.</param>
/// <param name="user">The user a token could be generated for.</param>
/// <returns>
/// The <see cref="Task"/> that represents the asynchronous operation, containing the a flag indicating if a two
/// factor token could be generated by this provider for the specified <paramref name="user"/>.
/// The task will return true if a two-factor authentication token could be generated as the user has
/// a telephone number, otherwise false.
/// </returns>
public override async Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user)
{
ArgumentNullThrowHelper.ThrowIfNull(manager);
var phoneNumber = await manager.GetPhoneNumberAsync(user).ConfigureAwait(false);
return !string.IsNullOrWhiteSpace(phoneNumber) && await manager.IsPhoneNumberConfirmedAsync(user).ConfigureAwait(false);
}
/// <summary>
/// Returns a constant, provider and user unique modifier used for entropy in generated tokens from user information.
/// </summary>
/// <param name="purpose">The purpose the token will be generated for.</param>
/// <param name="manager">The <see cref="UserManager{TUser}"/> that can be used to retrieve user properties.</param>
/// <param name="user">The user a token should be generated for.</param>
/// <returns>
/// The <see cref="Task"/> that represents the asynchronous operation, containing a constant modifier for the specified
/// <paramref name="user"/> and <paramref name="purpose"/>.
/// </returns>
public override async Task<string> GetUserModifierAsync(string purpose, UserManager<TUser> manager, TUser user)
{
ArgumentNullThrowHelper.ThrowIfNull(manager);
var phoneNumber = await manager.GetPhoneNumberAsync(user).ConfigureAwait(false);
return $"PhoneNumber:{purpose}:{phoneNumber}";
}
}