DefaultAuthenticationProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #nullable enable
  2. using System;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Data.Entities;
  7. using MediaBrowser.Common.Cryptography;
  8. using MediaBrowser.Controller.Authentication;
  9. using MediaBrowser.Model.Cryptography;
  10. namespace Jellyfin.Server.Implementations.Users
  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 AuthenticationException("Specified user does not exist.");
  45. }
  46. bool success = false;
  47. // As long as jellyfin supports password-less 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. // Handle the case when the stored password is null, but the user tried to login with a password
  56. if (resolvedUser.Password != null)
  57. {
  58. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  59. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  60. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  61. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  62. {
  63. byte[] calculatedHash = _cryptographyProvider.ComputeHash(
  64. readyHash.Id,
  65. passwordBytes,
  66. readyHash.Salt.ToArray());
  67. if (readyHash.Hash.SequenceEqual(calculatedHash))
  68. {
  69. success = true;
  70. }
  71. }
  72. else
  73. {
  74. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  75. }
  76. }
  77. if (!success)
  78. {
  79. throw new AuthenticationException("Invalid username or password");
  80. }
  81. return Task.FromResult(new ProviderAuthenticationResult
  82. {
  83. Username = username
  84. });
  85. }
  86. /// <inheritdoc />
  87. public bool HasPassword(User user)
  88. => !string.IsNullOrEmpty(user?.Password);
  89. /// <inheritdoc />
  90. public Task ChangePassword(User user, string newPassword)
  91. {
  92. if (string.IsNullOrEmpty(newPassword))
  93. {
  94. user.Password = null;
  95. return Task.CompletedTask;
  96. }
  97. PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
  98. user.Password = newPasswordHash.ToString();
  99. return Task.CompletedTask;
  100. }
  101. }
  102. }