DefaultAuthenticationProvider.cs 6.1 KB

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