aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/examples/example6_authenticator.hpp
blob: 56f02392981dc6238dfed403e0fc8527ce1b4a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112


#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,
			const 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(const 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