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