Update PhoneNumberTokenProvider.cs (#22766)

* Update PhoneNumberTokenProvider.cs

Add license header.
Hyphenate two-factor authentication in documentation.
Add line breaks before return statements.
Use string interpolation instead of string concatenation.
This commit is contained in:
Pranav K 2020-06-16 17:22:26 -07:00 committed by GitHub
commit 65e933ec70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 20 deletions

View File

@ -13,14 +13,15 @@ namespace Microsoft.AspNetCore.Identity
public class AuthenticatorTokenProvider<TUser> : IUserTwoFactorTokenProvider<TUser> where TUser : class
{
/// <summary>
/// Checks if a two factor authentication token can be generated for the specified <paramref name="user"/>.
/// Checks if a two-factor authentication token can be generated for the specified <paramref name="user"/>.
/// </summary>
/// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve the <paramref name="user"/> from.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two factor authentication token.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two-factor authentication token.</param>
/// <returns>True if the user has an authenticator key set, otherwise false.</returns>
public async virtual Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user)
{
var key = await manager.GetAuthenticatorKeyAsync(user);
return !string.IsNullOrWhiteSpace(key);
}
@ -65,6 +66,7 @@ namespace Microsoft.AspNetCore.Identity
return true;
}
}
return false;
}
}

View File

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Identity
@ -10,29 +13,31 @@ namespace Microsoft.AspNetCore.Identity
where TUser : class
{
/// <summary>
/// Checks if a two factor authentication token can be generated for the specified <paramref name="user"/>.
/// Checks if a two-factor authentication token can be generated for the specified <paramref name="user"/>.
/// </summary>
/// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve the <paramref name="user"/> from.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two factor authentication token.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two-factor authentication token.</param>
/// <returns>True if the user has an email address set, otherwise false.</returns>
public override async Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user)
{
var email = await manager.GetEmailAsync(user);
return !string.IsNullOrWhiteSpace(email) && await manager.IsEmailConfirmedAsync(user);
}
/// <summary>
/// Returns the a value for the user used as entropy in the generated token.
/// </summary>
/// <param name="purpose">The purpose of the two factor authentication token.</param>
/// <param name="purpose">The purpose of the two-factor authentication token.</param>
/// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve the <paramref name="user"/> from.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two factor authentication token.</param>
/// <param name="user">The <typeparamref name="TUser"/> to check for the possibility of generating a two-factor authentication token.</param>
/// <returns>A string suitable for use as entropy in token generation.</returns>
public override async Task<string> GetUserModifierAsync(string purpose, UserManager<TUser> manager,
TUser user)
{
var email = await manager.GetEmailAsync(user);
return "Email:" + purpose + ":" + email;
return $"Email:{purpose}:{email}";
}
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Identity
{
/// <summary>
/// Provides an abstraction for two factor token generators.
/// Provides an abstraction for two-factor token generators.
/// </summary>
/// <typeparam name="TUser">The type encapsulating a user.</typeparam>
public interface IUserTwoFactorTokenProvider<TUser> where TUser : class
@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Identity
Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user);
/// <summary>
/// Returns a flag indicating whether the token provider can generate a token suitable for two factor authentication token for
/// 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>
@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Identity
/// <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, otherwise false.
/// The task will return true if a two-factor authentication token could be generated, otherwise false.
/// </returns>
Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user);
}

View File

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
@ -12,7 +15,7 @@ namespace Microsoft.AspNetCore.Identity
where TUser : class
{
/// <summary>
/// Returns a flag indicating whether the token provider can generate a token suitable for two factor authentication token for
/// 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>
@ -20,7 +23,7 @@ namespace Microsoft.AspNetCore.Identity
/// <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
/// 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)
@ -29,7 +32,9 @@ namespace Microsoft.AspNetCore.Identity
{
throw new ArgumentNullException(nameof(manager));
}
var phoneNumber = await manager.GetPhoneNumberAsync(user);
return !string.IsNullOrWhiteSpace(phoneNumber) && await manager.IsPhoneNumberConfirmedAsync(user);
}
@ -49,8 +54,10 @@ namespace Microsoft.AspNetCore.Identity
{
throw new ArgumentNullException(nameof(manager));
}
var phoneNumber = await manager.GetPhoneNumberAsync(user);
return "PhoneNumber:" + purpose + ":" + phoneNumber;
return $"PhoneNumber:{purpose}:{phoneNumber}";
}
}
}
}

View File

@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Identity
{
/// <summary>
/// Represents a token provider that generates time based codes using the user's security stamp.
/// Represents a token provider that generates time-based codes using the user's security stamp.
/// </summary>
/// <typeparam name="TUser">The type encapsulating a user.</typeparam>
public abstract class TotpSecurityStampBasedTokenProvider<TUser> : IUserTwoFactorTokenProvider<TUser>
@ -38,6 +38,7 @@ namespace Microsoft.AspNetCore.Identity
}
var token = await manager.CreateSecurityTokenAsync(user);
var modifier = await GetUserModifierAsync(purpose, manager, user);
return Rfc6238AuthenticationService.GenerateCode(token, modifier).ToString("D6", CultureInfo.InvariantCulture);
}
@ -67,6 +68,7 @@ namespace Microsoft.AspNetCore.Identity
}
var securityToken = await manager.CreateSecurityTokenAsync(user);
var modifier = await GetUserModifierAsync(purpose, manager, user);
return securityToken != null && Rfc6238AuthenticationService.ValidateCode(securityToken, code, modifier);
}
@ -87,11 +89,12 @@ namespace Microsoft.AspNetCore.Identity
throw new ArgumentNullException(nameof(manager));
}
var userId = await manager.GetUserIdAsync(user);
return "Totp:" + purpose + ":" + userId;
return $"Totp:{purpose}:{userId}";
}
/// <summary>
/// Returns a flag indicating whether the token provider can generate a token suitable for two factor authentication token for
/// 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>
@ -99,8 +102,8 @@ namespace Microsoft.AspNetCore.Identity
/// <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, otherwise false.
/// The task will return true if a two-factor authentication token could be generated, otherwise false.
/// </returns>
public abstract Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user);
}
}
}