Skip to content

Commit 871f427

Browse files
committed
Push auth_handler into an interface class
The standard impl is moved to a derived class auth_handler_impl to allow other derived implementations.
1 parent a197127 commit 871f427

File tree

4 files changed

+98
-70
lines changed

4 files changed

+98
-70
lines changed

src/auth_handler.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ namespace sdk {
1919
} // namespace
2020

2121
//
22-
// Common auth handling
22+
// Auth handling interface
2323
//
24-
auth_handler::auth_handler(session& session, const std::string& action, std::shared_ptr<signer> signer)
24+
auth_handler::auth_handler() {}
25+
26+
auth_handler::~auth_handler() {}
27+
28+
//
29+
// Common auth handling implementation
30+
//
31+
auth_handler_impl::auth_handler_impl(session& session, const std::string& action, std::shared_ptr<signer> signer)
2532
: m_session(session)
2633
, m_action(action)
2734
, m_attempts_remaining(TWO_FACTOR_ATTEMPTS)
@@ -33,7 +40,7 @@ namespace sdk {
3340
}
3441
}
3542

36-
auth_handler::auth_handler(session& session, const std::string& action)
43+
auth_handler_impl::auth_handler_impl(session& session, const std::string& action)
3744
: m_session(session)
3845
, m_action(action)
3946
, m_attempts_remaining(TWO_FACTOR_ATTEMPTS)
@@ -45,9 +52,9 @@ namespace sdk {
4552
}
4653
}
4754

48-
auth_handler::~auth_handler() {}
55+
auth_handler_impl::~auth_handler_impl() {}
4956

50-
void auth_handler::init(const std::string& action, std::shared_ptr<signer> signer, bool is_pre_login)
57+
void auth_handler_impl::init(const std::string& action, std::shared_ptr<signer> signer, bool is_pre_login)
5158
{
5259
m_signer = signer;
5360
set_action(action);
@@ -58,7 +65,7 @@ namespace sdk {
5865
m_state = m_methods.empty() ? state_type::make_call : state_type::request_code;
5966
}
6067

61-
void auth_handler::set_action(const std::string& action)
68+
void auth_handler_impl::set_action(const std::string& action)
6269
{
6370
m_action = action;
6471
m_is_hw_action = m_signer && m_signer->is_hw_device()
@@ -68,26 +75,26 @@ namespace sdk {
6875
|| action == "get_unspent_outputs" || action == "get_expired_deposits");
6976
}
7077

71-
void auth_handler::set_error(const std::string& error_message)
78+
void auth_handler_impl::set_error(const std::string& error_message)
7279
{
7380
GDK_LOG_SEV(log_level::debug) << m_action << " call exception: " << error_message;
7481
m_state = state_type::error;
7582
m_error = error_message;
7683
}
7784

78-
void auth_handler::set_data()
85+
void auth_handler_impl::set_data()
7986
{
8087
m_twofactor_data
8188
= { { "action", m_action }, { "device", m_is_hw_action ? m_signer->get_hw_device() : nlohmann::json() } };
8289
}
8390

84-
void auth_handler::request_code(const std::string& method)
91+
void auth_handler_impl::request_code(const std::string& method)
8592
{
8693
request_code_impl(method);
8794
m_attempts_remaining = TWO_FACTOR_ATTEMPTS;
8895
}
8996

90-
void auth_handler::request_code_impl(const std::string& method)
97+
void auth_handler_impl::request_code_impl(const std::string& method)
9198
{
9299
GDK_RUNTIME_ASSERT(m_state == state_type::request_code);
93100

@@ -100,14 +107,14 @@ namespace sdk {
100107
m_state = state_type::resolve_code;
101108
}
102109

103-
void auth_handler::resolve_code(const std::string& code)
110+
void auth_handler_impl::resolve_code(const std::string& code)
104111
{
105112
GDK_RUNTIME_ASSERT(m_state == state_type::resolve_code);
106113
m_code = code;
107114
m_state = state_type::make_call;
108115
}
109116

110-
void auth_handler::operator()()
117+
void auth_handler_impl::operator()()
111118
{
112119
GDK_RUNTIME_ASSERT(m_state == state_type::make_call);
113120
try {
@@ -149,7 +156,7 @@ namespace sdk {
149156
}
150157
}
151158

152-
nlohmann::json auth_handler::get_status() const
159+
nlohmann::json auth_handler_impl::get_status() const
153160
{
154161
GDK_RUNTIME_ASSERT(m_state == state_type::error || m_error.empty());
155162

src/auth_handler.hpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ namespace sdk {
99
class session;
1010

1111
struct auth_handler {
12-
auth_handler(session& session, const std::string& action, std::shared_ptr<signer> signer);
13-
auth_handler(session& session, const std::string& action);
12+
auth_handler();
1413
auth_handler(const auth_handler&) = delete;
1514
auth_handler& operator=(const auth_handler&) = delete;
1615
auth_handler(auth_handler&&) = delete;
1716
auth_handler& operator=(auth_handler&&) = delete;
1817
virtual ~auth_handler();
1918

20-
virtual void request_code(const std::string& method);
21-
void resolve_code(const std::string& code);
19+
virtual void request_code(const std::string& method) = 0;
20+
virtual void resolve_code(const std::string& code) = 0;
2221

23-
virtual nlohmann::json get_status() const;
24-
void set_action(const std::string& action);
25-
void set_data();
22+
virtual nlohmann::json get_status() const = 0;
2623

27-
virtual void operator()();
24+
virtual void operator()() = 0;
2825

2926
protected:
3027
enum class state_type : uint32_t {
@@ -35,10 +32,32 @@ namespace sdk {
3532
error // User should handle the error
3633
};
3734

38-
void set_error(const std::string& error_message);
35+
virtual void set_action(const std::string& action) = 0;
36+
virtual void set_error(const std::string& error_message) = 0;
37+
virtual void set_data() = 0;
3938

40-
void request_code_impl(const std::string& method);
39+
virtual void request_code_impl(const std::string& method) = 0;
4140
virtual state_type call_impl() = 0;
41+
};
42+
43+
struct auth_handler_impl : public auth_handler {
44+
auth_handler_impl(session& session, const std::string& action, std::shared_ptr<signer> signer);
45+
auth_handler_impl(session& session, const std::string& action);
46+
~auth_handler_impl();
47+
48+
virtual void request_code(const std::string& method) override;
49+
virtual void resolve_code(const std::string& code) final;
50+
51+
virtual nlohmann::json get_status() const final;
52+
53+
virtual void operator()() final;
54+
55+
protected:
56+
virtual void set_action(const std::string& action) final;
57+
virtual void set_error(const std::string& error_message) final;
58+
virtual void set_data() final;
59+
60+
virtual void request_code_impl(const std::string& method) final;
4261

4362
session& m_session;
4463
std::shared_ptr<signer> m_signer;
@@ -50,12 +69,13 @@ namespace sdk {
5069
std::string m_error; // Error details if any
5170
nlohmann::json m_result; // Result of any successful action
5271
nlohmann::json m_twofactor_data; // Actual data to send along with any call
53-
state_type m_state; // Current state
72+
auth_handler::state_type m_state; // Current state
5473
uint32_t m_attempts_remaining;
5574

5675
private:
5776
void init(const std::string& action, std::shared_ptr<signer> signer, bool is_pre_login);
5877
};
78+
5979
} // namespace sdk
6080
} // namespace ga
6181
#endif

0 commit comments

Comments
 (0)