Mission Impossible / How to create datatypes which cannot contain invalid state

adrianimboden
6,397 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Final - Hands On
Authentication Procedure
// {...}
// 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)
: config_{config} {
}
RequestTokenResult request_token(const std::string& email) {
if (email_to_userlevel(email) == UserLevel::logged_out) {
return RequestTokenResult::not_authorized;
}
const auto token = "random-token";
const auto send_successful = send_mail(email, token);
if (not send_successful) {
return RequestTokenResult::mail_send_error;
}
state_ = TokenRequested{token, email};
return RequestTokenResult::mail_sent;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content