1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaUserAuth.h
10 * Convenience interface for handling authentication data of media user.
12 #ifndef ZYPP_MEDIA_USER_AUTH_H
13 #define ZYPP_MEDIA_USER_AUTH_H
15 #include <zypp/APIConfig.h>
18 #include <zypp/base/PtrTypes.h>
23 ///////////////////////////////////////////////////////////////////
27 * Class for handling media authentication data. This is the most generic
28 * class containing only username and password members.
36 AuthData(const Url & url);
38 AuthData(const std::string & username, const std::string & password)
39 : _username(username), _password(password)
42 virtual ~AuthData() {};
45 * Checks validity of authentication data.
46 * \return true if the object contains non-empty username and
47 * non-empty password, false otherwise.
49 virtual bool valid() const;
51 void setUrl(const Url & url) { _url = url; }
52 void setUsername(const std::string & username) { _username = username; }
53 void setPassword(const std::string & password) { _password = password; }
55 Url url() const { return _url; }
56 std::string username() const { return _username; }
57 std::string password() const { return _password; }
59 virtual std::ostream & dumpOn( std::ostream & str ) const;
61 virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
65 std::string _username;
66 std::string _password;
69 typedef shared_ptr<AuthData> AuthData_Ptr;
72 * Curl HTTP authentication data.
74 class CurlAuthData : public AuthData {
77 * Default constructor. Initializes username and password to empty strings
78 * and authetication type to CURLAUTH_NONE.
82 CurlAuthData(const AuthData & authData);
84 CurlAuthData(std::string & username, std::string & password, std::string & auth_type)
85 : AuthData(username,password), _auth_type_str(auth_type)
87 _auth_type = auth_type_str2long(auth_type);
90 CurlAuthData(std::string & username, std::string & password, long auth_type)
91 : AuthData(username,password), _auth_type(auth_type)
93 _auth_type_str = auth_type_long2str(auth_type);
97 * Checks validity of authentication data.
98 * \return true if the object contains non-empty username,
99 * non-empty password, and specifies authentication type; false otherwise.
101 virtual bool valid() const;
104 * Set HTTP authentication type(s) to use.
105 * \param comma separated list of HTTP authentication type names
107 void setAuthType(std::string auth_type)
109 _auth_type_str = auth_type; _auth_type = auth_type_str2long(auth_type);
113 * Set HTTP authentication type(s) to use.
114 * \param HTTP authentication type as in long ORed form.
115 * \see curl.h for available auth types
117 void setAuthType(long auth_type)
119 _auth_type = auth_type;
120 _auth_type_str = auth_type_long2str(auth_type);
123 long authType() const { return _auth_type; }
124 std::string authTypeAsString() const { return _auth_type_str; }
126 std::string getUserPwd() const { return username() + ":" + password(); }
130 * Converts a string of comma separated list of authetication type names
131 * into a long of ORed CURLAUTH_* identifiers.
132 * The method also automatically leaves out any auth types declared
133 * not supported by curl_version_info().
135 * \throws MediaException if an invalid authentication type name is
138 static long auth_type_str2long(std::string & auth_type_str);
141 * Converts a long of ORed CURLAUTH_* identifiers into a string of comma
142 * separated list of authentication type names.
144 static std::string auth_type_long2str(long auth_type);
146 virtual std::ostream & dumpOn( std::ostream & str ) const;
149 std::string _auth_type_str;
153 typedef shared_ptr<CurlAuthData> CurlAuthData_Ptr;
155 std::ostream & operator << (std::ostream & str, const AuthData & auth_data);
156 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data);
158 ///////////////////////////////////////////////////////////////////
163 #endif // ZYPP_MEDIA_USER_AUTH_H