Authentication - C++
Authentication - C++
#include <iostream>
class AuthenticationResult {
bool authenticated;
string username;
public:
AuthenticationResult(bool authenticated, string username);
bool isAuthenticated();
string getUsername();
};
class IAuthentication {
public:
virtual AuthenticationResult authenticate() = 0;
};
class Client {
public:
void setAuthentication(IAuthentication* auth);
void execute();
};
void Client::execute() {
authResult = auth->authenticate();
if (authResult.isAuthenticated()) {
cout << "Das Programm wird ausgeführt für " <<
authResult.getUsername() << "." << endl;
}
else {
cout << "Das Programm konnte nicht ausgeführt werden." << endl;
}
}
AuthenticationResult MockAuth::authenticate()
{
return AuthenticationResult(true, "Default");
}
AuthenticationResult UsernamePassword::authenticate() {
string username;
string password;
cout << "Username: " << endl;
cin >> username;
cout << "Password: " << endl;
cin >> password;
if (username == password) {
return AuthenticationResult(true, username);
}
else {
return AuthenticationResult(false, username);
}
}
AuthenticationResult Certificate::authenticate() {
string owner;
cout << "Zertifikatsaussteller: " << endl;
cin >> owner;
if (owner == "hs-esslingen") {
return AuthenticationResult(true, "certificate.owner");
}
else {
return AuthenticationResult(false);
}
}