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 <curl/curl.h>
17 #include "zypp/base/Deprecated.h"
20 #include "zypp/base/PtrTypes.h"
25 ///////////////////////////////////////////////////////////////////
29 * Class for handling media authentication data. This is the most generic
30 * class containing only username and password members.
38 AuthData(const Url & url);
40 AuthData(const std::string & username, const std::string & password)
41 : _username(username), _password(password)
44 virtual ~AuthData() {};
47 * Checks validity of authentication data.
48 * \return true if the object contains non-empty username and
49 * non-empty password, false otherwise.
51 virtual bool valid() const;
53 void setUrl(const Url & url) { _url = url; }
54 void setUsername(const std::string & username) { _username = username; }
55 void setPassword(const std::string & password) { _password = password; }
57 Url url() const { return _url; }
58 std::string username() const { return _username; }
59 std::string password() const { return _password; }
61 virtual std::ostream & dumpOn( std::ostream & str ) const;
63 virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
67 std::string _username;
68 std::string _password;
71 typedef shared_ptr<AuthData> AuthData_Ptr;
74 * Curl HTTP authentication data.
76 class CurlAuthData : public AuthData {
79 * Default constructor. Initializes username and password to empty strings
80 * and authetication type to CURLAUTH_NONE.
82 CurlAuthData() : AuthData(), _auth_type_str(), _auth_type(CURLAUTH_NONE)
85 CurlAuthData(const AuthData & authData)
88 , _auth_type(CURLAUTH_NONE)
91 CurlAuthData(std::string & username, std::string & password, std::string & auth_type)
92 : AuthData(username,password), _auth_type_str(auth_type)
94 _auth_type = auth_type_str2long(auth_type);
97 CurlAuthData(std::string & username, std::string & password, long auth_type)
98 : AuthData(username,password), _auth_type(auth_type)
100 _auth_type_str = auth_type_long2str(auth_type);
104 * Checks validity of authentication data.
105 * \return true if the object contains non-empty username,
106 * non-empty password, and specifies authentication type; false otherwise.
108 virtual bool valid() const;
111 * Set HTTP authentication type(s) to use.
112 * \param comma separated list of HTTP authentication type names
114 void setAuthType(std::string auth_type)
116 _auth_type_str = auth_type; _auth_type = auth_type_str2long(auth_type);
120 * Set HTTP authentication type(s) to use.
121 * \param HTTP authentication type as in long ORed form.
122 * \see curl.h for available auth types
124 void setAuthType(long auth_type)
126 _auth_type = auth_type;
127 _auth_type_str = auth_type_long2str(auth_type);
130 long authType() { return _auth_type; } const
131 std::string authTypeAsString() { return _auth_type_str; } const
133 std::string getUserPwd() const { return username() + ":" + password(); }
137 * Converts a string of comma separated list of authetication type names
138 * into a long of ORed CURLAUTH_* identifiers.
139 * The method also automatically leaves out any auth types declared
140 * not supported by curl_version_info().
142 * \throws MediaException if an invalid authentication type name is
145 static long auth_type_str2long(std::string & auth_type_str);
148 * Converts a long of ORed CURLAUTH_* identifiers into a string of comma
149 * separated list of authentication type names.
151 static std::string auth_type_long2str(long auth_type);
153 virtual std::ostream & dumpOn( std::ostream & str ) const;
156 std::string _auth_type_str;
160 typedef shared_ptr<CurlAuthData> CurlAuthData_Ptr;
162 std::ostream & operator << (std::ostream & str, const AuthData & auth_data);
163 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data);
165 ///////////////////////////////////////////////////////////////////
170 #endif // ZYPP_MEDIA_USER_AUTH_H