2
0

KeyboardInteractiveConnectionInfo.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Linq;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet
  5. {
  6. /// <summary>
  7. /// Provides connection information when keyboard interactive authentication method is used
  8. /// </summary>
  9. /// <example>
  10. /// <code source="..\..\Renci.SshNet.Tests\Classes\KeyboardInteractiveConnectionInfoTest.cs" region="Example KeyboardInteractiveConnectionInfo AuthenticationPrompt" language="C#" title="Connect using interactive method" />
  11. /// </example>
  12. public class KeyboardInteractiveConnectionInfo : ConnectionInfo, IDisposable
  13. {
  14. /// <summary>
  15. /// Occurs when server prompts for more authentication information.
  16. /// </summary>
  17. /// <example>
  18. /// <code source="..\..\Renci.SshNet.Tests\Classes\KeyboardInteractiveConnectionInfoTest.cs" region="Example KeyboardInteractiveConnectionInfo AuthenticationPrompt" language="C#" title="Connect using interactive method" />
  19. /// </example>
  20. public event EventHandler<AuthenticationPromptEventArgs> AuthenticationPrompt;
  21. // TODO: DOCS Add exception documentation for this class.
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  24. /// </summary>
  25. /// <param name="host">The host.</param>
  26. /// <param name="username">The username.</param>
  27. public KeyboardInteractiveConnectionInfo(string host, string username)
  28. : this(host, DefaultPort, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  29. {
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  33. /// </summary>
  34. /// <param name="host">The host.</param>
  35. /// <param name="port">The port.</param>
  36. /// <param name="username">The username.</param>
  37. public KeyboardInteractiveConnectionInfo(string host, int port, string username)
  38. : this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  43. /// </summary>
  44. /// <param name="host">Connection host.</param>
  45. /// <param name="port">Connection port.</param>
  46. /// <param name="username">Connection username.</param>
  47. /// <param name="proxyType">Type of the proxy.</param>
  48. /// <param name="proxyHost">The proxy host.</param>
  49. /// <param name="proxyPort">The proxy port.</param>
  50. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  51. : this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  52. {
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  56. /// </summary>
  57. /// <param name="host">Connection host.</param>
  58. /// <param name="port">Connection port.</param>
  59. /// <param name="username">Connection username.</param>
  60. /// <param name="proxyType">Type of the proxy.</param>
  61. /// <param name="proxyHost">The proxy host.</param>
  62. /// <param name="proxyPort">The proxy port.</param>
  63. /// <param name="proxyUsername">The proxy username.</param>
  64. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  65. : this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  66. {
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  70. /// </summary>
  71. /// <param name="host">Connection host.</param>
  72. /// <param name="username">Connection username.</param>
  73. /// <param name="proxyType">Type of the proxy.</param>
  74. /// <param name="proxyHost">The proxy host.</param>
  75. /// <param name="proxyPort">The proxy port.</param>
  76. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  77. : this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  78. {
  79. }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  82. /// </summary>
  83. /// <param name="host">Connection host.</param>
  84. /// <param name="username">Connection username.</param>
  85. /// <param name="proxyType">Type of the proxy.</param>
  86. /// <param name="proxyHost">The proxy host.</param>
  87. /// <param name="proxyPort">The proxy port.</param>
  88. /// <param name="proxyUsername">The proxy username.</param>
  89. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  90. : this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  91. {
  92. }
  93. /// <summary>
  94. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  95. /// </summary>
  96. /// <param name="host">Connection host.</param>
  97. /// <param name="username">Connection username.</param>
  98. /// <param name="proxyType">Type of the proxy.</param>
  99. /// <param name="proxyHost">The proxy host.</param>
  100. /// <param name="proxyPort">The proxy port.</param>
  101. /// <param name="proxyUsername">The proxy username.</param>
  102. /// <param name="proxyPassword">The proxy password.</param>
  103. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  104. : this(host, DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  105. {
  106. }
  107. /// <summary>
  108. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  109. /// </summary>
  110. /// <param name="host">Connection host.</param>
  111. /// <param name="port">Connection port.</param>
  112. /// <param name="username">Connection username.</param>
  113. /// <param name="proxyType">Type of the proxy.</param>
  114. /// <param name="proxyHost">The proxy host.</param>
  115. /// <param name="proxyPort">The proxy port.</param>
  116. /// <param name="proxyUsername">The proxy username.</param>
  117. /// <param name="proxyPassword">The proxy password.</param>
  118. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  119. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new KeyboardInteractiveAuthenticationMethod(username))
  120. {
  121. foreach (var authenticationMethod in AuthenticationMethods.OfType<KeyboardInteractiveAuthenticationMethod>())
  122. {
  123. authenticationMethod.AuthenticationPrompt += AuthenticationMethod_AuthenticationPrompt;
  124. }
  125. }
  126. private void AuthenticationMethod_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
  127. {
  128. if (AuthenticationPrompt != null)
  129. {
  130. AuthenticationPrompt(sender, e);
  131. }
  132. }
  133. #region IDisposable Members
  134. private bool _isDisposed;
  135. /// <summary>
  136. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  137. /// </summary>
  138. public void Dispose()
  139. {
  140. Dispose(true);
  141. GC.SuppressFinalize(this);
  142. }
  143. /// <summary>
  144. /// Releases unmanaged and - optionally - managed resources
  145. /// </summary>
  146. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  147. protected virtual void Dispose(bool disposing)
  148. {
  149. if (_isDisposed)
  150. return;
  151. if (disposing)
  152. {
  153. if (AuthenticationMethods != null)
  154. {
  155. foreach (var authenticationMethods in AuthenticationMethods.OfType<IDisposable>())
  156. {
  157. authenticationMethods.Dispose();
  158. }
  159. }
  160. _isDisposed = true;
  161. }
  162. }
  163. /// <summary>
  164. /// Releases unmanaged resources and performs other cleanup operations before the
  165. /// <see cref="KeyboardInteractiveConnectionInfo"/> is reclaimed by garbage collection.
  166. /// </summary>
  167. ~KeyboardInteractiveConnectionInfo()
  168. {
  169. Dispose(false);
  170. }
  171. #endregion
  172. }
  173. }