diff options
author | Vincent Richard <[email protected]> | 2014-03-16 21:52:40 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2014-03-16 21:52:40 +0000 |
commit | 84e570bbbb1188d2bcd3f03ff519fbc9217e624a (patch) | |
tree | 392b7e600433ee3fecd0c13e4020754477b42241 /examples/example6_authenticator.hpp | |
parent | Modified IMAP parser constructor to make testing easier. (diff) | |
download | vmime-84e570bbbb1188d2bcd3f03ff519fbc9217e624a.tar.gz vmime-84e570bbbb1188d2bcd3f03ff519fbc9217e624a.zip |
Connection trace facility.
Diffstat (limited to 'examples/example6_authenticator.hpp')
-rw-r--r-- | examples/example6_authenticator.hpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/examples/example6_authenticator.hpp b/examples/example6_authenticator.hpp new file mode 100644 index 00000000..b46f8ebd --- /dev/null +++ b/examples/example6_authenticator.hpp @@ -0,0 +1,106 @@ + + +#if VMIME_HAVE_SASL_SUPPORT + +// SASL authentication handler +class interactiveAuthenticator : public vmime::security::sasl::defaultSASLAuthenticator +{ + const std::vector <vmime::shared_ptr <vmime::security::sasl::SASLMechanism> > getAcceptableMechanisms + (const std::vector <vmime::shared_ptr <vmime::security::sasl::SASLMechanism> >& available, + vmime::shared_ptr <vmime::security::sasl::SASLMechanism> suggested) const + { + std::cout << std::endl << "Available SASL mechanisms:" << std::endl; + + for (unsigned int i = 0 ; i < available.size() ; ++i) + { + std::cout << " " << available[i]->getName(); + + if (suggested && available[i]->getName() == suggested->getName()) + std::cout << "(suggested)"; + } + + std::cout << std::endl << std::endl; + + return defaultSASLAuthenticator::getAcceptableMechanisms(available, suggested); + } + + void setSASLMechanism(vmime::shared_ptr <vmime::security::sasl::SASLMechanism> mech) + { + std::cout << "Trying '" << mech->getName() << "' authentication mechanism" << std::endl; + + defaultSASLAuthenticator::setSASLMechanism(mech); + } + + const vmime::string getUsername() const + { + if (m_username.empty()) + m_username = getUserInput("Username"); + + return m_username; + } + + const vmime::string getPassword() const + { + if (m_password.empty()) + m_password = getUserInput("Password"); + + return m_password; + } + + static const vmime::string getUserInput(const std::string& prompt) + { + std::cout << prompt << ": "; + std::cout.flush(); + + vmime::string res; + std::getline(std::cin, res); + + return res; + } + +private: + + mutable vmime::string m_username; + mutable vmime::string m_password; +}; + +#else // !VMIME_HAVE_SASL_SUPPORT + +// Simple authentication handler +class interactiveAuthenticator : public vmime::security::defaultAuthenticator +{ + const vmime::string getUsername() const + { + if (m_username.empty()) + m_username = getUserInput("Username"); + + return m_username; + } + + const vmime::string getPassword() const + { + if (m_password.empty()) + m_password = getUserInput("Password"); + + return m_password; + } + + static const vmime::string getUserInput(const std::string& prompt) + { + std::cout << prompt << ": "; + std::cout.flush(); + + vmime::string res; + std::getline(std::cin, res); + + return res; + } + +private: + + mutable vmime::string m_username; + mutable vmime::string m_password; +}; + +#endif // VMIME_HAVE_SASL_SUPPORT + |