DefaultAuthenticationProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Data.Entities;
  6. using MediaBrowser.Common.Cryptography;
  7. using MediaBrowser.Controller.Authentication;
  8. using MediaBrowser.Model.Cryptography;
  9. namespace Jellyfin.Server.Implementations.Users
  10. {
  11. /// <summary>
  12. /// The default authentication provider.
  13. /// </summary>
  14. public class DefaultAuthenticationProvider : IAuthenticationProvider, IRequiresResolvedUser
  15. {
  16. private readonly ICryptoProvider _cryptographyProvider;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
  19. /// </summary>
  20. /// <param name="cryptographyProvider">The cryptography provider.</param>
  21. public DefaultAuthenticationProvider(ICryptoProvider cryptographyProvider)
  22. {
  23. _cryptographyProvider = cryptographyProvider;
  24. }
  25. /// <inheritdoc />
  26. public string Name => "Default";
  27. /// <inheritdoc />
  28. public bool IsEnabled => true;
  29. /// <inheritdoc />
  30. // This is dumb and an artifact of the backwards way auth providers were designed.
  31. // This version of authenticate was never meant to be called, but needs to be here for interface compat
  32. // Only the providers that don't provide local user support use this
  33. public Task<ProviderAuthenticationResult> Authenticate(string username, string password)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. /// <inheritdoc />
  38. // This is the version that we need to use for local users. Because reasons.
  39. public Task<ProviderAuthenticationResult> Authenticate(string username, string password, User resolvedUser)
  40. {
  41. if (resolvedUser == null)
  42. {
  43. throw new AuthenticationException("Specified user does not exist.");
  44. }
  45. bool success = false;
  46. // As long as jellyfin supports password-less users, we need this little block here to accommodate
  47. if (!HasPassword(resolvedUser) && string.IsNullOrEmpty(password))
  48. {
  49. return Task.FromResult(new ProviderAuthenticationResult
  50. {
  51. Username = username
  52. });
  53. }
  54. // Handle the case when the stored password is null, but the user tried to login with a password
  55. if (resolvedUser.Password != null)
  56. {
  57. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  58. PasswordHash readyHash = PasswordHash.Parse(resolvedUser.Password);
  59. if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id)
  60. || _cryptographyProvider.DefaultHashMethod == readyHash.Id)
  61. {
  62. byte[] calculatedHash = _cryptographyProvider.ComputeHash(
  63. readyHash.Id,
  64. passwordBytes,
  65. readyHash.Salt.ToArray());
  66. if (readyHash.Hash.SequenceEqual(calculatedHash))
  67. {
  68. success = true;
  69. }
  70. }
  71. else
  72. {
  73. throw new AuthenticationException($"Requested crypto method not available in provider: {readyHash.Id}");
  74. }
  75. }
  76. if (!success)
  77. {
  78. throw new AuthenticationException("Invalid username or password");
  79. }
  80. return Task.FromResult(new ProviderAuthenticationResult
  81. {
  82. Username = username
  83. });
  84. }
  85. /// <inheritdoc />
  86. public bool HasPassword(User user)
  87. => !string.IsNullOrEmpty(user?.Password);
  88. /// <inheritdoc />
  89. public Task ChangePassword(User user, string newPassword)
  90. {
  91. if (string.IsNullOrEmpty(newPassword))
  92. {
  93. user.Password = null;
  94. return Task.CompletedTask;
  95. }
  96. PasswordHash newPasswordHash = _cryptographyProvider.CreatePasswordHash(newPassword);
  97. user.Password = newPasswordHash.ToString();
  98. return Task.CompletedTask;
  99. }
  100. }
  101. }