< Summary

Information
Class: Renci.SshNet.PasswordAuthenticationMethod
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\PasswordAuthenticationMethod.cs
Line coverage
69%
Covered lines: 68
Uncovered lines: 30
Coverable lines: 98
Total lines: 213
Line coverage: 69.3%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)50%280%
get_Name()100%1100%
get_Password()100%1100%
.ctor(...)100%1100%
Authenticate(...)50%480%
Session_UserAuthenticationSuccessReceived(...)100%1100%
Session_UserAuthenticationFailureReceived(...)100%2100%
Session_UserAuthenticationPasswordChangeRequiredReceived(...)100%10%
Dispose()100%1100%
Dispose(...)83.33%686.66%
Finalize()100%1100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\PasswordAuthenticationMethod.cs

#LineLine coverage
 1using System;
 2using System.Runtime.ExceptionServices;
 3using System.Text;
 4using System.Threading;
 5
 6using Renci.SshNet.Abstractions;
 7using Renci.SshNet.Common;
 8using Renci.SshNet.Messages;
 9using Renci.SshNet.Messages.Authentication;
 10
 11namespace Renci.SshNet
 12{
 13    /// <summary>
 14    /// Provides functionality to perform password authentication.
 15    /// </summary>
 16    public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
 17    {
 18        private readonly RequestMessage _requestMessage;
 19        private readonly byte[] _password;
 149020        private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
 21        private Session _session;
 149022        private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false);
 23        private Exception _exception;
 24        private bool _isDisposed;
 25
 26        /// <summary>
 27        /// Gets the name of the authentication method.
 28        /// </summary>
 29        public override string Name
 30        {
 160831            get { return _requestMessage.MethodName; }
 32        }
 33
 34        /// <summary>
 35        /// Gets the password as a sequence of bytes.
 36        /// </summary>
 37        /// <value>
 38        /// The password as a sequence of bytes.
 39        /// </value>
 40        internal byte[] Password
 41        {
 1842            get { return _password; }
 43        }
 44
 45        /// <summary>
 46        /// Occurs when user's password has expired and needs to be changed.
 47        /// </summary>
 48        public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired;
 49
 50        /// <summary>
 51        /// Initializes a new instance of the <see cref="PasswordAuthenticationMethod"/> class.
 52        /// </summary>
 53        /// <param name="username">The username.</param>
 54        /// <param name="password">The password.</param>
 55        /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex
 56        /// <exception cref="ArgumentException"><paramref name="password"/> is <see langword="null"/>.</exception>
 57        public PasswordAuthenticationMethod(string username, string password)
 125358            : this(username, Encoding.UTF8.GetBytes(password))
 124459        {
 124460        }
 61
 62        /// <summary>
 63        /// Initializes a new instance of the <see cref="PasswordAuthenticationMethod"/> class.
 64        /// </summary>
 65        /// <param name="username">The username.</param>
 66        /// <param name="password">The password.</param>
 67        /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex
 68        /// <exception cref="ArgumentException"><paramref name="password"/> is <see langword="null"/>.</exception>
 69        public PasswordAuthenticationMethod(string username, byte[] password)
 149070            : base(username)
 147871        {
 147872            if (password is null)
 073            {
 074                throw new ArgumentNullException(nameof(password));
 75            }
 76
 147877            _password = password;
 147878            _requestMessage = new RequestMessagePassword(ServiceName.Connection, Username, _password);
 147879        }
 80
 81        /// <summary>
 82        /// Authenticates the specified session.
 83        /// </summary>
 84        /// <param name="session">The session to authenticate.</param>
 85        /// <returns>
 86        /// Result of authentication  process.
 87        /// </returns>
 88        /// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null"/>.</exception>
 89        public override AuthenticationResult Authenticate(Session session)
 52890        {
 52891            if (session is null)
 092            {
 093                throw new ArgumentNullException(nameof(session));
 94            }
 95
 52896            _session = session;
 97
 52898            session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
 52899            session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;
 528100            session.UserAuthenticationPasswordChangeRequiredReceived += Session_UserAuthenticationPasswordChangeRequired
 101
 102            try
 528103            {
 528104                session.RegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
 528105                session.SendMessage(_requestMessage);
 528106                session.WaitOnHandle(_authenticationCompleted);
 528107            }
 108            finally
 528109            {
 528110                session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
 528111                session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived;
 528112                session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived;
 528113                session.UserAuthenticationPasswordChangeRequiredReceived -= Session_UserAuthenticationPasswordChangeRequ
 528114            }
 115
 528116            if (_exception != null)
 0117            {
 0118                ExceptionDispatchInfo.Capture(_exception).Throw();
 0119            }
 120
 528121            return _authenticationResult;
 528122        }
 123
 124        private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e)
 514125        {
 514126            _authenticationResult = AuthenticationResult.Success;
 127
 514128            _ = _authenticationCompleted.Set();
 514129        }
 130
 131        private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
 14132        {
 14133            if (e.Message.PartialSuccess)
 12134            {
 12135                _authenticationResult = AuthenticationResult.PartialSuccess;
 12136            }
 137            else
 2138            {
 2139                _authenticationResult = AuthenticationResult.Failure;
 2140            }
 141
 142            // Copy allowed authentication methods
 14143            AllowedAuthentications = e.Message.AllowedAuthentications;
 144
 14145            _ = _authenticationCompleted.Set();
 14146        }
 147
 148        private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sender, MessageEventArgs<PasswordCh
 0149        {
 0150            _session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
 151
 0152            ThreadAbstraction.ExecuteThread(() =>
 0153            {
 0154                try
 0155                {
 0156                    var eventArgs = new AuthenticationPasswordChangeEventArgs(Username);
 0157
 0158                    // Raise an event to allow user to supply a new password
 0159                    PasswordExpired?.Invoke(this, eventArgs);
 0160
 0161                    // Send new authentication request with new password
 0162                    _session.SendMessage(new RequestMessagePassword(ServiceName.Connection, Username, _password, eventAr
 0163                }
 0164                catch (Exception exp)
 0165                {
 0166                    _exception = exp;
 0167                    _ = _authenticationCompleted.Set();
 0168                }
 0169            });
 0170        }
 171
 172        /// <summary>
 173        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 174        /// </summary>
 175        public void Dispose()
 174176        {
 174177            Dispose(disposing: true);
 174178            GC.SuppressFinalize(this);
 174179        }
 180
 181        /// <summary>
 182        /// Releases unmanaged and - optionally - managed resources.
 183        /// </summary>
 184        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor
 185        protected virtual void Dispose(bool disposing)
 1489186        {
 1489187            if (_isDisposed)
 0188            {
 0189                return;
 190            }
 191
 1489192            if (disposing)
 174193            {
 174194                var authenticationCompleted = _authenticationCompleted;
 174195                if (authenticationCompleted != null)
 174196                {
 174197                    authenticationCompleted.Dispose();
 174198                    _authenticationCompleted = null;
 174199                }
 200
 174201                _isDisposed = true;
 174202            }
 1489203        }
 204
 205        /// <summary>
 206        /// Finalizes an instance of the <see cref="PasswordAuthenticationMethod"/> class.
 207        /// </summary>
 208        ~PasswordAuthenticationMethod()
 2630209        {
 1315210            Dispose(disposing: false);
 2630211        }
 212    }
 213}