libzypp  17.31.31
curlauthdata.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include "curlauthdata.h"
14 
15 #include <zypp-core/base/Gettext.h>
16 #include <zypp-core/base/String.h>
17 #include <zypp-media/MediaException>
18 
19 #include <curl/curl.h>
20 
21 
22 using std::endl;
23 
24 namespace zypp::media {
25 
27  : AuthData()
28  , _auth_type_str()
29  , _auth_type(CURLAUTH_NONE)
30  {}
31 
33  : AuthData(authData)
34  , _auth_type_str()
35  , _auth_type(CURLAUTH_NONE)
36  {}
37 
38  bool CurlAuthData::valid() const
39  {
40  return username().size() && password().size();
41  }
42 
43  std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
44  {
45  AuthData::dumpOn(str) << endl
46  << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
47  return str;
48  }
49 
50  long CurlAuthData::auth_type_str2long( std::string & auth_type_str )
51  {
52  return auth_type_str2long( const_cast< const std::string &>(auth_type_str) );
53  }
54 
55  long CurlAuthData::auth_type_str2long( const std::string & auth_type_str )
56  {
57  curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
58 
59  std::vector<std::string> list;
60  std::vector<std::string>::const_iterator it;
61  long auth_type = CURLAUTH_NONE;
62 
63  zypp::str::split(auth_type_str, std::back_inserter(list), ",");
64 
65  for(it = list.begin(); it != list.end(); ++it)
66  {
67  if(*it == "basic")
68  {
69  auth_type |= CURLAUTH_BASIC;
70  }
71  else
72  if(*it == "digest")
73  {
74  auth_type |= CURLAUTH_DIGEST;
75  }
76  else
77  if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
78  (*it == "ntlm"))
79  {
80  auth_type |= CURLAUTH_NTLM;
81  }
82  else
83  if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
84  (*it == "spnego" || *it == "negotiate"))
85  {
86  // there is no separate spnego flag for this auth type
87  auth_type |= CURLAUTH_GSSNEGOTIATE;
88  }
89  else
90  if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
91  (*it == "gssnego" || *it == "negotiate"))
92  {
93  auth_type |= CURLAUTH_GSSNEGOTIATE;
94  }
95  else
96  {
97  ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
98  }
99  }
100 
101  return auth_type;
102  }
103 
104  std::string CurlAuthData::auth_type_long2str(long auth_type)
105  {
106  std::list<std::string> auth_list;
107 
108  if(auth_type & CURLAUTH_GSSNEGOTIATE)
109  auth_list.push_back("negotiate");
110 
111  if(auth_type & CURLAUTH_NTLM)
112  auth_list.push_back("ntlm");
113 
114  if(auth_type & CURLAUTH_DIGEST)
115  auth_list.push_back("digest");
116 
117  if(auth_type & CURLAUTH_BASIC)
118  auth_list.push_back("basic");
119 
120  return str::join(auth_list, ",");
121  }
122 
123  std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
124  {
125  auth_data.dumpOn(str);
126  return str;
127  }
128 
129 }
std::string password() const
Definition: authdata.h:55
std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
#define _(MSG)
Definition: Gettext.h:37
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:776
String related utilities and Regular expression matching.
Convenient building of std::string with boost::format.
Definition: String.h:252
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: authdata.cc:55
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:531
Just inherits Exception to separate media exceptions.
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: curlauthdata.cc:43
std::string username() const
Definition: authdata.h:54
CurlAuthData()
Default constructor.
Definition: curlauthdata.cc:26
Class for handling media authentication data.
Definition: authdata.h:28
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 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: curlauthdata.cc:50
Curl HTTP authentication data.
Definition: curlauthdata.h:22
virtual bool valid() const
Checks validity of authentication data.
Definition: curlauthdata.cc:38