252e591808e5d780289ac5846903c7e80fb6a8ec
[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   /** \deprecated use setUsername() instead */
56   ZYPP_DEPRECATED void setUserName(const std::string & username) { _username = username; }
57   void setPassword(const std::string & password) { _password = password; }  
58
59   Url url() const { return _url; }
60   std::string username() const { return _username; }
61   std::string password() const { return _password; } 
62
63   virtual std::ostream & dumpOn( std::ostream & str ) const;
64
65   virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
66
67 private:
68   Url _url;
69   std::string _username;
70   std::string _password;
71 };
72
73 typedef shared_ptr<AuthData> AuthData_Ptr;
74
75 /**
76  * Curl HTTP authentication data.
77  */
78 class CurlAuthData : public AuthData {
79 public:
80   /**
81    * Default constructor. Initializes username and password to empty strings
82    * and authetication type to CURLAUTH_NONE.
83    */
84   CurlAuthData() : AuthData(), _auth_type_str(), _auth_type(CURLAUTH_NONE)
85   {}
86
87   CurlAuthData(const AuthData & authData)
88     : AuthData(authData)
89     , _auth_type_str()
90     , _auth_type(CURLAUTH_NONE)
91   {}
92
93   CurlAuthData(std::string & username, std::string & password, std::string & auth_type)
94     : AuthData(username,password), _auth_type_str(auth_type)
95   {
96     _auth_type = auth_type_str2long(auth_type);
97   }
98
99   CurlAuthData(std::string & username, std::string & password, long auth_type)
100     : AuthData(username,password), _auth_type(auth_type)
101   {
102     _auth_type_str = auth_type_long2str(auth_type);
103   }
104
105   /**
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.  
109    */
110   virtual bool valid() const;
111
112   /**
113    * Set HTTP authentication type(s) to use.
114    * \param comma separated list of HTTP authentication type names
115    */
116   void setAuthType(std::string auth_type)
117   {
118     _auth_type_str = auth_type; _auth_type = auth_type_str2long(auth_type);
119   }
120
121   /*
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
125    */
126   void setAuthType(long auth_type)
127   {
128     _auth_type = auth_type;
129     _auth_type_str = auth_type_long2str(auth_type);
130   }
131
132   long authType() { return _auth_type; } const
133   std::string authTypeAsString() { return _auth_type_str; } const
134
135   std::string getUserPwd() const { return username() + ":" + password(); } 
136
137
138   /**
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().
143    * 
144    * \throws MediaException if an invalid authentication type name is
145    *         encountered.
146    */
147   static long auth_type_str2long(std::string & auth_type_str);
148
149   /**
150    * Converts a long of ORed CURLAUTH_* identifiers into a string of comma
151    * separated list of authentication type names.
152    */
153   static std::string auth_type_long2str(long auth_type);
154
155   virtual std::ostream & dumpOn( std::ostream & str ) const;
156
157 private:
158   std::string _auth_type_str;
159   long _auth_type;
160 };
161
162 typedef shared_ptr<CurlAuthData> CurlAuthData_Ptr;
163
164 std::ostream & operator << (std::ostream & str, const AuthData & auth_data);
165 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data);
166
167 ///////////////////////////////////////////////////////////////////
168
169   } // namespace media
170 } // namespace zypp
171
172 #endif // ZYPP_MEDIA_USER_AUTH_H