Skip to content

Commit

Permalink
ssh: Expand ssh:connection_info/2 with user_auth
Browse files Browse the repository at this point in the history
user_auth is the authentication method used for
user authentication.
  • Loading branch information
alexandrejbr committed Dec 9, 2024
1 parent 056acac commit 8a96161
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/ssh/src/ssh.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,8 @@ in the User's Guide chapter.
userauth_preference,
available_host_keys,
pwdfun_user_state,
authenticated = false
authenticated = false,
last_userauth_tried = "none" %% Not used for server role
}).

-record(alg,
Expand Down
6 changes: 4 additions & 2 deletions lib/ssh/src/ssh_connection_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,8 @@ conn_info_keys() ->
sockname,
options,
algorithms,
channels
channels,
user_auth
].

conn_info(client_version, #data{ssh_params=S}) -> {S#ssh.c_vsn, S#ssh.c_version};
Expand All @@ -1876,7 +1877,8 @@ conn_info(socket, D) -> D#data.socket;
conn_info(chan_ids, D) ->
ssh_client_channel:cache_foldl(fun(#channel{local_id=Id}, Acc) ->
[Id | Acc]
end, [], cache(D)).
end, [], cache(D));
conn_info(user_auth, #data{ssh_params=#ssh{last_userauth_tried=UserAuth}}) -> UserAuth.

conn_info_chans(Chs) ->
Fs = record_info(fields, channel),
Expand Down
6 changes: 4 additions & 2 deletions lib/ssh/src/ssh_fsm_userauth_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ handle_event(internal, #ssh_msg_userauth_failure{authentications = Methods}, Sta
io_lib:format("User auth failed for: ~p",[D0#data.auth_user]),
StateName, D0#data{ssh_params = Ssh}),
{stop, Shutdown, D};
{"keyboard-interactive", {Msg, Ssh}} ->
{"keyboard-interactive", {Msg, Ssh2}} ->
Ssh = Ssh2#ssh{last_userauth_tried = "keyboard-interactive"},
D = ssh_connection_handler:send_msg(Msg, D0#data{ssh_params = Ssh}),
{next_state, {userauth_keyboard_interactive,client}, D};
{_Method, {Msg, Ssh}} ->
{Method, {Msg, Ssh2}} ->
Ssh = Ssh2#ssh{last_userauth_tried = Method},
D = ssh_connection_handler:send_msg(Msg, D0#data{ssh_params = Ssh}),
{keep_state, D}
end;
Expand Down
9 changes: 8 additions & 1 deletion lib/ssh/test/ssh_options_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ auth_none(Config) ->
{password, "wrong-pwd"},
{user_dir, UserDir},
{user_interaction, false}]),
[{user_auth, "none"}] =
ssh:connection_info(ClientConnRef1, [user_auth]),
"some-other-user" =
proplists:get_value(user, ssh:connection_info(ClientConnRef1, [user])),
ok = ssh:close(ClientConnRef1),
Expand Down Expand Up @@ -677,12 +679,14 @@ user_dir_fun_option(Config) ->
UserDir
end},
{failfun, fun ssh_test_lib:failfun/2}]),
_ConnectionRef =
ConnectionRef =
ssh_test_lib:connect(Host, Port, [{silently_accept_hosts, true},
{user, "foo"},
{user_dir, UserDir},
{auth_methods,"publickey"},
{user_interaction, false}]),
[{user_auth, "publickey"}] =
ssh:connection_info(ConnectionRef, [user_auth]),
receive
{user,Ref,"foo"} ->
ssh:stop_daemon(Pid),
Expand Down Expand Up @@ -802,6 +806,9 @@ connectfun4_server(Config) ->
{password, "morot"},
{user_dir, UserDir},
{user_interaction, false}]),
[{user_auth, "keyboard-interactive"}] =
ssh:connection_info(ConnectionRef, [user_auth]),

receive
{connect,Ref,User,Method,ConnInfo} ->
"foo" = User,
Expand Down

0 comments on commit 8a96161

Please sign in to comment.