1 
2 
3 #if VMIME_HAVE_SASL_SUPPORT
4 
5 // SASL authentication handler
6 class interactiveAuthenticator : public vmime::security::sasl::defaultSASLAuthenticator
7 {
getAcceptableMechanisms(const std::vector<vmime::shared_ptr<vmime::security::sasl::SASLMechanism>> & available,vmime::shared_ptr<vmime::security::sasl::SASLMechanism> suggested) const8 	const std::vector <vmime::shared_ptr <vmime::security::sasl::SASLMechanism> > getAcceptableMechanisms
9 		(const std::vector <vmime::shared_ptr <vmime::security::sasl::SASLMechanism> >& available,
10 		 vmime::shared_ptr <vmime::security::sasl::SASLMechanism> suggested) const
11 	{
12 		std::cout << std::endl << "Available SASL mechanisms:" << std::endl;
13 
14 		for (unsigned int i = 0 ; i < available.size() ; ++i)
15 		{
16 			std::cout << "  " << available[i]->getName();
17 
18 			if (suggested && available[i]->getName() == suggested->getName())
19 				std::cout << "(suggested)";
20 		}
21 
22 		std::cout << std::endl << std::endl;
23 
24 		return defaultSASLAuthenticator::getAcceptableMechanisms(available, suggested);
25 	}
26 
setSASLMechanism(vmime::shared_ptr<vmime::security::sasl::SASLMechanism> mech)27 	void setSASLMechanism(vmime::shared_ptr <vmime::security::sasl::SASLMechanism> mech)
28 	{
29 		std::cout << "Trying '" << mech->getName() << "' authentication mechanism" << std::endl;
30 
31 		defaultSASLAuthenticator::setSASLMechanism(mech);
32 	}
33 
getUsername() const34 	const vmime::string getUsername() const
35 	{
36 		if (m_username.empty())
37 			m_username = getUserInput("Username");
38 
39 		return m_username;
40 	}
41 
getPassword() const42 	const vmime::string getPassword() const
43 	{
44 		if (m_password.empty())
45 			m_password = getUserInput("Password");
46 
47 		return m_password;
48 	}
49 
getUserInput(const std::string & prompt)50 	static const vmime::string getUserInput(const std::string& prompt)
51 	{
52 		std::cout << prompt << ": ";
53 		std::cout.flush();
54 
55 		vmime::string res;
56 		std::getline(std::cin, res);
57 
58 		return res;
59 	}
60 
61 private:
62 
63 	mutable vmime::string m_username;
64 	mutable vmime::string m_password;
65 };
66 
67 #else // !VMIME_HAVE_SASL_SUPPORT
68 
69 // Simple authentication handler
70 class interactiveAuthenticator : public vmime::security::defaultAuthenticator
71 {
getUsername() const72 	const vmime::string getUsername() const
73 	{
74 		if (m_username.empty())
75 			m_username = getUserInput("Username");
76 
77 		return m_username;
78 	}
79 
getPassword() const80 	const vmime::string getPassword() const
81 	{
82 		if (m_password.empty())
83 			m_password = getUserInput("Password");
84 
85 		return m_password;
86 	}
87 
getUserInput(const std::string & prompt)88 	static const vmime::string getUserInput(const std::string& prompt)
89 	{
90 		std::cout << prompt << ": ";
91 		std::cout.flush();
92 
93 		vmime::string res;
94 		std::getline(std::cin, res);
95 
96 		return res;
97 	}
98 
99 private:
100 
101 	mutable vmime::string m_username;
102 	mutable vmime::string m_password;
103 };
104 
105 #endif // VMIME_HAVE_SASL_SUPPORT
106 
107