2019-02-13 08:43:48 +00: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;
|
|
|
|
public DefaultAuthenticationProvider(ICryptoProvider crypto)
|
|
|
|
{
|
|
|
|
_cryptographyProvider = crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Name => "Default";
|
|
|
|
|
|
|
|
public bool IsEnabled => true;
|
|
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
|
public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//This is the verson that we need to use for local users. Because reasons.
|
2019-02-12 10:16:03 +00:00
|
|
|
public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
|
2019-02-13 08:43:48 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
2019-02-12 10:16:03 +00:00
|
|
|
if (resolvedUser == null)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid username or password");
|
2019-02-18 09:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//As long as jellyfin supports passwordless users, we need this little block here to accomodate
|
|
|
|
if (IsPasswordEmpty(resolvedUser, password))
|
|
|
|
{
|
|
|
|
return Task.FromResult(new ProviderAuthenticationResult
|
|
|
|
{
|
|
|
|
Username = username
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:30:26 +00:00
|
|
|
ConvertPasswordFormat(resolvedUser);
|
|
|
|
byte[] passwordbytes = Encoding.UTF8.GetBytes(password);
|
2019-02-13 08:33:00 +00:00
|
|
|
|
|
|
|
PasswordHash readyHash = new PasswordHash(resolvedUser.Password);
|
2019-02-13 08:43:48 +00:00
|
|
|
byte[] CalculatedHash;
|
2019-02-12 10:16:03 +00:00
|
|
|
string CalculatedHashString;
|
2019-02-18 08:31:03 +00:00
|
|
|
if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id))
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
2019-02-13 08:33:00 +00:00
|
|
|
if (String.IsNullOrEmpty(readyHash.Salt))
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
2019-02-13 08:43:48 +00:00
|
|
|
CalculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes);
|
2019-02-12 10:16:03 +00:00
|
|
|
CalculatedHashString = BitConverter.ToString(CalculatedHash).Replace("-", string.Empty);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-13 08:43:48 +00:00
|
|
|
CalculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes, readyHash.SaltBytes);
|
2019-02-12 10:16:03 +00:00
|
|
|
CalculatedHashString = BitConverter.ToString(CalculatedHash).Replace("-", string.Empty);
|
|
|
|
}
|
2019-02-13 08:33:00 +00:00
|
|
|
if (CalculatedHashString == readyHash.Hash)
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
//throw new Exception("Invalid username or password");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-18 08:31:03 +00:00
|
|
|
throw new Exception(String.Format($"Requested crypto method not available in provider: {readyHash.Id}"));
|
2019-02-12 10:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid username or password");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult(new ProviderAuthenticationResult
|
|
|
|
{
|
|
|
|
Username = username
|
|
|
|
});
|
2019-02-13 08:43:48 +00: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-12 10:16:03 +00:00
|
|
|
private void ConvertPasswordFormat(User user)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
if (!user.Password.Contains("$"))
|
|
|
|
{
|
|
|
|
string hash = user.Password;
|
|
|
|
user.Password = String.Format("$SHA1${0}", hash);
|
2019-02-13 08:43:48 +00:00
|
|
|
}
|
2019-02-13 08:44:07 +00:00
|
|
|
|
2019-02-12 10:16:03 +00:00
|
|
|
if (user.EasyPassword != null && !user.EasyPassword.Contains("$"))
|
|
|
|
{
|
|
|
|
string hash = user.EasyPassword;
|
2019-02-18 08:31:03 +00:00
|
|
|
user.EasyPassword = string.Format("$SHA1${0}", hash);
|
2019-02-12 10:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 08:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<bool> HasPassword(User user)
|
|
|
|
{
|
|
|
|
var hasConfiguredPassword = !IsPasswordEmpty(user, GetPasswordHash(user));
|
|
|
|
return Task.FromResult(hasConfiguredPassword);
|
|
|
|
}
|
|
|
|
|
2019-02-18 09:26:01 +00:00
|
|
|
private bool IsPasswordEmpty(User user, string password)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
return string.IsNullOrEmpty(password);
|
|
|
|
}
|
|
|
|
return false;
|
2019-02-13 08:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task ChangePassword(User user, string newPassword)
|
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
//This is needed to support changing a no password user to a password user
|
|
|
|
if (string.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
PasswordHash newPasswordHash = new PasswordHash(_cryptographyProvider);
|
|
|
|
newPasswordHash.SaltBytes = _cryptographyProvider.GenerateSalt();
|
|
|
|
newPasswordHash.Salt = PasswordHash.ConvertToByteString(newPasswordHash.SaltBytes);
|
|
|
|
newPasswordHash.Id = _cryptographyProvider.DefaultHashMethod;
|
|
|
|
newPasswordHash.Hash = GetHashedStringChangeAuth(newPassword, newPasswordHash);
|
|
|
|
user.Password = newPasswordHash.ToString();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:43:48 +00:00
|
|
|
PasswordHash passwordHash = new PasswordHash(user.Password);
|
|
|
|
if (passwordHash.Id == "SHA1" && string.IsNullOrEmpty(passwordHash.Salt))
|
|
|
|
{
|
|
|
|
passwordHash.SaltBytes = _cryptographyProvider.GenerateSalt();
|
|
|
|
passwordHash.Salt = PasswordHash.ConvertToByteString(passwordHash.SaltBytes);
|
|
|
|
passwordHash.Id = _cryptographyProvider.DefaultHashMethod;
|
|
|
|
passwordHash.Hash = GetHashedStringChangeAuth(newPassword, passwordHash);
|
2019-02-18 08:31:03 +00:00
|
|
|
}
|
|
|
|
else if (newPassword != null)
|
2019-02-13 08:43:48 +00:00
|
|
|
{
|
|
|
|
passwordHash.Hash = GetHashedString(user, newPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(passwordHash.Hash))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(passwordHash.Hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = passwordHash.ToString();
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetPasswordHash(User user)
|
|
|
|
{
|
|
|
|
return user.Password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetHashedStringChangeAuth(string newPassword, PasswordHash passwordHash)
|
|
|
|
{
|
|
|
|
passwordHash.HashBytes = Encoding.UTF8.GetBytes(newPassword);
|
|
|
|
return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the hashed string.
|
|
|
|
/// </summary>
|
2019-02-12 10:16:03 +00:00
|
|
|
public string GetHashedString(User user, string str)
|
|
|
|
{
|
|
|
|
PasswordHash passwordHash;
|
|
|
|
if (String.IsNullOrEmpty(user.Password))
|
|
|
|
{
|
|
|
|
passwordHash = new PasswordHash(_cryptographyProvider);
|
|
|
|
}
|
|
|
|
else
|
2019-02-13 08:43:48 +00:00
|
|
|
{
|
2019-02-12 10:16:03 +00:00
|
|
|
ConvertPasswordFormat(user);
|
|
|
|
passwordHash = new PasswordHash(user.Password);
|
|
|
|
}
|
|
|
|
if (passwordHash.SaltBytes != null)
|
2019-02-13 08:43:48 +00:00
|
|
|
{
|
|
|
|
//the password is modern format with PBKDF and we should take advantage of that
|
|
|
|
passwordHash.HashBytes = Encoding.UTF8.GetBytes(str);
|
2019-02-13 08:33:00 +00:00
|
|
|
return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
|
2019-02-12 10:16:03 +00:00
|
|
|
}
|
|
|
|
else
|
2019-02-13 08:43:48 +00:00
|
|
|
{
|
2019-02-13 08:33:00 +00:00
|
|
|
//the password has no salt and should be called with the older method for safety
|
|
|
|
return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str)));
|
2019-02-13 08:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|