2019-03-26 04:40:10 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-03-29 03:49:11 +00:00
|
|
|
using System.Text;
|
2019-03-26 04:40:10 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Controller.Authentication;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2019-03-28 15:15:53 +00:00
|
|
|
using MediaBrowser.Model.Cryptography;
|
2019-03-26 04:40:10 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using MediaBrowser.Model.Users;
|
2019-03-24 07:30:16 +00:00
|
|
|
|
2019-03-26 04:40:10 +00:00
|
|
|
namespace Emby.Server.Implementations.Library
|
|
|
|
{
|
|
|
|
public class DefaultPasswordResetProvider : IPasswordResetProvider
|
|
|
|
{
|
|
|
|
public string Name => "Default Password Reset Provider";
|
|
|
|
|
|
|
|
public bool IsEnabled => true;
|
|
|
|
|
|
|
|
private readonly string _passwordResetFileBase;
|
|
|
|
private readonly string _passwordResetFileBaseDir;
|
|
|
|
private readonly string _passwordResetFileBaseName = "passwordreset";
|
|
|
|
|
2019-03-29 14:10:49 +00:00
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly ICryptoProvider _crypto;
|
2019-03-26 04:40:10 +00:00
|
|
|
|
2019-03-28 15:15:53 +00:00
|
|
|
public DefaultPasswordResetProvider(IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IUserManager userManager, ICryptoProvider cryptoProvider)
|
2019-03-26 04:40:10 +00:00
|
|
|
{
|
|
|
|
_passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
|
|
|
|
_passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, _passwordResetFileBaseName);
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_userManager = userManager;
|
2019-03-28 15:15:53 +00:00
|
|
|
_crypto = cryptoProvider;
|
2019-03-26 04:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
|
|
|
|
{
|
2019-03-28 05:46:25 +00:00
|
|
|
SerializablePasswordReset spr;
|
2019-03-26 04:40:10 +00:00
|
|
|
HashSet<string> usersreset = new HashSet<string>();
|
|
|
|
foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{_passwordResetFileBaseName}*"))
|
|
|
|
{
|
2019-03-27 23:17:18 +00:00
|
|
|
using (var str = File.OpenRead(resetfile))
|
|
|
|
{
|
2019-03-28 05:46:25 +00:00
|
|
|
spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
|
2019-03-29 19:48:07 +00:00
|
|
|
}
|
2019-03-28 05:46:25 +00:00
|
|
|
|
2019-03-26 04:40:10 +00:00
|
|
|
if (spr.ExpirationDate < DateTime.Now)
|
|
|
|
{
|
|
|
|
File.Delete(resetfile);
|
|
|
|
}
|
2019-03-29 23:13:01 +00:00
|
|
|
else if (spr.Pin.Replace("-", "").Equals(pin.Replace("-", ""), StringComparison.InvariantCultureIgnoreCase))
|
2019-03-26 04:40:10 +00:00
|
|
|
{
|
2019-03-26 05:17:23 +00:00
|
|
|
var resetUser = _userManager.GetUserByName(spr.UserName);
|
2019-03-28 05:46:25 +00:00
|
|
|
if (resetUser == null)
|
2019-03-26 04:40:10 +00:00
|
|
|
{
|
2019-03-28 05:46:25 +00:00
|
|
|
throw new Exception($"User with a username of {spr.UserName} not found");
|
2019-03-26 04:40:10 +00:00
|
|
|
}
|
2019-03-28 05:46:25 +00:00
|
|
|
|
|
|
|
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
|
|
|
|
usersreset.Add(resetUser.Name);
|
|
|
|
File.Delete(resetfile);
|
2019-03-26 04:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usersreset.Count < 1)
|
|
|
|
{
|
|
|
|
throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new PinRedeemResult
|
|
|
|
{
|
|
|
|
Success = true,
|
|
|
|
UsersReset = usersreset.ToArray()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork)
|
|
|
|
{
|
2019-03-28 15:15:53 +00:00
|
|
|
string pin = string.Empty;
|
|
|
|
using (var cryptoRandom = System.Security.Cryptography.RandomNumberGenerator.Create())
|
2019-03-29 19:48:07 +00:00
|
|
|
{
|
2019-03-28 15:15:53 +00:00
|
|
|
byte[] bytes = new byte[4];
|
|
|
|
cryptoRandom.GetBytes(bytes);
|
2019-03-29 03:49:11 +00:00
|
|
|
pin = BitConverter.ToString(bytes);
|
2019-03-29 19:48:07 +00:00
|
|
|
}
|
2019-03-28 15:15:53 +00:00
|
|
|
|
2019-03-26 04:40:10 +00:00
|
|
|
DateTime expireTime = DateTime.Now.AddMinutes(30);
|
2019-03-26 05:17:23 +00:00
|
|
|
string filePath = _passwordResetFileBase + user.InternalId + ".json";
|
2019-03-26 04:40:10 +00:00
|
|
|
SerializablePasswordReset spr = new SerializablePasswordReset
|
|
|
|
{
|
|
|
|
ExpirationDate = expireTime,
|
|
|
|
Pin = pin,
|
|
|
|
PinFile = filePath,
|
|
|
|
UserName = user.Name
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
2019-03-27 23:17:18 +00:00
|
|
|
{
|
2019-03-28 05:46:25 +00:00
|
|
|
using (FileStream fileStream = File.OpenWrite(filePath))
|
|
|
|
{
|
|
|
|
_jsonSerializer.SerializeToStream(spr, fileStream);
|
|
|
|
await fileStream.FlushAsync().ConfigureAwait(false);
|
|
|
|
}
|
2019-03-26 04:40:10 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw new Exception($"Error serializing or writing password reset for {user.Name} to location: {filePath}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ForgotPasswordResult
|
|
|
|
{
|
|
|
|
Action = ForgotPasswordAction.PinCode,
|
|
|
|
PinExpirationDate = expireTime,
|
|
|
|
PinFile = filePath
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SerializablePasswordReset : PasswordPinCreationResult
|
|
|
|
{
|
|
|
|
public string Pin { get; set; }
|
|
|
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 07:30:16 +00:00
|
|
|
}
|