1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaUserAuth.cc
14 #include <curl/curl.h>
16 #include <zypp/base/Gettext.h>
17 #include <zypp/base/String.h>
19 #include <zypp/media/MediaException.h>
20 #include <zypp/media/MediaUserAuth.h>
28 AuthData::AuthData(const Url & url)
31 _username = url.getUsername();
32 _password = url.getPassword();
36 bool AuthData::valid() const
38 return username().size() && password().size();
41 std::ostream & AuthData::dumpOn( std::ostream & str ) const
44 str << "[" << _url.asString( url::ViewOptions() - url::ViewOptions::WITH_USERNAME - url::ViewOptions::WITH_PASSWORD ) << "]" << endl;
46 str << "[<no-url>]" << endl;
47 str << "username: '" << _username << "'" << std::endl
48 << "password: " << (_password.empty() ? "<empty>" : "<non-empty>");
52 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
56 << "[" << _url.asString(
58 - url::ViewOptions::WITH_USERNAME
59 - url::ViewOptions::WITH_PASSWORD)
63 << "username = " << _username << endl
64 << "password = " << _password << endl;
69 CurlAuthData::CurlAuthData()
72 , _auth_type(CURLAUTH_NONE)
75 CurlAuthData::CurlAuthData(const AuthData & authData)
78 , _auth_type(CURLAUTH_NONE)
81 bool CurlAuthData::valid() const
83 return username().size() && password().size();
86 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
88 AuthData::dumpOn(str) << endl
89 << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
93 long CurlAuthData::auth_type_str2long( std::string & auth_type_str )
95 return auth_type_str2long( const_cast< const std::string &>(auth_type_str) );
98 long CurlAuthData::auth_type_str2long( const std::string & auth_type_str )
100 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
102 std::vector<std::string> list;
103 std::vector<std::string>::const_iterator it;
104 long auth_type = CURLAUTH_NONE;
106 zypp::str::split(auth_type_str, std::back_inserter(list), ",");
108 for(it = list.begin(); it != list.end(); ++it)
112 auth_type |= CURLAUTH_BASIC;
117 auth_type |= CURLAUTH_DIGEST;
120 if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
123 auth_type |= CURLAUTH_NTLM;
126 if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
127 (*it == "spnego" || *it == "negotiate"))
129 // there is no separate spnego flag for this auth type
130 auth_type |= CURLAUTH_GSSNEGOTIATE;
133 if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
134 (*it == "gssnego" || *it == "negotiate"))
136 auth_type |= CURLAUTH_GSSNEGOTIATE;
140 ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
147 std::string CurlAuthData::auth_type_long2str(long auth_type)
149 std::list<std::string> auth_list;
151 if(auth_type & CURLAUTH_GSSNEGOTIATE)
152 auth_list.push_back("negotiate");
154 if(auth_type & CURLAUTH_NTLM)
155 auth_list.push_back("ntlm");
157 if(auth_type & CURLAUTH_DIGEST)
158 auth_list.push_back("digest");
160 if(auth_type & CURLAUTH_BASIC)
161 auth_list.push_back("basic");
163 return str::join(auth_list, ",");
167 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
169 auth_data.dumpOn(str);
173 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
175 auth_data.dumpOn(str);