2019-02-20 02:17:30 -07:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Authentication;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Model.Cryptography;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Library
|
|
|
|
{
|
|
|
|
public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
|
|
|
|
{
|
|
|
|
private readonly ICryptoProvider _cryptographyProvider;
|
2019-05-21 10:28:34 -07:00
|
|
|
public DefaultAuthenticationProvider(ICryptoProvider cryptographyProvider)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
_cryptographyProvider = cryptographyProvider;
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name => "Default";
|
|
|
|
|
|
|
|
public bool IsEnabled => true;
|
2019-05-11 16:32:20 -07:00
|
|
|
|
2019-03-05 00:58:25 -07:00
|
|
|
// This is dumb and an artifact of the backwards way auth providers were designed.
|
|
|
|
// This version of authenticate was never meant to be called, but needs to be here for interface compat
|
|
|
|
// Only the providers that don't provide local user support use this
|
2019-02-20 02:17:30 -07:00
|
|
|
public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2019-05-11 16:32:20 -07:00
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
// This is the version that we need to use for local users. Because reasons.
|
2019-02-20 02:17:30 -07:00
|
|
|
public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
if (resolvedUser == null)
|
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
throw new ArgumentNullException(nameof(resolvedUser));
|
2019-02-18 02:26:01 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 00:58:25 -07:00
|
|
|
// As long as jellyfin supports passwordless users, we need this little block here to accomodate
|
2019-05-21 10:28:34 -07:00
|
|
|
if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(password))
|
2019-02-18 02:26:01 -07:00
|
|
|
{
|
|
|
|
return Task.FromResult(new ProviderAuthenticationResult
|
|
|
|
{
|
|
|
|
Username = username
|
|
|
|
});
|
|
|
|
}
|
2019-02-20 02:17:30 -07:00
|
|
|
|
|
|
|
ConvertPasswordFormat(resolvedUser);
|
|
|
|
byte[] passwordbytes = Encoding.UTF8.GetBytes(password);
|
|
|
|
|
|
|
|
PasswordHash readyHash = new PasswordHash(resolvedUser.Password);
|
2019-05-21 10:28:34 -07:00
|
|
|
if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
|
|
|
|
|| _cryptographyProvider.DefaultHashMethod == readyHash.Id)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
byte[] calculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes, readyHash.Salt);
|
2019-02-20 02:17:30 -07:00
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
if (calculatedHash.SequenceEqual(readyHash.Hash))
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
throw new AuthenticationException("Invalid username or password");
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult(new ProviderAuthenticationResult
|
|
|
|
{
|
|
|
|
Username = username
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:58:25 -07:00
|
|
|
// This allows us to move passwords forward to the newformat without breaking. They are still insecure, unsalted, and dumb before a password change
|
|
|
|
// but at least they are in the new format.
|
2019-02-20 02:17:30 -07:00
|
|
|
private void ConvertPasswordFormat(User user)
|
|
|
|
{
|
2019-02-28 00:05:12 -07:00
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
2019-02-20 01:00:26 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-02-20 02:17:30 -07:00
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
if (user.Password.IndexOf('$') == -1)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
|
|
|
string hash = user.Password;
|
2019-03-07 03:41:44 -07:00
|
|
|
user.Password = string.Format("$SHA1${0}", hash);
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
2019-05-11 16:32:20 -07:00
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
if (user.EasyPassword != null
|
|
|
|
&& user.EasyPassword.IndexOf('$') == -1)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
|
|
|
string hash = user.EasyPassword;
|
|
|
|
user.EasyPassword = string.Format("$SHA1${0}", hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
public bool HasPassword(User user)
|
|
|
|
=> !string.IsNullOrEmpty(user.Password);
|
2019-02-20 02:17:30 -07:00
|
|
|
|
|
|
|
public Task ChangePassword(User user, string newPassword)
|
|
|
|
{
|
2019-02-18 02:26:01 -07:00
|
|
|
ConvertPasswordFormat(user);
|
2019-03-05 00:58:25 -07:00
|
|
|
// This is needed to support changing a no password user to a password user
|
2019-02-18 02:26:01 -07:00
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
PasswordHash newPasswordHash = new PasswordHash(_cryptographyProvider);
|
2019-05-21 10:28:34 -07:00
|
|
|
newPasswordHash.Salt = _cryptographyProvider.GenerateSalt();
|
2019-02-20 02:17:30 -07:00
|
|
|
newPasswordHash.Id = _cryptographyProvider.DefaultHashMethod;
|
2019-05-21 10:28:34 -07:00
|
|
|
newPasswordHash.Hash = GetHashedChangeAuth(newPassword, newPasswordHash);
|
2019-02-18 02:26:01 -07:00
|
|
|
user.Password = newPasswordHash.ToString();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:17:30 -07:00
|
|
|
PasswordHash passwordHash = new PasswordHash(user.Password);
|
2019-05-21 10:28:34 -07:00
|
|
|
if (passwordHash.Id == "SHA1"
|
|
|
|
&& passwordHash.Salt.Length == 0)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
passwordHash.Salt = _cryptographyProvider.GenerateSalt();
|
2019-02-20 02:17:30 -07:00
|
|
|
passwordHash.Id = _cryptographyProvider.DefaultHashMethod;
|
2019-05-21 10:28:34 -07:00
|
|
|
passwordHash.Hash = GetHashedChangeAuth(newPassword, passwordHash);
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
else if (newPassword != null)
|
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
passwordHash.Hash = GetHashed(user, newPassword);
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = passwordHash.ToString();
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:46:55 -07:00
|
|
|
public void ChangeEasyPassword(User user, string newPassword, string newPasswordHash)
|
|
|
|
{
|
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
|
|
|
|
if (newPassword != null)
|
|
|
|
{
|
|
|
|
newPasswordHash = string.Format("$SHA1${0}", GetHashedString(user, newPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(newPasswordHash))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(newPasswordHash));
|
|
|
|
}
|
|
|
|
|
|
|
|
user.EasyPassword = newPasswordHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetEasyPasswordHash(User user)
|
|
|
|
{
|
|
|
|
// This should be removed in the future. This was added to let user login after
|
|
|
|
// Jellyfin 10.3.3 failed to save a well formatted PIN.
|
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
|
|
|
|
return string.IsNullOrEmpty(user.EasyPassword)
|
|
|
|
? null
|
2019-05-21 10:28:34 -07:00
|
|
|
: PasswordHash.ConvertToByteString(new PasswordHash(user.EasyPassword).Hash);
|
2019-05-25 10:46:55 -07:00
|
|
|
}
|
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
internal byte[] GetHashedChangeAuth(string newPassword, PasswordHash passwordHash)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
2019-05-21 10:28:34 -07:00
|
|
|
passwordHash.Hash = Encoding.UTF8.GetBytes(newPassword);
|
|
|
|
return _cryptographyProvider.ComputeHash(passwordHash);
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the hashed string.
|
|
|
|
/// </summary>
|
|
|
|
public string GetHashedString(User user, string str)
|
|
|
|
{
|
|
|
|
PasswordHash passwordHash;
|
2019-03-05 00:58:25 -07:00
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
|
|
|
passwordHash = new PasswordHash(_cryptographyProvider);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
passwordHash = new PasswordHash(user.Password);
|
|
|
|
}
|
|
|
|
|
2019-05-21 10:28:34 -07:00
|
|
|
if (passwordHash.Salt != null)
|
2019-02-20 02:17:30 -07:00
|
|
|
{
|
2019-03-05 00:58:25 -07:00
|
|
|
// the password is modern format with PBKDF and we should take advantage of that
|
2019-05-21 10:28:34 -07:00
|
|
|
passwordHash.Hash = Encoding.UTF8.GetBytes(str);
|
2019-02-20 02:17:30 -07:00
|
|
|
return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-05 00:58:25 -07:00
|
|
|
// the password has no salt and should be called with the older method for safety
|
2019-02-20 02:17:30 -07:00
|
|
|
return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str)));
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 10:28:34 -07:00
|
|
|
|
|
|
|
public byte[] GetHashed(User user, string str)
|
|
|
|
{
|
|
|
|
PasswordHash passwordHash;
|
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
passwordHash = new PasswordHash(_cryptographyProvider);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
passwordHash = new PasswordHash(user.Password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (passwordHash.Salt != null)
|
|
|
|
{
|
|
|
|
// the password is modern format with PBKDF and we should take advantage of that
|
|
|
|
passwordHash.Hash = Encoding.UTF8.GetBytes(str);
|
|
|
|
return _cryptographyProvider.ComputeHash(passwordHash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// the password has no salt and should be called with the older method for safety
|
|
|
|
return _cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str));
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 02:17:30 -07:00
|
|
|
}
|
|
|
|
}
|