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