Files
jellyfin-jellyfin-1/Jellyfin.Server.Implementations/Users/DefaultAuthenticationProvider.cs

116 lines
4.3 KiB
C#
Raw Normal View History

2019-02-20 01:17:30 -08:00
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
2019-02-20 01:17:30 -08:00
using System.Threading.Tasks;
2025-03-25 16:45:00 +01:00
using Jellyfin.Database.Implementations.Entities;
2019-02-20 01:17:30 -08:00
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Model.Cryptography;
using Microsoft.Extensions.Logging;
2019-02-20 01:17:30 -08:00
2020-05-15 17:24:01 -04:00
namespace Jellyfin.Server.Implementations.Users
2019-02-20 01:17:30 -08:00
{
2019-11-01 18:38:54 +01:00
/// <summary>
/// The default authentication provider.
/// </summary>
2019-02-20 01:17:30 -08:00
public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
{
private readonly ILogger<DefaultAuthenticationProvider> _logger;
2019-02-20 01:17:30 -08:00
private readonly ICryptoProvider _cryptographyProvider;
2019-11-01 18:38:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
2019-11-01 18:38:54 +01:00
/// <param name="cryptographyProvider">The cryptography provider.</param>
public DefaultAuthenticationProvider(ILogger<DefaultAuthenticationProvider> logger, ICryptoProvider cryptographyProvider)
2019-02-20 01:17:30 -08:00
{
_logger = logger;
2019-05-21 19:28:34 +02:00
_cryptographyProvider = cryptographyProvider;
2019-02-20 01:17:30 -08:00
}
/// <inheritdoc />
2019-02-20 01:17:30 -08:00
public string Name => "Default";
/// <inheritdoc />
2019-02-20 01:17:30 -08:00
public bool IsEnabled => true;
/// <inheritdoc />
2019-03-04 23:58:25 -08: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 01:17:30 -08:00
public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
{
throw new NotImplementedException();
}
/// <inheritdoc />
2019-05-21 19:28:34 +02:00
// This is the version that we need to use for local users. Because reasons.
public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User? resolvedUser)
2019-02-20 01:17:30 -08:00
{
[DoesNotReturn]
static void ThrowAuthenticationException()
2019-02-20 01:17:30 -08:00
{
throw new AuthenticationException("Invalid username or password");
}
if (resolvedUser is null)
{
ThrowAuthenticationException();
}
2019-11-01 18:38:54 +01:00
2020-11-18 13:46:14 +00:00
// As long as jellyfin supports password-less users, we need this little block here to accommodate
2020-05-23 14:53:24 -04:00
if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(password))
{
return Task.FromResult(new ProviderAuthenticationResult
{
Username = username
});
}
2019-02-20 01:17:30 -08:00
// Handle the case when the stored password is null, but the user tried to login with a password
2022-12-05 15:00:20 +01:00
if (resolvedUser.Password is null)
2019-02-20 01:17:30 -08:00
{
ThrowAuthenticationException();
2019-02-20 01:17:30 -08:00
}
PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
if (!_cryptographyProvider.Verify(readyHash, password))
2019-02-20 01:17:30 -08:00
{
ThrowAuthenticationException();
2019-02-20 01:17:30 -08:00
}
// Migrate old hashes to the new default
if (!string.Equals(readyHash.Id, _cryptographyProvider.DefaultHashMethod, StringComparison.Ordinal)
|| int.Parse(readyHash.Parameters["iterations"], CultureInfo.InvariantCulture) != Constants.DefaultIterations)
{
_logger.LogInformation("Migrating password hash of {User} to the latest default", username);
ChangePassword(resolvedUser, password);
}
2019-02-20 01:17:30 -08:00
return Task.FromResult(new ProviderAuthenticationResult
{
Username = username
});
}
/// <inheritdoc />
2020-05-20 13:07:53 -04:00
public bool HasPassword(User user)
2020-05-30 00:19:36 -04:00
=> !string.IsNullOrEmpty(user?.Password);
2019-02-20 01:17:30 -08:00
/// <inheritdoc />
2020-05-20 13:07:53 -04:00
public Task ChangePassword(User user, string newPassword)
2019-02-20 01:17:30 -08:00
{
if (string.IsNullOrEmpty(newPassword))
{
user.Password = null;
return Task.CompletedTask;
}
PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
user.Password = newPasswordHash.ToString();
2019-02-20 01:17:30 -08:00
return Task.CompletedTask;
}
}
}