DefaultAuthenticationProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #nullable enable
  2. using System;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Data.Entities;
  7. using MediaBrowser.Common;
  8. using MediaBrowser.Common.Cryptography;
  9. using MediaBrowser.Controller.Authentication;
  10. using MediaBrowser.Model.Cryptography;
  11. namespace Jellyfin.Server.Implementations.Users
  12. {
  13. /// <summary>
  14. /// The default authentication provider.
  15. /// </summary>
  16. public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
  17. {
  18. private readonly ICryptoProvider _cryptographyProvider;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
  21. /// </summary>
  22. /// <param name="cryptographyProvider">The cryptography provider.</param>
  23. public DefaultAuthenticationProvider(ICryptoProvider cryptographyProvider)
  24. {
  25. _cryptographyProvider = cryptographyProvider;
  26. }
  27. /// <inheritdoc />
  28. public string Name => "Default";
  29. /// <inheritdoc />
  30. public bool IsEnabled => true;
  31. /// <inheritdoc />
  32. // This is dumb and an artifact of the backwards way auth providers were designed.
  33. // This version of authenticate was never meant to be called, but needs to be here for interface compat
  34. // Only the providers that don't provide local user support use this
  35. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. /// <inheritdoc />
  40. // This is the version that we need to use for local users. Because reasons.
  41. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
  42. {
  43. if (resolvedUser == null)
  44. {
  45. throw new AuthenticationException("Specified user does not exist.");
  46. }
  47. bool success = false;
  48. // As long as jellyfin supports passwordless users, we need this little block here to accommodate
  49. if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(password))
  50. {
  51. return Task.FromResult(new ProviderAuthenticationResult
  52. {
  53. Username = username
  54. });
  55. }
  56. // Handle the case when the stored password is null, but the user tried to login with a password
  57. if (resolvedUser.Password != null)
  58. {
  59. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  60. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  61. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  62. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  63. {
  64. byte[] calculatedHash = _cryptographyProvider.ComputeHash(
  65. readyHash.Id,
  66. passwordBytes,
  67. readyHash.Salt.ToArray());
  68. if (readyHash.Hash.SequenceEqual(calculatedHash))
  69. {
  70. success = true;
  71. }
  72. }
  73. else
  74. {
  75. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  76. }
  77. }
  78. if (!success)
  79. {
  80. throw new AuthenticationException("Invalid username or password");
  81. }
  82. return Task.FromResult(new ProviderAuthenticationResult
  83. {
  84. Username = username
  85. });
  86. }
  87. /// <inheritdoc />
  88. public bool HasPassword(User user)
  89. => !string.IsNullOrEmpty(user?.Password);
  90. /// <inheritdoc />
  91. public Task ChangePassword(User user, string newPassword)
  92. {
  93. if (string.IsNullOrEmpty(newPassword))
  94. {
  95. user.Password = null;
  96. return Task.CompletedTask;
  97. }
  98. PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
  99. user.Password = newPasswordHash.ToString();
  100. return Task.CompletedTask;
  101. }
  102. /// <inheritdoc />
  103. public void ChangeEasyPassword(User user, string newPassword, string newPasswordHash)
  104. {
  105. if (newPassword != null)
  106. {
  107. newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword).ToString();
  108. }
  109. if (string.IsNullOrWhiteSpace(newPasswordHash))
  110. {
  111. throw new ArgumentNullException(nameof(newPasswordHash));
  112. }
  113. user.EasyPassword = newPasswordHash;
  114. }
  115. /// <inheritdoc />
  116. public string? GetEasyPasswordHash(User user)
  117. {
  118. return string.IsNullOrEmpty(user.EasyPassword)
  119. ? null
  120. : Hex.Encode(PasswordHash.Parse(user.EasyPassword).Hash);
  121. }
  122. }
  123. }