Skip to content

Instantly share code, notes, and snippets.

@TheMaverickProgrammer
Created January 5, 2020 21:29
Show Gist options
  • Save TheMaverickProgrammer/07e1dabf4c704dbfbd364b90d1d30ee6 to your computer and use it in GitHub Desktop.
Save TheMaverickProgrammer/07e1dabf4c704dbfbd364b90d1d30ee6 to your computer and use it in GitHub Desktop.
void login(httplib::Client& client) {
std::string user, pass;
std::cout << "user: ";
std::getline(std::cin, user);
std::cout << "pass: ";
char input[256];
// We're going to overflow/overwrite the cout buffer via this string stream contents
std::stringstream ss(input);
char c = 0;
size_t idx = 0;
do {
c = _getch(); // this is cross paltform and POSIX standard
if (idx > 0 && (int)c == 8) {
// code 8 is backspace, so let's overwrite our cursor pos
// and then fill the previous line with a space
std::cout << c << " " << c;
// keep track of how far we backspace so we don't clear the entire console with this trick:
idx--;
continue;
}
else if ((int)c == 13) {
// code 13 is ENTER
break;
}
else if (idx == 0 && (int)c == 8) {
// We don't backspace if there's nothing to erase...
continue;
}
// Fill whatever the user typed with *
std::cout << "*";
// Store the real password in the buffer
input[idx++] = c;
} while (idx < 256);
std::cout << std::endl;
input[idx] = '\0';
// Get the password from the character stream
std::getline(ss, pass);
// create an http basic auth header
client.set_basic_auth(user.c_str(), pass.c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment