DefaultAuthenticationProvider.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Data.Entities;
  6. using MediaBrowser.Controller.Authentication;
  7. using MediaBrowser.Model.Cryptography;
  8. using Microsoft.Extensions.Logging;
  9. namespace Jellyfin.Server.Implementations.Users
  10. {
  11. /// <summary>
  12. /// The default authentication provider.
  13. /// </summary>
  14. public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
  15. {
  16. private readonly ILogger<DefaultAuthenticationProvider> _logger;
  17. private readonly ICryptoProvider _cryptographyProvider;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
  20. /// </summary>
  21. /// <param name="logger">The logger.</param>
  22. /// <param name="cryptographyProvider">The cryptography provider.</param>
  23. public DefaultAuthenticationProvider(ILogger<DefaultAuthenticationProvider> logger, ICryptoProvider cryptographyProvider)
  24. {
  25. _logger = logger;
  26. _cryptographyProvider = cryptographyProvider;
  27. }
  28. /// <inheritdoc />
  29. public string Name => "Default";
  30. /// <inheritdoc />
  31. public bool IsEnabled => true;
  32. /// <inheritdoc />
  33. // This is dumb and an artifact of the backwards way auth providers were designed.
  34. // This version of authenticate was never meant to be called, but needs to be here for interface compat
  35. // Only the providers that don't provide local user support use this
  36. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. /// <inheritdoc />
  41. // This is the version that we need to use for local users. Because reasons.
  42. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User? resolvedUser)
  43. {
  44. [DoesNotReturn]
  45. static void ThrowAuthenticationException()
  46. {
  47. throw new AuthenticationException("Invalid username or password");
  48. }
  49. if (resolvedUser is null)
  50. {
  51. ThrowAuthenticationException();
  52. }
  53. // As long as jellyfin supports password-less users, we need this little block here to accommodate
  54. if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(password))
  55. {
  56. return Task.FromResult(new ProviderAuthenticationResult
  57. {
  58. Username = username
  59. });
  60. }
  61. // Handle the case when the stored password is null, but the user tried to login with a password
  62. if (resolvedUser.Password is null)
  63. {
  64. ThrowAuthenticationException();
  65. }
  66. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  67. if (!_cryptographyProvider.Verify(readyHash, password))
  68. {
  69. ThrowAuthenticationException();
  70. }
  71. // Migrate old hashes to the new default
  72. if (!string.Equals(readyHash.Id, _cryptographyProvider.DefaultHashMethod, StringComparison.Ordinal)
  73. || int.Parse(readyHash.Parameters["iterations"], CultureInfo.InvariantCulture) != Constants.DefaultIterations)
  74. {
  75. _logger.LogInformation("Migrating password hash of {User} to the latest default", username);
  76. ChangePassword(resolvedUser, password);
  77. }
  78. return Task.FromResult(new ProviderAuthenticationResult
  79. {
  80. Username = username
  81. });
  82. }
  83. /// <inheritdoc />
  84. public bool HasPassword(User user)
  85. => !string.IsNullOrEmpty(user?.Password);
  86. /// <inheritdoc />
  87. public Task ChangePassword(User user, string newPassword)
  88. {
  89. if (string.IsNullOrEmpty(newPassword))
  90. {
  91. user.Password = null;
  92. return Task.CompletedTask;
  93. }
  94. PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
  95. user.Password = newPasswordHash.ToString();
  96. return Task.CompletedTask;
  97. }
  98. }
  99. }