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 /** \deprecated use setUsername() instead */
56 ZYPP_DEPRECATED void setUserName(const std::string & username) { _username = username; }
57 void setPassword(const std::string & password) { _password = password; }
59 Url url() const { return _url; }
60 std::string username() const { return _username; }
61 std::string password() const { return _password; }
63 virtual std::ostream & dumpOn( std::ostream & str ) const;
65 virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
69 std::string _username;
70 std::string _password;
73 typedef shared_ptr<AuthData> AuthData_Ptr;
76 * Curl HTTP authentication data.
78 class CurlAuthData : public AuthData {
81 * Default constructor. Initializes username and password to empty strings
82 * and authetication type to CURLAUTH_NONE.
84 CurlAuthData() : AuthData(), _auth_type_str(), _auth_type(CURLAUTH_NONE)
87 CurlAuthData(const AuthData & authData)
90 , _auth_type(CURLAUTH_NONE)
93 CurlAuthData(std::string & username, std::string & password, std::string & auth_type)
94 : AuthData(username,password), _auth_type_str(auth_type)
96 _auth_type = auth_type_str2long(auth_type);
99 CurlAuthData(std::string & username, std::string & password, long auth_type)
100 : AuthData(username,password), _auth_type(auth_type)
102 _auth_type_str = auth_type_long2str(auth_type);
106 * Checks validity of authentication data.
107 * \return true if the object contains non-empty username,
108 * non-empty password, and specifies authentication type; false otherwise.
110 virtual bool valid() const;
113 * Set HTTP authentication type(s) to use.
114 * \param comma separated list of HTTP authentication type names
116 void setAuthType(std::string auth_type)
118 _auth_type_str = auth_type; _auth_type = auth_type_str2long(auth_type);
122 * Set HTTP authentication type(s) to use.
123 * \param HTTP authentication type as in long ORed form.
124 * \see curl.h for available auth types
126 void setAuthType(long auth_type)
128 _auth_type = auth_type;
129 _auth_type_str = auth_type_long2str(auth_type);
132 long authType() { return _auth_type; } const
133 std::string authTypeAsString() { return _auth_type_str; } const
135 std::string getUserPwd() const { return username() + ":" + password(); }
139 * Converts a string of comma separated list of authetication type names
140 * into a long of ORed CURLAUTH_* identifiers.
141 * The method also automatically leaves out any auth types declared
142 * not supported by curl_version_info().
144 * \throws MediaException if an invalid authentication type name is
147 static long auth_type_str2long(std::string & auth_type_str);
150 * Converts a long of ORed CURLAUTH_* identifiers into a string of comma
151 * separated list of authentication type names.
153 static std::string auth_type_long2str(long auth_type);
155 virtual std::ostream & dumpOn( std::ostream & str ) const;
158 std::string _auth_type_str;
162 typedef shared_ptr<CurlAuthData> CurlAuthData_Ptr;
164 std::ostream & operator << (std::ostream & str, const AuthData & auth_data);
165 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data);
167 ///////////////////////////////////////////////////////////////////
172 #endif // ZYPP_MEDIA_USER_AUTH_H