Reimplement password resetting

This commit is contained in:
Patrick Barron 2020-05-30 00:19:36 -04:00
parent 4857b7d620
commit c8fef9dd2e
2 changed files with 21 additions and 10 deletions

View File

@ -95,7 +95,7 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc /> /// <inheritdoc />
public bool HasPassword(User user) public bool HasPassword(User user)
=> !string.IsNullOrEmpty(user.Password); => !string.IsNullOrEmpty(user?.Password);
/// <inheritdoc /> /// <inheritdoc />
public Task ChangePassword(User user, string newPassword) public Task ChangePassword(User user, string newPassword)

View File

@ -52,16 +52,16 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc /> /// <inheritdoc />
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin) public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
{ {
SerializablePasswordReset spr; var usersReset = new List<string>();
List<string> usersReset = new List<string>();
foreach (var resetFile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{BaseResetFileName}*")) foreach (var resetFile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{BaseResetFileName}*"))
{ {
SerializablePasswordReset spr;
await using (var str = File.OpenRead(resetFile)) await using (var str = File.OpenRead(resetFile))
{ {
spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false); spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
} }
if (spr.ExpirationDate < DateTime.Now) if (spr.ExpirationDate < DateTime.UtcNow)
{ {
File.Delete(resetFile); File.Delete(resetFile);
} }
@ -70,11 +70,8 @@ namespace Jellyfin.Server.Implementations.Users
pin.Replace("-", string.Empty, StringComparison.Ordinal), pin.Replace("-", string.Empty, StringComparison.Ordinal),
StringComparison.InvariantCultureIgnoreCase)) StringComparison.InvariantCultureIgnoreCase))
{ {
var resetUser = _userManager.GetUserByName(spr.UserName); var resetUser = _userManager.GetUserByName(spr.UserName)
if (resetUser == null) ?? throw new ResourceNotFoundException($"User with a username of {spr.UserName} not found");
{
throw new ResourceNotFoundException($"User with a username of {spr.UserName} not found");
}
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false); await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
usersReset.Add(resetUser.Username); usersReset.Add(resetUser.Username);
@ -105,7 +102,21 @@ namespace Jellyfin.Server.Implementations.Users
pin = BitConverter.ToString(bytes); pin = BitConverter.ToString(bytes);
} }
DateTime expireTime = DateTime.Now.AddMinutes(30); DateTime expireTime = DateTime.UtcNow.AddMinutes(30);
string filePath = _passwordResetFileBase + user.Id + ".json";
SerializablePasswordReset spr = new SerializablePasswordReset
{
ExpirationDate = expireTime,
Pin = pin,
PinFile = filePath,
UserName = user.Username
};
await using (FileStream fileStream = File.OpenWrite(filePath))
{
_jsonSerializer.SerializeToStream(spr, fileStream);
await fileStream.FlushAsync().ConfigureAwait(false);
}
user.EasyPassword = pin; user.EasyPassword = pin;
await _userManager.UpdateUserAsync(user).ConfigureAwait(false); await _userManager.UpdateUserAsync(user).ConfigureAwait(false);