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
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
// {
// 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)
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content