libzypp  14.43.0
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 <boost/format.hpp>
17 
18 #include "zypp/base/Gettext.h"
19 #include "zypp/base/String.h"
20 
23 
24 
25 using namespace std;
26 
27 namespace zypp {
28  namespace media {
29 
30 
31 AuthData::AuthData(const Url & url)
32  : _url(url)
33 {
34  _username = url.getUsername();
35  _password = url.getPassword();
36 }
37 
38 
39 bool AuthData::valid() const
40 {
41  return username().size() && password().size();
42 }
43 
44 std::ostream & AuthData::dumpOn( std::ostream & str ) const
45 {
46  if (_url.isValid())
48  else
49  str << "[<no-url>]" << endl;
50  str << "username: '" << _username << "'" << std::endl
51  << "password: " << (_password.empty() ? "<empty>" : "<non-empty>");
52  return str;
53 }
54 
55 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
56 {
57  if (_url.isValid())
58  str
59  << "[" << _url.asString(
63  << "]" << endl;
64 
65  str
66  << "username = " << _username << endl
67  << "password = " << _password << endl;
68 
69  return str;
70 }
71 
73  : AuthData()
74  , _auth_type_str()
75  , _auth_type(CURLAUTH_NONE)
76 {}
77 
79  : AuthData(authData)
80  , _auth_type_str()
81  , _auth_type(CURLAUTH_NONE)
82 {}
83 
84 bool CurlAuthData::valid() const
85 {
86  return username().size() && password().size();
87 }
88 
89 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
90 {
91  AuthData::dumpOn(str) << endl
92  << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
93  return str;
94 }
95 
96 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
97 {
98  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
99 
100  std::vector<std::string> list;
101  std::vector<std::string>::const_iterator it;
102  long auth_type = CURLAUTH_NONE;
103 
104  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
105 
106  for(it = list.begin(); it != list.end(); ++it)
107  {
108  if(*it == "basic")
109  {
110  auth_type |= CURLAUTH_BASIC;
111  }
112  else
113  if(*it == "digest")
114  {
115  auth_type |= CURLAUTH_DIGEST;
116  }
117  else
118  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
119  (*it == "ntlm"))
120  {
121  auth_type |= CURLAUTH_NTLM;
122  }
123  else
124  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
125  (*it == "spnego" || *it == "negotiate"))
126  {
127  // there is no separate spnego flag for this auth type
128  auth_type |= CURLAUTH_GSSNEGOTIATE;
129  }
130  else
131  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
132  (*it == "gssnego" || *it == "negotiate"))
133  {
134  auth_type |= CURLAUTH_GSSNEGOTIATE;
135  }
136  else
137  {
138  std::string msg = boost::str(
139  boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
140 
142  }
143  }
144 
145  return auth_type;
146 }
147 
148 std::string CurlAuthData::auth_type_long2str(long auth_type)
149 {
150  std::list<std::string> auth_list;
151 
152  if(auth_type & CURLAUTH_GSSNEGOTIATE)
153  auth_list.push_back("negotiate");
154 
155  if(auth_type & CURLAUTH_NTLM)
156  auth_list.push_back("ntlm");
157 
158  if(auth_type & CURLAUTH_DIGEST)
159  auth_list.push_back("digest");
160 
161  if(auth_type & CURLAUTH_BASIC)
162  auth_list.push_back("basic");
163 
164  return str::join(auth_list, ",");
165 }
166 
167 
168 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
169 {
170  auth_data.dumpOn(str);
171  return str;
172 }
173 
174 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
175 {
176  auth_data.dumpOn(str);
177  return str;
178 }
179 
180 
181  } // namespace media
182 } // namespace zypp
Interface to gettext.
static const ViewOption WITH_USERNAME
Option to include username in the URL string.
Definition: UrlBase.h:58
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:471
String related utilities and Regular expression matching.
virtual bool valid() const
Checks validity of authentication data.
Definition: Arch.h:339
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
Definition: Url.cc:566
bool isValid() const
Verifies the Url.
Definition: Url.cc:483
Url::asString() view options.
Definition: UrlBase.h:39
std::ostream & operator<<(std::ostream &str, const MediaAccess &obj)
Definition: MediaAccess.cc:482
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:491
std::string username() const
Definition: MediaUserAuth.h:56
std::string join(_Iterator begin, _Iterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:721
Just inherits Exception to separate media exceptions.
zypp::Url url
Definition: MediaCurl.cc:193
virtual std::ostream & dumpOn(std::ostream &str) const
#define _(MSG)
Definition: Gettext.h:29
virtual std::ostream & dumpAsIniOn(std::ostream &str) const
CurlAuthData()
Default constructor.
Class for handling media authentication data.
Definition: MediaUserAuth.h:30
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...
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_* ...
virtual std::ostream & dumpOn(std::ostream &str) const
virtual bool valid() const
Checks validity of authentication data.
std::string password() const
Definition: MediaUserAuth.h:57
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Curl HTTP authentication data.
Definition: MediaUserAuth.h:74
Convenience interface for handling authentication data of media user.
Url manipulation class.
Definition: Url.h:87
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
Definition: Url.cc:574