From 57063f36046fd24e80dc334f35f70a1c8cf3538b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Tue, 15 Dec 2015 19:08:17 -0600 Subject: [PATCH] SSH2: add isAuthenticated() method --- phpseclib/Net/SSH2.php | 11 ++++++++++ tests/Functional/Net/SSH2Test.php | 35 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index b393eb72..d3d340ce 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -2964,6 +2964,17 @@ class Net_SSH2 return (bool) ($this->bitmap & NET_SSH2_MASK_CONNECTED); } + /** + * Have you successfully been logged in? + * + * @return bool + * @access public + */ + function isAuthenticated() + { + return (bool) ($this->bitmap & NET_SSH2_MASK_LOGIN); + } + /** * Gets Binary Packets * diff --git a/tests/Functional/Net/SSH2Test.php b/tests/Functional/Net/SSH2Test.php index c8917de5..968df276 100644 --- a/tests/Functional/Net/SSH2Test.php +++ b/tests/Functional/Net/SSH2Test.php @@ -32,6 +32,11 @@ class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase 'Failed asserting that SSH2 is not connected after construction.' ); + $this->assertFalse( + $ssh->isAuthenticated(), + 'Failed asserting that SSH2 is not authenticated after construction.' + ); + $this->assertNotEmpty( $ssh->getServerPublicHostKey(), 'Failed asserting that a non-empty public host key was fetched.' @@ -53,6 +58,31 @@ class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testPreLogin */ + public function testBadPassword($ssh) + { + $username = $this->getEnv('SSH_USERNAME'); + $password = $this->getEnv('SSH_PASSWORD'); + $this->assertFalse( + $ssh->login($username, 'zzz' . $password), + 'SSH2 login using password succeeded.' + ); + + $this->assertTrue( + $ssh->isConnected(), + 'Failed asserting that SSH2 is connected after bad login attempt.' + ); + + $this->assertFalse( + $ssh->isAuthenticated(), + 'Failed asserting that SSH2 is not authenticated after bad login attempt.' + ); + + return $ssh; + } + + /** + * @depends testBadPassword + */ public function testPasswordLogin($ssh) { $username = $this->getEnv('SSH_USERNAME'); @@ -62,6 +92,11 @@ class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase 'SSH2 login using password failed.' ); + $this->assertTrue( + $ssh->isAuthenticated(), + 'Failed asserting that SSH2 is authenticated after good login attempt.' + ); + return $ssh; }