libzypp  15.28.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 
23 using namespace std;
24 
25 namespace zypp {
26  namespace media {
27 
28 
29 AuthData::AuthData(const Url & url)
30  : _url(url)
31 {
32  _username = url.getUsername();
33  _password = url.getPassword();
34 }
35 
36 
37 bool AuthData::valid() const
38 {
39  return username().size() && password().size();
40 }
41 
42 std::ostream & AuthData::dumpOn( std::ostream & str ) const
43 {
44  if (_url.isValid())
46  else
47  str << "[<no-url>]" << endl;
48  str << "username: '" << _username << "'" << std::endl
49  << "password: " << (_password.empty() ? "<empty>" : "<non-empty>");
50  return str;
51 }
52 
53 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
54 {
55  if (_url.isValid())
56  str
57  << "[" << _url.asString(
61  << "]" << endl;
62 
63  str
64  << "username = " << _username << endl
65  << "password = " << _password << endl;
66 
67  return str;
68 }
69 
71  : AuthData()
72  , _auth_type_str()
73  , _auth_type(CURLAUTH_NONE)
74 {}
75 
77  : AuthData(authData)
78  , _auth_type_str()
79  , _auth_type(CURLAUTH_NONE)
80 {}
81 
82 bool CurlAuthData::valid() const
83 {
84  return username().size() && password().size();
85 }
86 
87 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
88 {
89  AuthData::dumpOn(str) << endl
90  << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
91  return str;
92 }
93 
94 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
95 {
96  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
97 
98  std::vector<std::string> list;
99  std::vector<std::string>::const_iterator it;
100  long auth_type = CURLAUTH_NONE;
101 
102  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
103 
104  for(it = list.begin(); it != list.end(); ++it)
105  {
106  if(*it == "basic")
107  {
108  auth_type |= CURLAUTH_BASIC;
109  }
110  else
111  if(*it == "digest")
112  {
113  auth_type |= CURLAUTH_DIGEST;
114  }
115  else
116  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
117  (*it == "ntlm"))
118  {
119  auth_type |= CURLAUTH_NTLM;
120  }
121  else
122  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
123  (*it == "spnego" || *it == "negotiate"))
124  {
125  // there is no separate spnego flag for this auth type
126  auth_type |= CURLAUTH_GSSNEGOTIATE;
127  }
128  else
129  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
130  (*it == "gssnego" || *it == "negotiate"))
131  {
132  auth_type |= CURLAUTH_GSSNEGOTIATE;
133  }
134  else
135  {
136  ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
137  }
138  }
139 
140  return auth_type;
141 }
142 
143 std::string CurlAuthData::auth_type_long2str(long auth_type)
144 {
145  std::list<std::string> auth_list;
146 
147  if(auth_type & CURLAUTH_GSSNEGOTIATE)
148  auth_list.push_back("negotiate");
149 
150  if(auth_type & CURLAUTH_NTLM)
151  auth_list.push_back("ntlm");
152 
153  if(auth_type & CURLAUTH_DIGEST)
154  auth_list.push_back("digest");
155 
156  if(auth_type & CURLAUTH_BASIC)
157  auth_list.push_back("basic");
158 
159  return str::join(auth_list, ",");
160 }
161 
162 
163 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
164 {
165  auth_data.dumpOn(str);
166  return str;
167 }
168 
169 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
170 {
171  auth_data.dumpOn(str);
172  return str;
173 }
174 
175 
176  } // namespace media
177 } // 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:321
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:758
virtual bool valid() const
Checks validity of authentication data.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
Definition: Url.cc:566
Url url
Definition: MediaCurl.cc:180
Convenient building of std::string with boost::format.
Definition: String.h:247
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
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:518
std::string username() const
Definition: MediaUserAuth.h:56
Just inherits Exception to separate media exceptions.
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
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