AuthenticationMethodFactory.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. namespace Renci.SshNet.IntegrationTests
  2. {
  3. public class AuthenticationMethodFactory
  4. {
  5. public PasswordAuthenticationMethod CreatePowerUserPasswordAuthenticationMethod()
  6. {
  7. var user = Users.Admin;
  8. return new PasswordAuthenticationMethod(user.UserName, user.Password);
  9. }
  10. public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethod()
  11. {
  12. var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
  13. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
  14. }
  15. public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod()
  16. {
  17. var privateKeyFile1 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
  18. var privateKeyFile2 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
  19. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2);
  20. }
  21. public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod()
  22. {
  23. var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", "tester");
  24. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
  25. }
  26. public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod()
  27. {
  28. var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", null);
  29. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
  30. }
  31. public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey()
  32. {
  33. var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_noaccess.rsa");
  34. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
  35. }
  36. public PasswordAuthenticationMethod CreateRegulatUserPasswordAuthenticationMethod()
  37. {
  38. return new PasswordAuthenticationMethod(Users.Regular.UserName, Users.Regular.Password);
  39. }
  40. public PasswordAuthenticationMethod CreateRegularUserPasswordAuthenticationMethodWithBadPassword()
  41. {
  42. return new PasswordAuthenticationMethod(Users.Regular.UserName, "xxx");
  43. }
  44. public KeyboardInteractiveAuthenticationMethod CreateRegularUserKeyboardInteractiveAuthenticationMethod()
  45. {
  46. var keyboardInteractive = new KeyboardInteractiveAuthenticationMethod(Users.Regular.UserName);
  47. keyboardInteractive.AuthenticationPrompt += (sender, args) =>
  48. {
  49. foreach (var authenticationPrompt in args.Prompts)
  50. {
  51. authenticationPrompt.Response = Users.Regular.Password;
  52. }
  53. };
  54. return keyboardInteractive;
  55. }
  56. private PrivateKeyFile GetPrivateKey(string resourceName, string passPhrase = null)
  57. {
  58. using (var stream = GetResourceStream(resourceName))
  59. {
  60. return new PrivateKeyFile(stream, passPhrase);
  61. }
  62. }
  63. private Stream GetResourceStream(string resourceName)
  64. {
  65. var type = GetType();
  66. var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
  67. if (resourceStream == null)
  68. {
  69. throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
  70. }
  71. return resourceStream;
  72. }
  73. }
  74. }