DefaultAuthenticationProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Authentication;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Model.Cryptography;
  7. namespace Emby.Server.Implementations.Library
  8. {
  9. public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
  10. {
  11. private readonly ICryptoProvider _cryptographyProvider;
  12. public DefaultAuthenticationProvider(ICryptoProvider crypto)
  13. {
  14. _cryptographyProvider = crypto;
  15. }
  16. public string Name => "Default";
  17. public bool IsEnabled => true;
  18. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  19. {
  20. throw new NotImplementedException();
  21. }
  22. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
  23. {
  24. if (resolvedUser == null)
  25. {
  26. throw new Exception("Invalid username or password");
  27. }
  28. var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase);
  29. if (!success)
  30. {
  31. throw new Exception("Invalid username or password");
  32. }
  33. return Task.FromResult(new ProviderAuthenticationResult
  34. {
  35. Username = username
  36. });
  37. }
  38. public Task<bool> HasPassword(User user)
  39. {
  40. var hasConfiguredPassword = !IsPasswordEmpty(user, GetPasswordHash(user));
  41. return Task.FromResult(hasConfiguredPassword);
  42. }
  43. private bool IsPasswordEmpty(User user, string passwordHash)
  44. {
  45. return string.Equals(passwordHash, GetEmptyHashedString(user), StringComparison.OrdinalIgnoreCase);
  46. }
  47. public Task ChangePassword(User user, string newPassword)
  48. {
  49. string newPasswordHash = null;
  50. if (newPassword != null)
  51. {
  52. newPasswordHash = GetHashedString(user, newPassword);
  53. }
  54. if (string.IsNullOrWhiteSpace(newPasswordHash))
  55. {
  56. throw new ArgumentNullException(nameof(newPasswordHash));
  57. }
  58. user.Password = newPasswordHash;
  59. return Task.CompletedTask;
  60. }
  61. public string GetPasswordHash(User user)
  62. {
  63. return string.IsNullOrEmpty(user.Password)
  64. ? GetEmptyHashedString(user)
  65. : user.Password;
  66. }
  67. public string GetEmptyHashedString(User user)
  68. {
  69. return GetHashedString(user, string.Empty);
  70. }
  71. /// <summary>
  72. /// Gets the hashed string.
  73. /// </summary>
  74. public string GetHashedString(User user, string str)
  75. {
  76. var salt = user.Salt;
  77. if (salt != null)
  78. {
  79. // return BCrypt.HashPassword(str, salt);
  80. }
  81. // legacy
  82. return BitConverter.ToString(_cryptographyProvider.ComputeSHA1(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty);
  83. }
  84. }
  85. }