jellyfin-server/Jellyfin.Server.Implementations/Users/DefaultPasswordResetProvider.cs

134 lines
4.9 KiB
C#
Raw Normal View History

2019-05-21 17:28:34 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2019-06-09 20:08:01 +00:00
using System.Security.Cryptography;
using System.Text.Json;
2019-05-21 17:28:34 +00:00
using System.Threading.Tasks;
2020-05-15 21:24:01 +00:00
using Jellyfin.Data.Entities;
2020-06-20 21:58:09 +00:00
using MediaBrowser.Common;
2019-05-21 17:28:34 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.IO;
2019-05-21 17:28:34 +00:00
using MediaBrowser.Model.Users;
2020-05-15 21:24:01 +00:00
namespace Jellyfin.Server.Implementations.Users
2019-05-21 17:28:34 +00:00
{
2019-11-01 17:38:54 +00:00
/// <summary>
/// The default password reset provider.
/// </summary>
2019-05-21 17:28:34 +00:00
public class DefaultPasswordResetProvider : IPasswordResetProvider
{
2019-06-09 20:08:01 +00:00
private const string BaseResetFileName = "passwordreset";
2019-05-21 17:28:34 +00:00
2020-06-20 21:58:09 +00:00
private readonly IApplicationHost _appHost;
2019-05-21 17:28:34 +00:00
private readonly string _passwordResetFileBase;
private readonly string _passwordResetFileBaseDir;
2019-11-01 17:38:54 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="DefaultPasswordResetProvider"/> class.
/// </summary>
/// <param name="configurationManager">The configuration manager.</param>
2020-06-20 21:58:09 +00:00
/// <param name="appHost">The application host.</param>
public DefaultPasswordResetProvider(IServerConfigurationManager configurationManager, IApplicationHost appHost)
2019-05-21 17:28:34 +00:00
{
_passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
2019-06-09 20:08:01 +00:00
_passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, BaseResetFileName);
2020-06-20 21:58:09 +00:00
_appHost = appHost;
// TODO: Remove the circular dependency on UserManager
2019-05-21 17:28:34 +00:00
}
2019-06-09 20:08:01 +00:00
/// <inheritdoc />
public string Name => "Default Password Reset Provider";
/// <inheritdoc />
public bool IsEnabled => true;
/// <inheritdoc />
2019-05-21 17:28:34 +00:00
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
{
2020-06-20 21:58:09 +00:00
var userManager = _appHost.Resolve<IUserManager>();
2020-05-30 04:19:36 +00:00
var usersReset = new List<string>();
2020-05-13 02:10:35 +00:00
foreach (var resetFile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{BaseResetFileName}*"))
2019-05-21 17:28:34 +00:00
{
2020-11-15 18:35:36 +00:00
SerializablePasswordReset spr;
await using (var str = AsyncFile.OpenRead(resetFile))
2019-05-21 17:28:34 +00:00
{
2020-11-13 18:24:46 +00:00
spr = await JsonSerializer.DeserializeAsync<SerializablePasswordReset>(str).ConfigureAwait(false)
2020-11-14 01:04:06 +00:00
?? throw new ResourceNotFoundException($"Provided path ({resetFile}) is not valid.");
}
2020-05-30 04:19:36 +00:00
if (spr.ExpirationDate < DateTime.UtcNow)
2019-05-21 17:28:34 +00:00
{
2020-05-13 02:10:35 +00:00
File.Delete(resetFile);
2019-05-21 17:28:34 +00:00
}
2019-06-09 20:08:01 +00:00
else if (string.Equals(
2019-11-01 17:38:54 +00:00
spr.Pin.Replace("-", string.Empty, StringComparison.Ordinal),
pin.Replace("-", string.Empty, StringComparison.Ordinal),
2021-03-09 04:57:38 +00:00
StringComparison.OrdinalIgnoreCase))
2019-05-21 17:28:34 +00:00
{
2020-06-20 21:58:09 +00:00
var resetUser = userManager.GetUserByName(spr.UserName)
2020-05-30 04:19:36 +00:00
?? throw new ResourceNotFoundException($"User with a username of {spr.UserName} not found");
2019-05-21 17:28:34 +00:00
2020-06-20 21:58:09 +00:00
await userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
2020-05-13 02:10:35 +00:00
usersReset.Add(resetUser.Username);
File.Delete(resetFile);
2019-05-21 17:28:34 +00:00
}
}
2020-05-13 02:10:35 +00:00
if (usersReset.Count < 1)
2019-05-21 17:28:34 +00:00
{
throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
}
2020-05-13 02:10:35 +00:00
return new PinRedeemResult
2019-05-21 17:28:34 +00:00
{
2020-05-13 02:10:35 +00:00
Success = true,
UsersReset = usersReset.ToArray()
};
2019-05-21 17:28:34 +00:00
}
2019-06-09 20:08:01 +00:00
/// <inheritdoc />
2020-05-15 21:24:01 +00:00
public async Task<ForgotPasswordResult> StartForgotPasswordProcess(User user, bool isInNetwork)
2019-05-21 17:28:34 +00:00
{
2021-10-08 13:02:58 +00:00
byte[] bytes = new byte[4];
RandomNumberGenerator.Fill(bytes);
string pin = BitConverter.ToString(bytes);
2019-05-21 17:28:34 +00:00
2020-05-30 04:19:36 +00:00
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 = AsyncFile.OpenWrite(filePath))
2020-05-30 04:19:36 +00:00
{
2020-07-25 17:07:12 +00:00
await JsonSerializer.SerializeAsync(fileStream, spr).ConfigureAwait(false);
2020-05-30 04:19:36 +00:00
}
2019-05-21 17:28:34 +00:00
2020-05-13 02:10:35 +00:00
user.EasyPassword = pin;
2019-05-21 17:28:34 +00:00
return new ForgotPasswordResult
{
Action = ForgotPasswordAction.PinCode,
PinExpirationDate = expireTime,
PinFile = filePath
2019-05-21 17:28:34 +00:00
};
}
2020-06-09 16:21:21 +00:00
#nullable disable
2019-05-21 17:28:34 +00:00
private class SerializablePasswordReset : PasswordPinCreationResult
{
public string Pin { get; set; }
public string UserName { get; set; }
}
}
}