1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaUserAuth.cc
14 #include <curl/curl.h>
16 #include <boost/format.hpp>
18 #include "zypp/base/Gettext.h"
19 #include "zypp/base/String.h"
21 #include "zypp/media/MediaException.h"
22 #include "zypp/media/MediaUserAuth.h"
31 AuthData::AuthData(const Url & url)
34 _username = url.getUsername();
35 _password = url.getPassword();
39 bool AuthData::valid() const
41 return username().size() && password().size();
44 std::ostream & AuthData::dumpOn( std::ostream & str ) const
46 str << "username: '" << _username << "'" << std::endl
47 << "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) << " auth_type: " << _auth_type_str
89 << " (" << _auth_type << ")" << std::endl;
93 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
95 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
97 std::vector<std::string> list;
98 std::vector<std::string>::const_iterator it;
99 long auth_type = CURLAUTH_NONE;
101 zypp::str::split(auth_type_str, std::back_inserter(list), ",");
103 for(it = list.begin(); it != list.end(); ++it)
107 auth_type |= CURLAUTH_BASIC;
112 auth_type |= CURLAUTH_DIGEST;
115 if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
118 auth_type |= CURLAUTH_NTLM;
121 if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
122 (*it == "spnego" || *it == "negotiate"))
124 // there is no separate spnego flag for this auth type
125 auth_type |= CURLAUTH_GSSNEGOTIATE;
128 if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
129 (*it == "gssnego" || *it == "negotiate"))
131 auth_type |= CURLAUTH_GSSNEGOTIATE;
135 std::string msg = boost::str(
136 boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
138 ZYPP_THROW(MediaException(msg));
145 std::string CurlAuthData::auth_type_long2str(long auth_type)
147 std::list<std::string> auth_list;
149 if(auth_type & CURLAUTH_GSSNEGOTIATE)
150 auth_list.push_back("negotiate");
152 if(auth_type & CURLAUTH_NTLM)
153 auth_list.push_back("ntlm");
155 if(auth_type & CURLAUTH_DIGEST)
156 auth_list.push_back("digest");
158 if(auth_type & CURLAUTH_BASIC)
159 auth_list.push_back("basic");
161 return str::join(auth_list, ",");
165 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
167 auth_data.dumpOn(str);
171 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
173 auth_data.dumpOn(str);