libzypp  17.28.5
MediaUserAuth.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <list>
14 #include <curl/curl.h>
15 
16 #include <zypp/base/Gettext.h>
17 #include <zypp/base/String.h>
18 
21 
22 using std::endl;
23 
24 namespace zypp {
25  namespace media {
26 
27 
28 AuthData::AuthData(const Url & url)
29  : _url(url), _lastChange(0)
30 {
33 }
34 
35 
36 bool AuthData::valid() const
37 {
38  return username().size() && password().size();
39 }
40 
42 {
43  return _lastChange;
44 }
45 
47 {
48  _lastChange = time;
49 }
50 
51 std::ostream & AuthData::dumpOn( std::ostream & str ) const
52 {
53  if (_url.isValid())
55  else
56  str << "[<no-url>]" << endl;
57  str << "username: '" << _username << "'" << std::endl
58  << "password: " << (_password.empty() ? "<empty>" : "<non-empty>");
59  return str;
60 }
61 
62 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
63 {
64  if (_url.isValid())
65  str
66  << "[" << _url.asString(
70  << "]" << endl;
71 
72  str
73  << "username = " << _username << endl
74  << "password = " << _password << endl;
75 
76  return str;
77 }
78 
80  : AuthData()
81  , _auth_type_str()
82  , _auth_type(CURLAUTH_NONE)
83 {}
84 
86  : AuthData(authData)
87  , _auth_type_str()
88  , _auth_type(CURLAUTH_NONE)
89 {}
90 
91 bool CurlAuthData::valid() const
92 {
93  return username().size() && password().size();
94 }
95 
96 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
97 {
98  AuthData::dumpOn(str) << endl
99  << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
100  return str;
101 }
102 
103 long CurlAuthData::auth_type_str2long( std::string & auth_type_str )
104 {
105  return auth_type_str2long( const_cast< const std::string &>(auth_type_str) );
106 }
107 
108 long CurlAuthData::auth_type_str2long( const std::string & auth_type_str )
109 {
110  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
111 
112  std::vector<std::string> list;
113  std::vector<std::string>::const_iterator it;
114  long auth_type = CURLAUTH_NONE;
115 
116  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
117 
118  for(it = list.begin(); it != list.end(); ++it)
119  {
120  if(*it == "basic")
121  {
122  auth_type |= CURLAUTH_BASIC;
123  }
124  else
125  if(*it == "digest")
126  {
127  auth_type |= CURLAUTH_DIGEST;
128  }
129  else
130  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
131  (*it == "ntlm"))
132  {
133  auth_type |= CURLAUTH_NTLM;
134  }
135  else
136  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
137  (*it == "spnego" || *it == "negotiate"))
138  {
139  // there is no separate spnego flag for this auth type
140  auth_type |= CURLAUTH_GSSNEGOTIATE;
141  }
142  else
143  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
144  (*it == "gssnego" || *it == "negotiate"))
145  {
146  auth_type |= CURLAUTH_GSSNEGOTIATE;
147  }
148  else
149  {
150  ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
151  }
152  }
153 
154  return auth_type;
155 }
156 
157 std::string CurlAuthData::auth_type_long2str(long auth_type)
158 {
159  std::list<std::string> auth_list;
160 
161  if(auth_type & CURLAUTH_GSSNEGOTIATE)
162  auth_list.push_back("negotiate");
163 
164  if(auth_type & CURLAUTH_NTLM)
165  auth_list.push_back("ntlm");
166 
167  if(auth_type & CURLAUTH_DIGEST)
168  auth_list.push_back("digest");
169 
170  if(auth_type & CURLAUTH_BASIC)
171  auth_list.push_back("basic");
172 
173  return str::join(auth_list, ",");
174 }
175 
176 
177 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
178 {
179  auth_data.dumpOn(str);
180  return str;
181 }
182 
183 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
184 {
185  auth_data.dumpOn(str);
186  return str;
187 }
188 
189 
190  } // namespace media
191 } // namespace zypp
void setLastDatabaseUpdate(time_t time)
time_t _lastChange
static const ViewOption WITH_USERNAME
Option to include username in the URL string.
Definition: UrlBase.h:58
std::string password() const
Definition: MediaUserAuth.h:57
#define _(MSG)
Definition: Gettext.h:37
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:776
String related utilities and Regular expression matching.
Convenient building of std::string with boost::format.
Definition: String.h:252
time_t lastDatabaseUpdate() const
Url::asString() view options.
Definition: UrlBase.h:39
virtual std::ostream & dumpOn(std::ostream &str) const
std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:531
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:492
Just inherits Exception to separate media exceptions.
virtual std::ostream & dumpOn(std::ostream &str) const
std::string username() const
Definition: MediaUserAuth.h:56
bool isValid() const
Verifies the Url.
Definition: Url.cc:484
CurlAuthData()
Default constructor.
Class for handling media authentication data.
Definition: MediaUserAuth.h:30
virtual bool valid() const
Checks validity of authentication data.
static std::string auth_type_long2str(long auth_type)
Converts a long of ORed CURLAUTH_* identifiers into a string of comma separated list of authenticatio...
virtual std::ostream & dumpAsIniOn(std::ostream &str) const
static const ViewOption WITH_PASSWORD
Option to include password in the URL string.
Definition: UrlBase.h:67
static long auth_type_str2long(std::string &auth_type_str)
Converts a string of comma separated list of authetication type names into a long of ORed CURLAUTH_* ...
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Curl HTTP authentication data.
Definition: MediaUserAuth.h:82
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
Definition: Url.cc:575
virtual bool valid() const
Checks validity of authentication data.
Convenience interface for handling authentication data of media user.
Url manipulation class.
Definition: Url.h:91
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
Definition: Url.cc:567