Mission Impossible / How to create datatypes which cannot contain invalid state
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Authentication Procedure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// {
#include <boost/assert.hpp>
#include <optional>
#include <set>
#include <string>
#include <variant>
// likely part of C++20
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>;
// }
// given
bool send_mail(std::string recipient, std::string token) {
return true;
}
// end given
enum class UserLevel { logged_out, normal, admin };
struct AuthConfig {
std::set<std::string> users;
std::set<std::string> admins;
};
class AuthenticationProcedure {
private:
struct LoggedOut {};
struct TokenRequested {
std::string token;
std::string email;
};
struct LoggedIn {
std::string email;
};
using State = std::variant<LoggedOut, TokenRequested, LoggedIn>;
public:
struct VisibleState {
UserLevel level;
std::optional<std::string> email;
};
enum class LoginResult { success, failure };
enum class RequestTokenResult { mail_sent, mail_send_error, not_authorized };
explicit AuthenticationProcedure(AuthConfig config)
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content