DefaultAuthenticationProvider.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  57. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  58. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  59. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  60. {
  61. byte[] calculatedHash = _cryptographyProvider.ComputeHash(
  62. readyHash.Id,
  63. passwordBytes,
  64. readyHash.Salt.ToArray());
  65. if (readyHash.Hash.SequenceEqual(calculatedHash))
  66. {
  67. success = true;
  68. }
  69. }
  70. else
  71. {
  72. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  73. }
  74. if (!success)
  75. {
  76. throw new AuthenticationException("Invalid username or 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. /// <inheritdoc />
  99. public void ChangeEasyPassword(User user, string newPassword, string newPasswordHash)
  100. {
  101. if (newPassword != null)
  102. {
  103. newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword).ToString();
  104. }
  105. if (string.IsNullOrWhiteSpace(newPasswordHash))
  106. {
  107. throw new ArgumentNullException(nameof(newPasswordHash));
  108. }
  109. user.EasyPassword = newPasswordHash;
  110. }
  111. /// <inheritdoc />
  112. public string? GetEasyPasswordHash(User user)
  113. {
  114. return string.IsNullOrEmpty(user.EasyPassword)
  115. ? null
  116. : Hex.Encode(PasswordHash.Parse(user.EasyPassword).Hash);
  117. }
  118. }
  119. }