DefaultAuthenticationProvider.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 cryptographyProvider)
  14. {
  15. _cryptographyProvider = cryptographyProvider;
  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 version 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 ArgumentNullException(nameof(resolvedUser));
  33. }
  34. // As long as jellyfin supports passwordless users, we need this little block here to accomodate
  35. if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(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. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  46. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  47. {
  48. byte[] calculatedHash = _cryptographyProvider.ComputeHash(readyHash.Id, passwordbytes, readyHash.Salt);
  49. if (calculatedHash.SequenceEqual(readyHash.Hash))
  50. {
  51. success = true;
  52. }
  53. }
  54. else
  55. {
  56. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  57. }
  58. if (!success)
  59. {
  60. throw new AuthenticationException("Invalid username or password");
  61. }
  62. return Task.FromResult(new ProviderAuthenticationResult
  63. {
  64. Username = username
  65. });
  66. }
  67. // This allows us to move passwords forward to the newformat without breaking. They are still insecure, unsalted, and dumb before a password change
  68. // but at least they are in the new format.
  69. private void ConvertPasswordFormat(User user)
  70. {
  71. if (string.IsNullOrEmpty(user.Password))
  72. {
  73. return;
  74. }
  75. if (user.Password.IndexOf('$') == -1)
  76. {
  77. string hash = user.Password;
  78. user.Password = string.Format("$SHA1${0}", hash);
  79. }
  80. if (user.EasyPassword != null
  81. && user.EasyPassword.IndexOf('$') == -1)
  82. {
  83. string hash = user.EasyPassword;
  84. user.EasyPassword = string.Format("$SHA1${0}", hash);
  85. }
  86. }
  87. public bool HasPassword(User user)
  88. => !string.IsNullOrEmpty(user.Password);
  89. public Task ChangePassword(User user, string newPassword)
  90. {
  91. ConvertPasswordFormat(user);
  92. // This is needed to support changing a no password user to a password user
  93. if (string.IsNullOrEmpty(user.Password))
  94. {
  95. PasswordHash newPasswordHash = new PasswordHash(_cryptographyProvider);
  96. newPasswordHash.Salt = _cryptographyProvider.GenerateSalt();
  97. newPasswordHash.Id = _cryptographyProvider.DefaultHashMethod;
  98. newPasswordHash.Hash = GetHashedChangeAuth(newPassword, newPasswordHash);
  99. user.Password = newPasswordHash.ToString();
  100. return Task.CompletedTask;
  101. }
  102. PasswordHash passwordHash = new PasswordHash(user.Password);
  103. if (passwordHash.Id == "SHA1"
  104. && passwordHash.Salt.Length == 0)
  105. {
  106. passwordHash.Salt = _cryptographyProvider.GenerateSalt();
  107. passwordHash.Id = _cryptographyProvider.DefaultHashMethod;
  108. passwordHash.Hash = GetHashedChangeAuth(newPassword, passwordHash);
  109. }
  110. else if (newPassword != null)
  111. {
  112. passwordHash.Hash = GetHashed(user, newPassword);
  113. }
  114. user.Password = passwordHash.ToString();
  115. return Task.CompletedTask;
  116. }
  117. public void ChangeEasyPassword(User user, string newPassword, string newPasswordHash)
  118. {
  119. ConvertPasswordFormat(user);
  120. if (newPassword != null)
  121. {
  122. newPasswordHash = string.Format("$SHA1${0}", GetHashedString(user, newPassword));
  123. }
  124. if (string.IsNullOrWhiteSpace(newPasswordHash))
  125. {
  126. throw new ArgumentNullException(nameof(newPasswordHash));
  127. }
  128. user.EasyPassword = newPasswordHash;
  129. }
  130. public string GetEasyPasswordHash(User user)
  131. {
  132. // This should be removed in the future. This was added to let user login after
  133. // Jellyfin 10.3.3 failed to save a well formatted PIN.
  134. ConvertPasswordFormat(user);
  135. return string.IsNullOrEmpty(user.EasyPassword)
  136. ? null
  137. : PasswordHash.ConvertToByteString(new PasswordHash(user.EasyPassword).Hash);
  138. }
  139. internal byte[] GetHashedChangeAuth(string newPassword, PasswordHash passwordHash)
  140. {
  141. passwordHash.Hash = Encoding.UTF8.GetBytes(newPassword);
  142. return _cryptographyProvider.ComputeHash(passwordHash);
  143. }
  144. /// <summary>
  145. /// Gets the hashed string.
  146. /// </summary>
  147. public string GetHashedString(User user, string str)
  148. {
  149. PasswordHash passwordHash;
  150. if (string.IsNullOrEmpty(user.Password))
  151. {
  152. passwordHash = new PasswordHash(_cryptographyProvider);
  153. }
  154. else
  155. {
  156. ConvertPasswordFormat(user);
  157. passwordHash = new PasswordHash(user.Password);
  158. }
  159. if (passwordHash.Salt != null)
  160. {
  161. // the password is modern format with PBKDF and we should take advantage of that
  162. passwordHash.Hash = Encoding.UTF8.GetBytes(str);
  163. return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash));
  164. }
  165. else
  166. {
  167. // the password has no salt and should be called with the older method for safety
  168. return PasswordHash.ConvertToByteString(_cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str)));
  169. }
  170. }
  171. public byte[] GetHashed(User user, string str)
  172. {
  173. PasswordHash passwordHash;
  174. if (string.IsNullOrEmpty(user.Password))
  175. {
  176. passwordHash = new PasswordHash(_cryptographyProvider);
  177. }
  178. else
  179. {
  180. ConvertPasswordFormat(user);
  181. passwordHash = new PasswordHash(user.Password);
  182. }
  183. if (passwordHash.Salt != null)
  184. {
  185. // the password is modern format with PBKDF and we should take advantage of that
  186. passwordHash.Hash = Encoding.UTF8.GetBytes(str);
  187. return _cryptographyProvider.ComputeHash(passwordHash);
  188. }
  189. else
  190. {
  191. // the password has no salt and should be called with the older method for safety
  192. return _cryptographyProvider.ComputeHash(passwordHash.Id, Encoding.UTF8.GetBytes(str));
  193. }
  194. }
  195. }
  196. }