DefaultPasswordResetProvider.cs 4.7 KB

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