AuthenticationMethodFactory.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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("resources.client.id_rsa");
  13. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
  14. }
  15. public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod()
  16. {
  17. var privateKeyFile1 = GetPrivateKey("resources.client.id_rsa");
  18. var privateKeyFile2 = GetPrivateKey("resources.client.id_rsa");
  19. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2);
  20. }
  21. public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod()
  22. {
  23. var privateKeyFile = GetPrivateKey("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("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("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 = TestBase.GetData(resourceName))
  59. {
  60. return new PrivateKeyFile(stream, passPhrase);
  61. }
  62. }
  63. }
  64. }