DefaultPasswordResetProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Jellyfin.Server.Implementations.User
  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. await 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.Username);
  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. return new PinRedeemResult
  79. {
  80. Success = true,
  81. UsersReset = usersReset.ToArray()
  82. };
  83. }
  84. /// <inheritdoc />
  85. public async Task<ForgotPasswordResult> StartForgotPasswordProcess(Jellyfin.Data.Entities.User user, bool isInNetwork)
  86. {
  87. string pin;
  88. using (var cryptoRandom = RandomNumberGenerator.Create())
  89. {
  90. byte[] bytes = new byte[4];
  91. cryptoRandom.GetBytes(bytes);
  92. pin = BitConverter.ToString(bytes);
  93. }
  94. DateTime expireTime = DateTime.Now.AddMinutes(30);
  95. user.EasyPassword = pin;
  96. await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
  97. return new ForgotPasswordResult
  98. {
  99. Action = ForgotPasswordAction.PinCode,
  100. PinExpirationDate = expireTime,
  101. };
  102. }
  103. private class SerializablePasswordReset : PasswordPinCreationResult
  104. {
  105. public string Pin { get; set; }
  106. public string UserName { get; set; }
  107. }
  108. }
  109. }