< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Users.DefaultPasswordResetProvider
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DefaultPasswordResetProvider.cs
Line coverage
6%
Covered lines: 4
Uncovered lines: 54
Coverable lines: 58
Total lines: 139
Line coverage: 6.8%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 1/23/2026 - 12:11:06 AM Line coverage: 66.6% (4/6) Total lines: 1394/19/2026 - 12:14:27 AM Line coverage: 6.8% (4/58) Branch coverage: 0% (0/14) Total lines: 139 4/19/2026 - 12:14:27 AM Line coverage: 6.8% (4/58) Branch coverage: 0% (0/14) Total lines: 139

Coverage delta

Coverage delta 60 -60

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
get_IsEnabled()100%210%
RedeemPasswordResetPin()0%156120%
StartForgotPasswordProcess()0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DefaultPasswordResetProvider.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.IO;
 5using System.Security.Cryptography;
 6using System.Text.Json;
 7using System.Threading.Tasks;
 8using Jellyfin.Database.Implementations.Entities;
 9using MediaBrowser.Common;
 10using MediaBrowser.Common.Extensions;
 11using MediaBrowser.Controller.Authentication;
 12using MediaBrowser.Controller.Configuration;
 13using MediaBrowser.Controller.Library;
 14using MediaBrowser.Model.IO;
 15using MediaBrowser.Model.Users;
 16
 17namespace Jellyfin.Server.Implementations.Users
 18{
 19    /// <summary>
 20    /// The default password reset provider.
 21    /// </summary>
 22    public class DefaultPasswordResetProvider : IPasswordResetProvider
 23    {
 24        private const string BaseResetFileName = "passwordreset";
 25
 26        private readonly IApplicationHost _appHost;
 27
 28        private readonly string _passwordResetFileBase;
 29        private readonly string _passwordResetFileBaseDir;
 30
 31        /// <summary>
 32        /// Initializes a new instance of the <see cref="DefaultPasswordResetProvider"/> class.
 33        /// </summary>
 34        /// <param name="configurationManager">The configuration manager.</param>
 35        /// <param name="appHost">The application host.</param>
 36        public DefaultPasswordResetProvider(IServerConfigurationManager configurationManager, IApplicationHost appHost)
 37        {
 2138            _passwordResetFileBaseDir = configurationManager.ApplicationPaths.ProgramDataPath;
 2139            _passwordResetFileBase = Path.Combine(_passwordResetFileBaseDir, BaseResetFileName);
 2140            _appHost = appHost;
 41            // TODO: Remove the circular dependency on UserManager
 2142        }
 43
 44        /// <inheritdoc />
 045        public string Name => "Default Password Reset Provider";
 46
 47        /// <inheritdoc />
 048        public bool IsEnabled => true;
 49
 50        /// <inheritdoc />
 51        public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
 52        {
 053            var userManager = _appHost.Resolve<IUserManager>();
 054            var usersReset = new List<string>();
 055            foreach (var resetFile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{BaseResetFileName}*"))
 56            {
 57                SerializablePasswordReset spr;
 058                var str = AsyncFile.OpenRead(resetFile);
 059                await using (str.ConfigureAwait(false))
 60                {
 061                    spr = await JsonSerializer.DeserializeAsync<SerializablePasswordReset>(str).ConfigureAwait(false)
 062                        ?? throw new ResourceNotFoundException($"Provided path ({resetFile}) is not valid.");
 63                }
 64
 065                if (spr.ExpirationDate < DateTime.UtcNow)
 66                {
 067                    File.Delete(resetFile);
 68                }
 069                else if (string.Equals(
 070                    spr.Pin.Replace("-", string.Empty, StringComparison.Ordinal),
 071                    pin.Replace("-", string.Empty, StringComparison.Ordinal),
 072                    StringComparison.Ordinal))
 73                {
 074                    var resetUser = userManager.GetUserByName(spr.UserName)
 075                        ?? throw new ResourceNotFoundException($"User with a username of {spr.UserName} not found");
 76
 077                    await userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
 078                    usersReset.Add(resetUser.Username);
 079                    File.Delete(resetFile);
 080                }
 081            }
 82
 083            if (usersReset.Count < 1)
 84            {
 085                throw new ResourceNotFoundException($"No Users found with a password reset request matching pin {pin}");
 86            }
 87
 088            return new PinRedeemResult
 089            {
 090                Success = true,
 091                UsersReset = usersReset.ToArray()
 092            };
 093        }
 94
 95        /// <inheritdoc />
 96        public async Task<ForgotPasswordResult> StartForgotPasswordProcess(User? user, string enteredUsername, bool isIn
 97        {
 098            DateTime expireTime = DateTime.UtcNow.AddMinutes(30);
 099            var usernameHash = enteredUsername.ToUpperInvariant().GetMD5().ToString("N", CultureInfo.InvariantCulture);
 0100            var pinFile = _passwordResetFileBase + usernameHash + ".json";
 101
 0102            if (user is not null && isInNetwork)
 103            {
 0104                byte[] bytes = new byte[4];
 0105                RandomNumberGenerator.Fill(bytes);
 0106                string pin = BitConverter.ToString(bytes);
 107
 0108                SerializablePasswordReset spr = new SerializablePasswordReset
 0109                {
 0110                    ExpirationDate = expireTime,
 0111                    Pin = pin,
 0112                    PinFile = pinFile,
 0113                    UserName = user.Username
 0114                };
 115
 0116                FileStream fileStream = AsyncFile.Create(pinFile);
 0117                await using (fileStream.ConfigureAwait(false))
 118                {
 0119                    await JsonSerializer.SerializeAsync(fileStream, spr).ConfigureAwait(false);
 120                }
 121            }
 122
 0123            return new ForgotPasswordResult
 0124            {
 0125                Action = ForgotPasswordAction.PinCode,
 0126                PinExpirationDate = expireTime,
 0127                PinFile = pinFile
 0128            };
 0129        }
 130
 131#nullable disable
 132        private class SerializablePasswordReset : PasswordPinCreationResult
 133        {
 134            public string Pin { get; set; }
 135
 136            public string UserName { get; set; }
 137        }
 138    }
 139}