DefaultAuthenticationProvider.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Controller.Authentication;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Model.Cryptography;
  8. namespace Emby.Server.Implementations.Library
  9. {
  10. public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
  11. {
  12. private readonly ICryptoProvider _cryptographyProvider;
  13. public DefaultAuthenticationProvider(ICryptoProvider crypto)
  14. {
  15. _cryptographyProvider = crypto;
  16. }
  17. public string Name => "Default";
  18. public bool IsEnabled => true;
  19. // This is dumb and an artifact of the backwards way auth providers were designed.
  20. // This version of authenticate was never meant to be called, but needs to be here for interface compat
  21. // Only the providers that don't provide local user support use this
  22. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  23. {
  24. throw new NotImplementedException();
  25. }
  26. // This is the verson that we need to use for local users. Because reasons.
  27. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
  28. {
  29. bool success = false;
  30. if (resolvedUser == null)
  31. {
  32. throw new Exception("Invalid username or password");
  33. }
  34. // As long as jellyfin supports passwordless users, we need this little block here to accomodate
  35. if (IsPasswordEmpty(resolvedUser, password))
  36. {
  37. return Task.FromResult(new ProviderAuthenticationResult
  38. {
  39. Username = username
  40. });
  41. }
  42. ConvertPasswordFormat(resolvedUser);
  43. byte[] passwordbytes = Encoding.UTF8.GetBytes(password);
  44. PasswordHash readyHash = new PasswordHash(resolvedUser.Password);
  45. byte[] calculatedHash;
  46. string calculatedHashString;
  47. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id) || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  48. {
  49. if (string.IsNullOrEmpty(readyHash.Salt))
  50. {
  51. calculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes);
  52. calculatedHashString = BitConverter.ToString(calculatedHash).Replace("-", string.Empty);
  53. }
  54. else
  55. {
  56. calculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes, readyHash.SaltBytes);
  57. calculatedHashString = BitConverter.ToString(calculatedHash).Replace("-", string.Empty);
  58. }
  59. if (calculatedHashString == readyHash.Hash)
  60. {
  61. success = true;
  62. // throw new Exception("Invalid username or password");
  63. }
  64. }
  65. else
  66. {
  67. throw new Exception(string.Format($"Requested crypto method not available in provider: {readyHash.Id}"));
  68. }
  69. // var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase);
  70. if (!success)
  71. {
  72. throw new Exception("Invalid username or password");
  73. }
  74. return Task.FromResult(new ProviderAuthenticationResult
  75. {
  76. Username = username
  77. });
  78. }
  79. // This allows us to move passwords forward to the newformat without breaking. They are still insecure, unsalted, and dumb before a password change
  80. // but at least they are in the new format.
  81. private void ConvertPasswordFormat(User user)
  82. {
  83. if (string.IsNullOrEmpty(user.Password))
  84. {
  85. return;
  86. }
  87. if (!user.Password.Contains("$"))
  88. {
  89. string hash = user.Password;
  90. user.Password = string.Format("$SHA1${0}", hash);
  91. }
  92. if (user.EasyPassword != null && !user.EasyPassword.Contains("$"))
  93. {
  94. string hash = user.EasyPassword;
  95. user.EasyPassword = string.Format("$SHA1${0}", hash);
  96. }
  97. }
  98. public Task<bool> HasPassword(User user)
  99. {
  100. var hasConfiguredPassword = !IsPasswordEmpty(user, GetPasswordHash(user));
  101. return Task.FromResult(hasConfiguredPassword);
  102. }
  103. private bool IsPasswordEmpty(User user, string password)
  104. {
  105. return (string.IsNullOrEmpty(user.Password) && string.IsNullOrEmpty(password));
  106. }
  107. public Task ChangePassword(User user, string newPassword)
  108. {
  109. ConvertPasswordFormat(user);
  110. // This is needed to support changing a no password user to a password user
  111. if (string.IsNullOrEmpty(user.Password))
  112. {
  113. PasswordHash newPasswordHash = new PasswordHash(_cryptographyProvider);
  114. newPasswordHash.SaltBytes = _cryptographyProvider.GenerateSalt();
  115. newPasswordHash.Salt = PasswordHash.ConvertToByteString(newPasswordHash.SaltBytes);
  116. newPasswordHash.Id = _cryptographyProvider.DefaultHashMethod;
  117. newPasswordHash.Hash = GetHashedStringChangeAuth(newPassword, newPasswordHash);
  118. user.Password = newPasswordHash.ToString();
  119. return Task.CompletedTask;
  120. }
  121. PasswordHash passwordHash = new PasswordHash(user.Password);
  122. if (passwordHash.Id == "SHA1" && string.IsNullOrEmpty(passwordHash.Salt))
  123. {
  124. passwordHash.SaltBytes = _cryptographyProvider.GenerateSalt();
  125. passwordHash.Salt = PasswordHash.ConvertToByteString(passwordHash.SaltBytes);
  126. passwordHash.Id = _cryptographyProvider.DefaultHashMethod;
  127. passwordHash.Hash = GetHashedStringChangeAuth(newPassword, passwordHash);
  128. }
  129. else if (newPassword != null)
  130. {
  131. passwordHash.Hash = GetHashedString(user, newPassword);
  132. }
  133. if (string.IsNullOrWhiteSpace(passwordHash.Hash))
  134. {
  135. throw new ArgumentNullException(nameof(passwordHash.Hash));
  136. }
  137. user.Password = passwordHash.ToString();
  138. return Task.CompletedTask;
  139. }
  140. public string GetPasswordHash(User user)
  141. {
  142. return user.Password;
  143. }
  144. public void ChangeEasyPassword(User user, string newPassword, string newPasswordHash)
  145. {
  146. ConvertPasswordFormat(user);
  147. if (newPassword != null)
  148. {
  149. newPasswordHash = string.Format("$SHA1${0}", GetHashedString(user, newPassword));
  150. }
  151. if (string.IsNullOrWhiteSpace(newPasswordHash))
  152. {
  153. throw new ArgumentNullException(nameof(newPasswordHash));
  154. }
  155. user.EasyPassword = newPasswordHash;
  156. }
  157. public string GetEasyPasswordHash(User user)
  158. {
  159. // This should be removed in the future. This was added to let user login after
  160. // Jellyfin 10.3.3 failed to save a well formatted PIN.
  161. ConvertPasswordFormat(user);
  162. return string.IsNullOrEmpty(user.EasyPassword)
  163. ? null
  164. : (new PasswordHash(user.EasyPassword)).Hash;
  165. }
  166. public string GetHashedStringChangeAuth(string newPassword, PasswordHash passwordHash)
  167. {
  168. passwordHash.HashBytes = Encoding.UTF8.GetBytes(newPassword);
  169. return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
  170. }
  171. /// <summary>
  172. /// Gets the hashed string.
  173. /// </summary>
  174. public string GetHashedString(User user, string str)
  175. {
  176. PasswordHash passwordHash;
  177. if (string.IsNullOrEmpty(user.Password))
  178. {
  179. passwordHash = new PasswordHash(_cryptographyProvider);
  180. }
  181. else
  182. {
  183. ConvertPasswordFormat(user);
  184. passwordHash = new PasswordHash(user.Password);
  185. }
  186. if (passwordHash.SaltBytes != null)
  187. {
  188. // the password is modern format with PBKDF and we should take advantage of that
  189. passwordHash.HashBytes = Encoding.UTF8.GetBytes(str);
  190. return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
  191. }
  192. else
  193. {
  194. // the password has no salt and should be called with the older method for safety
  195. return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str)));
  196. }
  197. }
  198. }
  199. }