libzypp  13.10.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 <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  str << "username: '" << _username << "'" << std::endl
47  << "password: " << (_password.empty() ? "<empty>" : "<non-empty>")
48  << std::endl;
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) << " auth_type: " << _auth_type_str
89  << " (" << _auth_type << ")" << std::endl;
90  return str;
91 }
92 
93 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
94 {
95  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
96 
97  std::vector<std::string> list;
98  std::vector<std::string>::const_iterator it;
99  long auth_type = CURLAUTH_NONE;
100 
101  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
102 
103  for(it = list.begin(); it != list.end(); ++it)
104  {
105  if(*it == "basic")
106  {
107  auth_type |= CURLAUTH_BASIC;
108  }
109  else
110  if(*it == "digest")
111  {
112  auth_type |= CURLAUTH_DIGEST;
113  }
114  else
115  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
116  (*it == "ntlm"))
117  {
118  auth_type |= CURLAUTH_NTLM;
119  }
120  else
121  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
122  (*it == "spnego" || *it == "negotiate"))
123  {
124  // there is no separate spnego flag for this auth type
125  auth_type |= CURLAUTH_GSSNEGOTIATE;
126  }
127  else
128  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
129  (*it == "gssnego" || *it == "negotiate"))
130  {
131  auth_type |= CURLAUTH_GSSNEGOTIATE;
132  }
133  else
134  {
135  std::string msg = boost::str(
136  boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
137 
139  }
140  }
141 
142  return auth_type;
143 }
144 
145 std::string CurlAuthData::auth_type_long2str(long auth_type)
146 {
147  std::list<std::string> auth_list;
148 
149  if(auth_type & CURLAUTH_GSSNEGOTIATE)
150  auth_list.push_back("negotiate");
151 
152  if(auth_type & CURLAUTH_NTLM)
153  auth_list.push_back("ntlm");
154 
155  if(auth_type & CURLAUTH_DIGEST)
156  auth_list.push_back("digest");
157 
158  if(auth_type & CURLAUTH_BASIC)
159  auth_list.push_back("basic");
160 
161  return str::join(auth_list, ",");
162 }
163 
164 
165 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
166 {
167  auth_data.dumpOn(str);
168  return str;
169 }
170 
171 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
172 {
173  auth_data.dumpOn(str);
174  return str;
175 }
176 
177 
178  } // namespace media
179 } // 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:394
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
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:635
Just inherits Exception to separate media exceptions.
zypp::Url url
Definition: MediaCurl.cc:193
virtual std::ostream & dumpOn(std::ostream &str) const
#define _(MSG)
Return translated text.
Definition: Gettext.h:21
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