DefaultAuthenticationProvider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  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. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  20. {
  21. throw new NotImplementedException();
  22. }
  23. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
  24. {
  25. if (resolvedUser == null)
  26. {
  27. throw new Exception("Invalid username or password");
  28. }
  29. var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase);
  30. if (!success)
  31. {
  32. throw new Exception("Invalid username or password");
  33. }
  34. return Task.FromResult(new ProviderAuthenticationResult
  35. {
  36. Username = username
  37. });
  38. }
  39. public Task<bool> HasPassword(User user)
  40. {
  41. var hasConfiguredPassword = !IsPasswordEmpty(user, GetPasswordHash(user));
  42. return Task.FromResult(hasConfiguredPassword);
  43. }
  44. private bool IsPasswordEmpty(User user, string passwordHash)
  45. {
  46. return string.Equals(passwordHash, GetEmptyHashedString(user), StringComparison.OrdinalIgnoreCase);
  47. }
  48. public Task ChangePassword(User user, string newPassword)
  49. {
  50. string newPasswordHash = null;
  51. if (newPassword != null)
  52. {
  53. newPasswordHash = GetHashedString(user, newPassword);
  54. }
  55. if (string.IsNullOrWhiteSpace(newPasswordHash))
  56. {
  57. throw new ArgumentNullException(nameof(newPasswordHash));
  58. }
  59. user.Password = newPasswordHash;
  60. return Task.CompletedTask;
  61. }
  62. public string GetPasswordHash(User user)
  63. {
  64. return string.IsNullOrEmpty(user.Password)
  65. ? GetEmptyHashedString(user)
  66. : user.Password;
  67. }
  68. public string GetEmptyHashedString(User user)
  69. {
  70. return GetHashedString(user, string.Empty);
  71. }
  72. /// <summary>
  73. /// Gets the hashed string.
  74. /// </summary>
  75. public string GetHashedString(User user, string str)
  76. {
  77. var salt = user.Salt;
  78. if (salt != null)
  79. {
  80. // return BCrypt.HashPassword(str, salt);
  81. }
  82. // legacy
  83. return BitConverter.ToString(_cryptographyProvider.ComputeSHA1(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty);
  84. }
  85. }
  86. }