diff --git a/NMSSH/NMSSHSession.m b/NMSSH/NMSSHSession.m index ad115fc..7dec89c 100644 --- a/NMSSH/NMSSHSession.m +++ b/NMSSH/NMSSHSession.m @@ -112,33 +112,33 @@ - (BOOL)authenticateByPublicKey:(NSString *)publicKey // Check what authentication methods are available char *userauthlist = libssh2_userauth_list(session, [username UTF8String], strlen([username UTF8String])); - + if (strstr(userauthlist, "publickey") == NULL) { NSLog(@"NMSSH: Authentication by public key not available for %@", host); return NO; } - + if (password == nil) { password = @""; } - + // Get absolute paths for private/public key pair publicKey = [publicKey stringByExpandingTildeInPath]; - NSString *privateKey = [publicKey stringByReplacingOccurrencesOfString:@".pub" + NSString *privateKey = [publicKey stringByReplacingOccurrencesOfString:@".pub" withString:@""]; - + // Try to authenticate with key pair and password int error = libssh2_userauth_publickey_fromfile(session, [username UTF8String], [publicKey UTF8String], [privateKey UTF8String], [password UTF8String]); - + if (error) { NSLog(@"NMSSH: Public key authentication failed"); return NO; } - + authorized = YES; return [self isAuthorized]; } diff --git a/NMSSHTests/NMSSHSessionTests.m b/NMSSHTests/NMSSHSessionTests.m index 9585645..3073392 100644 --- a/NMSSHTests/NMSSHSessionTests.m +++ b/NMSSHTests/NMSSHSessionTests.m @@ -41,7 +41,7 @@ - (void)testConnectionToValidServerWorks { NSString *host = [validPasswordProtectedServer objectForKey:@"host"]; NSString *username = [validPasswordProtectedServer objectForKey:@"user"]; - + STAssertNoThrow(session = [NMSSHSession connectToHost:host withUsername:username], @"Connecting to a valid server does not throw exception"); @@ -53,7 +53,7 @@ - (void)testConnectionToValidServerWorks { - (void)testConnectionToInvalidServerFails { NSString *host = [invalidServer objectForKey:@"host"]; NSString *username = [invalidServer objectForKey:@"user"]; - + STAssertNoThrow(session = [NMSSHSession connectToHost:host withUsername:username], @"Connecting to a invalid server does not throw exception"); @@ -72,7 +72,7 @@ - (void)testPasswordAuthenticationWithValidPasswordWorks { objectForKey:@"user"]; NSString *password = [validPasswordProtectedServer objectForKey:@"password"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; STAssertNoThrow([session authenticateByPassword:password], @@ -88,7 +88,7 @@ - (void)testPasswordAuthenticationWithInvalidPasswordFails { NSString *username = [validPasswordProtectedServer objectForKey:@"user"]; NSString *password = [invalidServer objectForKey:@"password"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; STAssertNoThrow([session authenticateByPassword:password], @@ -103,13 +103,13 @@ - (void)testPasswordAuthenticationWithInvalidUserFails { NSString *host = [validPasswordProtectedServer objectForKey:@"host"]; NSString *username = [invalidServer objectForKey:@"user"]; NSString *password = [invalidServer objectForKey:@"password"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; - + STAssertNoThrow([session authenticateByPassword:password], @"Authentication with invalid username/password doesn't" @"throw exception"); - + STAssertFalse([session isAuthorized], @"Authentication with invalid username/password should not" @"work"); @@ -122,14 +122,14 @@ - (void)testPublicKeyAuthenticationWithValidKeyWorks { objectForKey:@"valid_public_key"]; NSString *password = [validPublicKeyProtectedServer objectForKey:@"password"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; STAssertNoThrow([session authenticateByPublicKey:publicKey andPassword:password], @"Authentication with valid public key doesn't throw" @"exception"); - + STAssertTrue([session isAuthorized], @"Authentication with valid public key should work"); } @@ -139,14 +139,14 @@ - (void)testPublicKeyAuthenticationWithInvalidPasswordFails { NSString *username = [validPublicKeyProtectedServer objectForKey:@"user"]; NSString *publicKey = [validPublicKeyProtectedServer objectForKey:@"valid_public_key"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; - + STAssertNoThrow([session authenticateByPublicKey:publicKey andPassword:nil], @"Public key authentication with invalid password doesn't" @"throw exception"); - + STAssertFalse([session isAuthorized], @"Public key authentication with invalid password should not" @"work"); @@ -158,14 +158,14 @@ - (void)testPublicKeyAuthenticationWithInvalidKeyFails { NSString *username = [validPublicKeyProtectedServer objectForKey:@"user"]; NSString *publicKey = [validPublicKeyProtectedServer objectForKey:@"invalid_public_key"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; - + STAssertNoThrow([session authenticateByPublicKey:publicKey andPassword:nil], @"Authentication with invalid public key doesn't throw" @"exception"); - + STAssertFalse([session isAuthorized], @"Authentication with invalid public key should not work"); } @@ -177,16 +177,18 @@ - (void)testPublicKeyAuthenticationWithInvalidUserFails { objectForKey:@"valid_public_key"]; NSString *password = [validPublicKeyProtectedServer objectForKey:@"password"]; - + session = [NMSSHSession connectToHost:host withUsername:username]; - + STAssertNoThrow([session authenticateByPublicKey:publicKey andPassword:password], @"Public key authentication with invalid user doesn't" @"throw exception"); - + STAssertFalse([session isAuthorized], @"Public key authentication with invalid user should not work"); } -@end \ No newline at end of file +// TODO: Create tests for SSH agent + +@end