Cleanup and remove deprecated interface methods
[platform/upstream/libzypp.git] / zypp / media / MediaUserAuth.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaUserAuth.h
10  * Convenience interface for handling authentication data of media user.
11  */
12 #ifndef ZYPP_MEDIA_USER_AUTH_H
13 #define ZYPP_MEDIA_USER_AUTH_H
14
15 #include <curl/curl.h>
16
17 #include "zypp/base/Deprecated.h"
18
19 #include "zypp/Url.h"
20 #include "zypp/base/PtrTypes.h"
21
22 namespace zypp {
23   namespace media {
24
25 ///////////////////////////////////////////////////////////////////
26
27
28 /**
29  * Class for handling media authentication data. This is the most generic
30  * class containing only username and password members.
31  */
32 class AuthData
33 {
34 public:
35   AuthData()
36   {}
37
38   AuthData(const Url & url);
39
40   AuthData(const std::string & username, const std::string & password)
41     : _username(username), _password(password)
42   {}
43
44   virtual ~AuthData() {};
45
46   /**
47    * Checks validity of authentication data.
48    * \return true if the object contains non-empty username and
49    *  non-empty password, false otherwise.
50    */
51   virtual bool valid() const;
52
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; }
56
57   Url url() const { return _url; }
58   std::string username() const { return _username; }
59   std::string password() const { return _password; }
60
61   virtual std::ostream & dumpOn( std::ostream & str ) const;
62
63   virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
64
65 private:
66   Url _url;
67   std::string _username;
68   std::string _password;
69 };
70
71 typedef shared_ptr<AuthData> AuthData_Ptr;
72
73 /**
74  * Curl HTTP authentication data.
75  */
76 class CurlAuthData : public AuthData {
77 public:
78   /**
79    * Default constructor. Initializes username and password to empty strings
80    * and authetication type to CURLAUTH_NONE.
81    */
82   CurlAuthData() : AuthData(), _auth_type_str(), _auth_type(CURLAUTH_NONE)
83   {}
84
85   CurlAuthData(const AuthData & authData)
86     : AuthData(authData)
87     , _auth_type_str()
88     , _auth_type(CURLAUTH_NONE)
89   {}
90
91   CurlAuthData(std::string & username, std::string & password, std::string & auth_type)
92     : AuthData(username,password), _auth_type_str(auth_type)
93   {
94     _auth_type = auth_type_str2long(auth_type);
95   }
96
97   CurlAuthData(std::string & username, std::string & password, long auth_type)
98     : AuthData(username,password), _auth_type(auth_type)
99   {
100     _auth_type_str = auth_type_long2str(auth_type);
101   }
102
103   /**
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.
107    */
108   virtual bool valid() const;
109
110   /**
111    * Set HTTP authentication type(s) to use.
112    * \param comma separated list of HTTP authentication type names
113    */
114   void setAuthType(std::string auth_type)
115   {
116     _auth_type_str = auth_type; _auth_type = auth_type_str2long(auth_type);
117   }
118
119   /*
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
123    */
124   void setAuthType(long auth_type)
125   {
126     _auth_type = auth_type;
127     _auth_type_str = auth_type_long2str(auth_type);
128   }
129
130   long authType() { return _auth_type; } const
131   std::string authTypeAsString() { return _auth_type_str; } const
132
133   std::string getUserPwd() const { return username() + ":" + password(); }
134
135
136   /**
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().
141    *
142    * \throws MediaException if an invalid authentication type name is
143    *         encountered.
144    */
145   static long auth_type_str2long(std::string & auth_type_str);
146
147   /**
148    * Converts a long of ORed CURLAUTH_* identifiers into a string of comma
149    * separated list of authentication type names.
150    */
151   static std::string auth_type_long2str(long auth_type);
152
153   virtual std::ostream & dumpOn( std::ostream & str ) const;
154
155 private:
156   std::string _auth_type_str;
157   long _auth_type;
158 };
159
160 typedef shared_ptr<CurlAuthData> CurlAuthData_Ptr;
161
162 std::ostream & operator << (std::ostream & str, const AuthData & auth_data);
163 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data);
164
165 ///////////////////////////////////////////////////////////////////
166
167   } // namespace media
168 } // namespace zypp
169
170 #endif // ZYPP_MEDIA_USER_AUTH_H