Skip to content

Commit

Permalink
Add Session#userauth_password
Browse files Browse the repository at this point in the history
  • Loading branch information
rosti committed Feb 21, 2018
1 parent 5a1a2d9 commit 4e3aacd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ext/libssh_ruby/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,23 @@ static VALUE m_userauth_none(VALUE self) {
return INT2FIX(rc);
}

/*
* @overload userauth_password
* Try to authenticate through the "password" method.
* @param [String] password
* @return [Fixnum]
* @see http://api.libssh.org/stable/group__libssh__auth.html ssh_userauth_password
*/
static VALUE m_userauth_password(VALUE self, VALUE password) {
SessionHolder *holder;
int rc;

TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
rc = ssh_userauth_password(holder->session, NULL, StringValueCStr(password));
RAISE_IF_ERROR(rc);
return INT2FIX(rc);
}

/*
* @overload userauth_list
* Get available authentication methods from the server.
Expand Down Expand Up @@ -699,6 +716,8 @@ void Init_libssh_session() {

rb_define_method(rb_cLibSSHSession, "userauth_none",
RUBY_METHOD_FUNC(m_userauth_none), 0);
rb_define_method(rb_cLibSSHSession, "userauth_password",
RUBY_METHOD_FUNC(m_userauth_password), 1);
rb_define_method(rb_cLibSSHSession, "userauth_list",
RUBY_METHOD_FUNC(m_userauth_list), 0);
rb_define_method(rb_cLibSSHSession, "userauth_publickey_auto",
Expand Down
21 changes: 21 additions & 0 deletions spec/integration/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@
end
end

describe '#userauth_password' do
before do
session.host = SshHelper.host
session.port = DockerHelper.port
session.user = SshHelper.user
session.connect
end

context 'wrong password' do
it 'is denied' do
expect(session.userauth_password('12345')).to eq(LibSSH::AUTH_DENIED)
end
end

context 'with valid password' do
it 'access is granted' do
expect(session.userauth_password(SshHelper.password)).to eq(LibSSH::AUTH_SUCCESS)
end
end
end

describe '#server_known' do
before do
session.host = SshHelper.host
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def user
'alice'
end

def password
'alice'
end

def identity_path
File.join(__dir__, 'id_ecdsa')
end
Expand Down

0 comments on commit 4e3aacd

Please sign in to comment.