DefaultAuthenticationProvider.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common;
  6. using MediaBrowser.Common.Cryptography;
  7. using MediaBrowser.Controller.Authentication;
  8. using MediaBrowser.Model.Cryptography;
  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 ICryptoProvider _cryptographyProvider;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
  19. /// </summary>
  20. /// <param name="cryptographyProvider">The cryptography provider.</param>
  21. public DefaultAuthenticationProvider(ICryptoProvider cryptographyProvider)
  22. {
  23. _cryptographyProvider = cryptographyProvider;
  24. }
  25. /// <inheritdoc />
  26. public string Name => "Default";
  27. /// <inheritdoc />
  28. public bool IsEnabled => true;
  29. /// <inheritdoc />
  30. // This is dumb and an artifact of the backwards way auth providers were designed.
  31. // This version of authenticate was never meant to be called, but needs to be here for interface compat
  32. // Only the providers that don't provide local user support use this
  33. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. /// <inheritdoc />
  38. // This is the version that we need to use for local users. Because reasons.
  39. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, Data.Entities.User resolvedUser)
  40. {
  41. if (resolvedUser == null)
  42. {
  43. throw new AuthenticationException("Specified user does not exist.");
  44. }
  45. bool success = false;
  46. // As long as jellyfin supports passwordless users, we need this little block here to accommodate
  47. if (!HasPassword(resolvedUser))
  48. {
  49. return Task.FromResult(new ProviderAuthenticationResult
  50. {
  51. Username = username
  52. });
  53. }
  54. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  55. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  56. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  57. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  58. {
  59. byte[] calculatedHash = _cryptographyProvider.ComputeHash(
  60. readyHash.Id,
  61. passwordBytes,
  62. readyHash.Salt.ToArray());
  63. if (readyHash.Hash.SequenceEqual(calculatedHash))
  64. {
  65. success = true;
  66. }
  67. }
  68. else
  69. {
  70. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  71. }
  72. if (!success)
  73. {
  74. throw new AuthenticationException("Invalid username or password");
  75. }
  76. return Task.FromResult(new ProviderAuthenticationResult
  77. {
  78. Username = username
  79. });
  80. }
  81. /// <inheritdoc />
  82. public bool HasPassword(Data.Entities.User user)
  83. => !string.IsNullOrEmpty(user.Password);
  84. /// <inheritdoc />
  85. public Task ChangePassword(Data.Entities.User user, string newPassword)
  86. {
  87. if (string.IsNullOrEmpty(newPassword))
  88. {
  89. user.Password = null;
  90. return Task.CompletedTask;
  91. }
  92. PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
  93. user.Password = newPasswordHash.ToString();
  94. return Task.CompletedTask;
  95. }
  96. /// <inheritdoc />
  97. public void ChangeEasyPassword(Data.Entities.User user, string newPassword, string newPasswordHash)
  98. {
  99. if (newPassword != null)
  100. {
  101. newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword).ToString();
  102. }
  103. if (string.IsNullOrWhiteSpace(newPasswordHash))
  104. {
  105. throw new ArgumentNullException(nameof(newPasswordHash));
  106. }
  107. user.EasyPassword = newPasswordHash;
  108. }
  109. /// <inheritdoc />
  110. public string GetEasyPasswordHash(Data.Entities.User user)
  111. {
  112. return string.IsNullOrEmpty(user.EasyPassword)
  113. ? null
  114. : Hex.Encode(PasswordHash.Parse(user.EasyPassword).Hash);
  115. }
  116. /// <summary>
  117. /// Hashes the provided string.
  118. /// </summary>
  119. /// <param name="user">The user.</param>
  120. /// <param name="str">The string to hash.</param>
  121. /// <returns>The hashed string.</returns>
  122. public string GetHashedString(Data.Entities.User user, string str)
  123. {
  124. if (string.IsNullOrEmpty(user.Password))
  125. {
  126. return _cryptographyProvider.CreatePasswordHash(str).ToString();
  127. }
  128. // TODO: make use of iterations parameter?
  129. PasswordHash passwordHash = PasswordHash.Parse(user.Password);
  130. var salt = passwordHash.Salt.ToArray();
  131. return new PasswordHash(
  132. passwordHash.Id,
  133. _cryptographyProvider.ComputeHash(
  134. passwordHash.Id,
  135. Encoding.UTF8.GetBytes(str),
  136. salt),
  137. salt,
  138. passwordHash.Parameters.ToDictionary(x => x.Key, y => y.Value)).ToString();
  139. }
  140. /// <summary>
  141. /// Hashes the provided string.
  142. /// </summary>
  143. /// <param name="user">The user.</param>
  144. /// <param name="str">The string to hash.</param>
  145. /// <returns>The hashed string.</returns>
  146. public ReadOnlySpan<byte> GetHashed(Data.Entities.User user, string str)
  147. {
  148. if (string.IsNullOrEmpty(user.Password))
  149. {
  150. return _cryptographyProvider.CreatePasswordHash(str).Hash;
  151. }
  152. // TODO: make use of iterations parameter?
  153. PasswordHash passwordHash = PasswordHash.Parse(user.Password);
  154. return _cryptographyProvider.ComputeHash(
  155. passwordHash.Id,
  156. Encoding.UTF8.GetBytes(str),
  157. passwordHash.Salt.ToArray());
  158. }
  159. }
  160. }