Skip to content

Commit

Permalink
Move AuthenticationMethod IDisposable implementation declaration from…
Browse files Browse the repository at this point in the history
… inheritees to parent (#746)

* Move IDisposable implementation declaration from inheritees to parent AuthenticationMethod

* Move common Dispose code to AuthenticationMethod class

* Remove unnecessary finalizers

* just move the definition

---------

Co-authored-by: Michał Drzymała <[email protected]>
Co-authored-by: Rob Hague <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2025
1 parent 7e71bb4 commit 5c29394
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 74 deletions.
20 changes: 19 additions & 1 deletion src/Renci.SshNet/AuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Renci.SshNet
/// <summary>
/// Base class for all supported authentication methods.
/// </summary>
public abstract class AuthenticationMethod : IAuthenticationMethod
public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
{
/// <summary>
/// Gets the name of the authentication method.
Expand Down Expand Up @@ -61,5 +61,23 @@ AuthenticationResult IAuthenticationMethod.Authenticate(ISession session)
{
return Authenticate((Session)session);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources;
/// <see langword="false"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
20 changes: 5 additions & 15 deletions src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform keyboard interactive authentication.
/// </summary>
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod
{
private readonly RequestMessageKeyboardInteractive _requestMessage;
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
Expand Down Expand Up @@ -151,20 +151,8 @@ private void Session_UserAuthenticationInformationRequestReceived(object sender,
});
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
Expand All @@ -182,6 +170,8 @@ protected virtual void Dispose(bool disposing)

_isDisposed = true;
}

base.Dispose(disposing);
}
}
}
7 changes: 2 additions & 5 deletions src/Renci.SshNet/KeyboardInteractiveConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,9 @@ protected virtual void Dispose(bool disposing)
{
if (AuthenticationMethods != null)
{
foreach (var authenticationMethods in AuthenticationMethods)
foreach (var authenticationMethod in AuthenticationMethods)
{
if (authenticationMethods is IDisposable disposable)
{
disposable.Dispose();
}
authenticationMethod.Dispose();
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/Renci.SshNet/NoneAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality for "none" authentication method.
/// </summary>
public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
public class NoneAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false);
Expand Down Expand Up @@ -87,20 +87,8 @@ private void Session_UserAuthenticationFailureReceived(object sender, MessageEve
_ = _authenticationCompleted.Set();
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
Expand All @@ -118,6 +106,8 @@ protected virtual void Dispose(bool disposing)

_isDisposed = true;
}

base.Dispose(disposing);
}
}
}
20 changes: 5 additions & 15 deletions src/Renci.SshNet/PasswordAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform password authentication.
/// </summary>
public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
public class PasswordAuthenticationMethod : AuthenticationMethod
{
private readonly RequestMessagePassword _requestMessage;
private readonly byte[] _password;
Expand Down Expand Up @@ -163,20 +163,8 @@ private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sen
});
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
Expand All @@ -194,6 +182,8 @@ protected virtual void Dispose(bool disposing)

_isDisposed = true;
}

base.Dispose(disposing);
}
}
}
5 changes: 1 addition & 4 deletions src/Renci.SshNet/PasswordConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ protected virtual void Dispose(bool disposing)
{
foreach (var authenticationMethod in AuthenticationMethods)
{
if (authenticationMethod is IDisposable disposable)
{
disposable.Dispose();
}
authenticationMethod.Dispose();
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Renci.SshNet
/// <summary>
/// Provides functionality to perform private key authentication.
/// </summary>
public class PrivateKeyAuthenticationMethod : AuthenticationMethod, IDisposable
public class PrivateKeyAuthenticationMethod : AuthenticationMethod
{
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
private EventWaitHandle _authenticationCompleted = new ManualResetEvent(initialState: false);
Expand Down Expand Up @@ -155,20 +155,8 @@ private void Session_UserAuthenticationPublicKeyReceived(object sender, MessageE
_ = _authenticationCompleted.Set();
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
Expand All @@ -186,6 +174,8 @@ protected virtual void Dispose(bool disposing)

_isDisposed = true;
}

base.Dispose(disposing);
}

private sealed class SignatureData : SshData
Expand Down
5 changes: 1 addition & 4 deletions src/Renci.SshNet/PrivateKeyConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ protected virtual void Dispose(bool disposing)
{
foreach (var authenticationMethod in AuthenticationMethods)
{
if (authenticationMethod is IDisposable disposable)
{
disposable.Dispose();
}
authenticationMethod.Dispose();
}
}

Expand Down

0 comments on commit 5c29394

Please sign in to comment.