libzypp  15.21.1
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  str << "username: '" << _username << "'" << std::endl
45  << "password: " << (_password.empty() ? "<empty>" : "<non-empty>")
46  << std::endl;
47  return str;
48 }
49 
50 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
51 {
52  if (_url.isValid())
53  str
54  << "[" << _url.asString(
58  << "]" << endl;
59 
60  str
61  << "username = " << _username << endl
62  << "password = " << _password << endl;
63 
64  return str;
65 }
66 
68  : AuthData()
69  , _auth_type_str()
70  , _auth_type(CURLAUTH_NONE)
71 {}
72 
74  : AuthData(authData)
75  , _auth_type_str()
76  , _auth_type(CURLAUTH_NONE)
77 {}
78 
79 bool CurlAuthData::valid() const
80 {
81  return username().size() && password().size();
82 }
83 
84 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
85 {
86  AuthData::dumpOn(str) << " auth_type: " << _auth_type_str
87  << " (" << _auth_type << ")" << std::endl;
88  return str;
89 }
90 
91 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
92 {
93  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
94 
95  std::vector<std::string> list;
96  std::vector<std::string>::const_iterator it;
97  long auth_type = CURLAUTH_NONE;
98 
99  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
100 
101  for(it = list.begin(); it != list.end(); ++it)
102  {
103  if(*it == "basic")
104  {
105  auth_type |= CURLAUTH_BASIC;
106  }
107  else
108  if(*it == "digest")
109  {
110  auth_type |= CURLAUTH_DIGEST;
111  }
112  else
113  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
114  (*it == "ntlm"))
115  {
116  auth_type |= CURLAUTH_NTLM;
117  }
118  else
119  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
120  (*it == "spnego" || *it == "negotiate"))
121  {
122  // there is no separate spnego flag for this auth type
123  auth_type |= CURLAUTH_GSSNEGOTIATE;
124  }
125  else
126  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
127  (*it == "gssnego" || *it == "negotiate"))
128  {
129  auth_type |= CURLAUTH_GSSNEGOTIATE;
130  }
131  else
132  {
133  ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
134  }
135  }
136 
137  return auth_type;
138 }
139 
140 std::string CurlAuthData::auth_type_long2str(long auth_type)
141 {
142  std::list<std::string> auth_list;
143 
144  if(auth_type & CURLAUTH_GSSNEGOTIATE)
145  auth_list.push_back("negotiate");
146 
147  if(auth_type & CURLAUTH_NTLM)
148  auth_list.push_back("ntlm");
149 
150  if(auth_type & CURLAUTH_DIGEST)
151  auth_list.push_back("digest");
152 
153  if(auth_type & CURLAUTH_BASIC)
154  auth_list.push_back("basic");
155 
156  return str::join(auth_list, ",");
157 }
158 
159 
160 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
161 {
162  auth_data.dumpOn(str);
163  return str;
164 }
165 
166 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
167 {
168  auth_data.dumpOn(str);
169  return str;
170 }
171 
172 
173  } // namespace media
174 } // 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
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.
zypp::Url url
Definition: MediaCurl.cc:192
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