0% found this document useful (0 votes)
67 views3 pages

Authentication - C++

This document defines an authentication system with classes for authentication results, different authentication interfaces (IAuthentication), and implementations of authentication using a mock, username/password, and certificate. The Client class uses the IAuthentication interface to perform authentication and get results, which it then checks and prints status messages based on authentication success. The main function demonstrates using the different authentication implementations with the Client.

Uploaded by

SeZaR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views3 pages

Authentication - C++

This document defines an authentication system with classes for authentication results, different authentication interfaces (IAuthentication), and implementations of authentication using a mock, username/password, and certificate. The Client class uses the IAuthentication interface to perform authentication and get results, which it then checks and prints status messages based on authentication success. The main function demonstrates using the different authentication implementations with the Client.

Uploaded by

SeZaR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <string>

#include <iostream>

using namespace std;

class AuthenticationResult {
bool authenticated;
string username;
public:
AuthenticationResult(bool authenticated, string username);
bool isAuthenticated();
string getUsername();

};

AuthenticationResult::AuthenticationResult(bool authenticated, string


username = "unauthenticated_user")
: authenticated(authenticated), username(username)
{}
bool AuthenticationResult::isAuthenticated()
{
return authenticated;
}
string AuthenticationResult::getUsername()
{
return username;
}

class IAuthentication {
public:
virtual AuthenticationResult authenticate() = 0;
};

class Client {

IAuthentication* auth = nullptr;


AuthenticationResult authResult = AuthenticationResult(false,
"unauthenticated_user");

public:
void setAuthentication(IAuthentication* auth);
void execute();

};

void Client::setAuthentication(IAuthentication* auth)


{
this->auth = auth;
}

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;
}
}

class MockAuth : public IAuthentication {


public:
AuthenticationResult authenticate();
};

AuthenticationResult MockAuth::authenticate()
{
return AuthenticationResult(true, "Default");
}

class UsernamePassword : public IAuthentication {


public:
AuthenticationResult authenticate();
};

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);
}
}

class Certificate : public IAuthentication {


public:
AuthenticationResult authenticate();
};

AuthenticationResult Certificate::authenticate() {
string owner;
cout << "Zertifikatsaussteller: " << endl;
cin >> owner;
if (owner == "hs-esslingen") {
return AuthenticationResult(true, "certificate.owner");
}
else {
return AuthenticationResult(false);
}
}

int main(int argc, char* argv[]) {


Client client;

cout << "Authentifizierung über MockAuth" << endl;


IAuthentication* mockauth = new MockAuth();
client.setAuthentication(mockauth);
client.execute();

cout << "Authentifizierung über UsernamePassword" << endl;


IAuthentication* usernamepassword = new UsernamePassword();
client.setAuthentication(usernamepassword);
client.execute();

cout << "Authentifizierung über Zertifikat" << endl;


IAuthentication* certificate = new Certificate();
client.setAuthentication(certificate);
client.execute();
return 0;
}

You might also like