libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
auth.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/config.hpp>
5
8#include <sshpp/export.hpp>
9#include <sshpp/key.hpp>
10#include <sshpp/result.hpp>
11#include <sshpp/types.hpp>
12
13#include <memory>
14#include <string_view>
15#include <vector>
16#include <filesystem>
17
18namespace sshpp {
19
21class SSHPP_API Authenticator {
22public:
23 virtual ~Authenticator() = default;
24 virtual std::string_view name() const noexcept = 0;
26 virtual Result<AuthStatus> attempt(detail::SessionCore&) const noexcept = 0;
27};
28
29namespace auth {
30
32class SSHPP_API None final : public Authenticator {
33public:
34 std::string_view name() const noexcept override { return "none"; }
35 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
36};
37
38class SSHPP_API Password final : public Authenticator {
39public:
40 explicit Password(SecureString password) : password_(std::move(password)) {}
41
42 std::string_view name() const noexcept override { return "password"; }
43 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
44
45private:
46 SecureString password_;
47};
48
50class SSHPP_API PublicKeyAuto final : public Authenticator {
51public:
52 PublicKeyAuto() = default;
53 explicit PublicKeyAuto(SecureString passphrase) : passphrase_(std::move(passphrase)) {}
54
55 std::string_view name() const noexcept override { return "publickey_auto"; }
56 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
57
58private:
59 SecureString passphrase_;
60};
61
63class SSHPP_API PublicKey final : public Authenticator {
64public:
65 explicit PublicKey(Key private_key) : key_(std::move(private_key)) {}
66
67 static Result<PublicKey> from_file(const std::filesystem::path& p, PassphraseCallback cb = {});
68
69 std::string_view name() const noexcept override { return "publickey"; }
70 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
71
72private:
73 Key key_;
74};
75
77class SSHPP_API Agent final : public Authenticator {
78public:
79 std::string_view name() const noexcept override { return "agent"; }
80 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
81};
82
84class SSHPP_API KeyboardInteractive final : public Authenticator {
85public:
86 struct Prompt { std::string text; bool echo; };
87 struct Challenge {
88 std::string name, instruction;
89 std::vector<Prompt> prompts;
90 };
91 using Handler = std::function<Result<std::vector<SecureString>>(const Challenge&)>;
92
93 explicit KeyboardInteractive(Handler h) : handler_(std::move(h)) {}
94
96 static KeyboardInteractive with_password(SecureString password);
97#if SSHPP_WITH_CONSOLE
100 static Handler console_handler();
101#endif
102
103 std::string_view name() const noexcept override { return "keyboard-interactive"; }
104 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
105
106private:
107 Handler handler_;
108};
109
111class SSHPP_API Chain final : public Authenticator {
112public:
113 Chain& add(std::shared_ptr<Authenticator> a) { chain_.push_back(std::move(a)); return *this; }
114
115 template <class A, class... Args>
116 Chain& emplace(Args&&... args) {
117 return add(std::make_shared<A>(std::forward<Args>(args)...));
118 }
119
120#if SSHPP_WITH_CONSOLE
126 static Chain interactive_default(PassphraseCallback passphrase_cb, PasswordCallback password_cb);
127#endif
128
129 std::string_view name() const noexcept override { return "chain"; }
130 Result<AuthStatus> attempt(detail::SessionCore&) const noexcept override;
131
132private:
133 std::vector<std::shared_ptr<Authenticator>> chain_;
134};
135
136#if SSHPP_WITH_CONSOLE
138SSHPP_API Result<SecureString> console_password_prompt(std::string_view prompt = "Password: ");
140SSHPP_API PassphraseCallback console_passphrase_prompt();
142SSHPP_API PasswordCallback console_password_callback();
143#endif
144
145} // namespace auth
146} // namespace sshpp
147
148#if SSHPP_HEADER_ONLY
149#include <sshpp/detail/auth.ipp>
150#endif
Authentication strategy interface. See docs/design/04 §4.4.
Definition auth.hpp:21
virtual std::string_view name() const noexcept=0
virtual ~Authenticator()=default
Definition key.hpp:59
Definition result.hpp:16
Definition types.hpp:132
ssh_userauth_agent.
Definition auth.hpp:77
std::string_view name() const noexcept override
Definition auth.hpp:79
Tries each authenticator in order until one succeeds.
Definition auth.hpp:111
Chain & emplace(Args &&... args)
Definition auth.hpp:116
std::string_view name() const noexcept override
Definition auth.hpp:129
Chain & add(std::shared_ptr< Authenticator > a)
Definition auth.hpp:113
Full keyboard-interactive loop.
Definition auth.hpp:84
std::function< Result< std::vector< SecureString > >(const Challenge &)> Handler
Definition auth.hpp:91
KeyboardInteractive(Handler h)
Definition auth.hpp:93
std::string_view name() const noexcept override
Definition auth.hpp:103
"none" — probes the server; sometimes succeeds outright (passwordless accounts).
Definition auth.hpp:32
std::string_view name() const noexcept override
Definition auth.hpp:34
Definition auth.hpp:38
std::string_view name() const noexcept override
Definition auth.hpp:42
Password(SecureString password)
Definition auth.hpp:40
ssh_userauth_publickey_auto: agent, then default identities, then configured identities.
Definition auth.hpp:50
std::string_view name() const noexcept override
Definition auth.hpp:55
PublicKeyAuto(SecureString passphrase)
Definition auth.hpp:53
Explicit key: try_publickey (offer) then publickey (sign).
Definition auth.hpp:63
PublicKey(Key private_key)
Definition auth.hpp:65
std::string_view name() const noexcept override
Definition auth.hpp:69
Definition session_core.hpp:17
int attempt
Definition key.ipp:70
const PassphraseCallback * cb
Definition key.ipp:68
Definition auth.hpp:18
AuthStatus
Definition auth_types.hpp:6
std::function< Result< SecureString >(const PassphraseRequest &)> PassphraseCallback
Definition key.hpp:35
std::function< Result< SecureString >()> PasswordCallback
Definition key.hpp:36
Definition error.hpp:110
std::string instruction
Definition auth.hpp:88
std::vector< Prompt > prompts
Definition auth.hpp:89
bool echo
Definition auth.hpp:86