CppSecurity  1.1.1.0
C++ Security Library
google_authenticator.cpp

Google Authenticator example

#include <iostream>
int main(int argc, char** argv)
{
// Show the password
std::password password = (argc > 1) ? argv[1] : "passw0rd";
std::cout << "Password: " << password << std::endl;
// Generate the secret, URL and QR Code link
std::password secret = authenticator.GenerateSecret(password);
std::password url = authenticator.GenerateURL("test", secret);
std::password qr = authenticator.GenerateQRCodeLink(url);
std::cout << "Generated secret: " << secret << std::endl;
std::cout << "Generated URL: " << url << std::endl;
std::cout << "Generated QR Code link: " << qr << std::endl;
std::cout << "Please enter Google Authenticator tokens to validate. Enter '0' to exit..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty() || (line == "0"))
break;
size_t token = std::stoi(line);
// Validate the token
std::cout << (authenticator.Validate(token, secret) ? "Valid" : "Invalid") << std::endl;
}
return 0;
}
std::password GenerateURL(std::string_view identifier, std::string_view secret) const
Generate the Google Authenticator URL.
std::password GenerateQRCodeLink(std::string_view url, size_t width=100, size_t height=100) const
Generate the Google Authenticator QR Code link.
std::password GenerateSecret(std::string_view password) const
Generate the Google Authenticator secret from the given user password.
bool Validate(size_t token, std::string_view secret, const CppCommon::Timestamp &timestamp=CppCommon::UtcTimestamp()) const
Validate the Google Authenticator token over the given secret and UTC timestamp.
Password string.
Definition: password.h:53
Google Authenticator definition.