DefaultPasswordResetProvider.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller.Authentication;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Serialization;
  11. using MediaBrowser.Model.Users;
  12. namespace Emby.Server.Implementations.Library
  13. {
  14. /// <summary>
  15. /// The default password reset provider.
  16. /// </summary>
  17. public class DefaultPasswordResetProvider : IPasswordResetProvider
  18. {
  19. private const string BaseResetFileName = "passwordreset";
  20. private readonly IJsonSerializer _jsonSerializer;
  21. private readonly IUserManager _userManager;
  22. private readonly string _passwordResetFileBase;
  23. private readonly string _passwordResetFileBaseDir;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="DefaultPasswordResetProvider"/> class.
  26. /// </summary>
  27. /// <param name="configurationManager">The configuration manager.</param>
  28. /// <param name="jsonSerializer">The JSON serializer.</param>
  29. /// <param name="userManager">The user manager.</param>
  30. public DefaultPasswordResetProvider(
  31. IServerConfigurationManager configurationManager,
  32. IJsonSerializer jsonSerializer,
  33. IUserManager userManager)
  34. {
  35. _passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
  36. _passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, BaseResetFileName);
  37. _jsonSerializer = jsonSerializer;
  38. _userManager = userManager;
  39. }
  40. /// <inheritdoc />
  41. public string Name => "Default Password Reset Provider";
  42. /// <inheritdoc />
  43. public bool IsEnabled => true;
  44. /// <inheritdoc />
  45. public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
  46. {
  47. SerializablePasswordReset spr;
  48. List<string> usersreset = new List<string>();
  49. foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{BaseResetFileName}*"))
  50. {
  51. using (var str = File.OpenRead(resetfile))
  52. {
  53. spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
  54. }
  55. if (spr.ExpirationDate < DateTime.Now)
  56. {
  57. File.Delete(resetfile);
  58. }
  59. else if (string.Equals(
  60. spr.Pin.Replace("-", string.Empty, StringComparison.Ordinal),
  61. pin.Replace("-", string.Empty, StringComparison.Ordinal),
  62. StringComparison.InvariantCultureIgnoreCase))
  63. {
  64. var resetUser = _userManager.GetUserByName(spr.UserName);
  65. if (resetUser == null)
  66. {
  67. throw new ResourceNotFoundException($"User with a username of {spr.UserName} not found");
  68. }
  69. await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
  70. usersreset.Add(resetUser.Name);
  71. File.Delete(resetfile);
  72. }
  73. }
  74. if (usersreset.Count < 1)
  75. {
  76. throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
  77. }
  78. else
  79. {
  80. return new PinRedeemResult
  81. {
  82. Success = true,
  83. UsersReset = usersreset.ToArray()
  84. };
  85. }
  86. }
  87. /// <inheritdoc />
  88. public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork)
  89. {
  90. string pin = string.Empty;
  91. using (var cryptoRandom = RandomNumberGenerator.Create())
  92. {
  93. byte[] bytes = new byte[4];
  94. cryptoRandom.GetBytes(bytes);
  95. pin = BitConverter.ToString(bytes);
  96. }
  97. DateTime expireTime = DateTime.Now.AddMinutes(30);
  98. string filePath = _passwordResetFileBase + user.InternalId + ".json";
  99. SerializablePasswordReset spr = new SerializablePasswordReset
  100. {
  101. ExpirationDate = expireTime,
  102. Pin = pin,
  103. PinFile = filePath,
  104. UserName = user.Name
  105. };
  106. using (FileStream fileStream = File.OpenWrite(filePath))
  107. {
  108. _jsonSerializer.SerializeToStream(spr, fileStream);
  109. await fileStream.FlushAsync().ConfigureAwait(false);
  110. }
  111. return new ForgotPasswordResult
  112. {
  113. Action = ForgotPasswordAction.PinCode,
  114. PinExpirationDate = expireTime,
  115. PinFile = filePath
  116. };
  117. }
  118. private class SerializablePasswordReset : PasswordPinCreationResult
  119. {
  120. public string Pin { get; set; }
  121. public string UserName { get; set; }
  122. }
  123. }
  124. }