libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00013 #include <list> 00014 #include <curl/curl.h> 00015 00016 #include <boost/format.hpp> 00017 00018 #include "zypp/base/Gettext.h" 00019 #include "zypp/base/String.h" 00020 00021 #include "zypp/media/MediaException.h" 00022 #include "zypp/media/MediaUserAuth.h" 00023 00024 00025 using namespace std; 00026 00027 namespace zypp { 00028 namespace media { 00029 00030 00031 AuthData::AuthData(const Url & url) 00032 : _url(url) 00033 { 00034 _username = url.getUsername(); 00035 _password = url.getPassword(); 00036 } 00037 00038 00039 bool AuthData::valid() const 00040 { 00041 return username().size() && password().size(); 00042 } 00043 00044 std::ostream & AuthData::dumpOn( std::ostream & str ) const 00045 { 00046 str << "username: '" << _username << "'" << std::endl 00047 << "password: " << (_password.empty() ? "<empty>" : "<non-empty>") 00048 << std::endl; 00049 return str; 00050 } 00051 00052 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const 00053 { 00054 if (_url.isValid()) 00055 str 00056 << "[" << _url.asString( 00057 url::ViewOptions() 00058 - url::ViewOptions::WITH_USERNAME 00059 - url::ViewOptions::WITH_PASSWORD) 00060 << "]" << endl; 00061 00062 str 00063 << "username = " << _username << endl 00064 << "password = " << _password << endl; 00065 00066 return str; 00067 } 00068 00069 CurlAuthData::CurlAuthData() 00070 : AuthData() 00071 , _auth_type_str() 00072 , _auth_type(CURLAUTH_NONE) 00073 {} 00074 00075 CurlAuthData::CurlAuthData(const AuthData & authData) 00076 : AuthData(authData) 00077 , _auth_type_str() 00078 , _auth_type(CURLAUTH_NONE) 00079 {} 00080 00081 bool CurlAuthData::valid() const 00082 { 00083 return username().size() && password().size(); 00084 } 00085 00086 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const 00087 { 00088 AuthData::dumpOn(str) << " auth_type: " << _auth_type_str 00089 << " (" << _auth_type << ")" << std::endl; 00090 return str; 00091 } 00092 00093 long CurlAuthData::auth_type_str2long(std::string & auth_type_str) 00094 { 00095 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW); 00096 00097 std::vector<std::string> list; 00098 std::vector<std::string>::const_iterator it; 00099 long auth_type = CURLAUTH_NONE; 00100 00101 zypp::str::split(auth_type_str, std::back_inserter(list), ","); 00102 00103 for(it = list.begin(); it != list.end(); ++it) 00104 { 00105 if(*it == "basic") 00106 { 00107 auth_type |= CURLAUTH_BASIC; 00108 } 00109 else 00110 if(*it == "digest") 00111 { 00112 auth_type |= CURLAUTH_DIGEST; 00113 } 00114 else 00115 if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) && 00116 (*it == "ntlm")) 00117 { 00118 auth_type |= CURLAUTH_NTLM; 00119 } 00120 else 00121 if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) && 00122 (*it == "spnego" || *it == "negotiate")) 00123 { 00124 // there is no separate spnego flag for this auth type 00125 auth_type |= CURLAUTH_GSSNEGOTIATE; 00126 } 00127 else 00128 if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) && 00129 (*it == "gssnego" || *it == "negotiate")) 00130 { 00131 auth_type |= CURLAUTH_GSSNEGOTIATE; 00132 } 00133 else 00134 { 00135 std::string msg = boost::str( 00136 boost::format (_("Unsupported HTTP authentication method '%s'")) % *it); 00137 00138 ZYPP_THROW(MediaException(msg)); 00139 } 00140 } 00141 00142 return auth_type; 00143 } 00144 00145 std::string CurlAuthData::auth_type_long2str(long auth_type) 00146 { 00147 std::list<std::string> auth_list; 00148 00149 if(auth_type & CURLAUTH_GSSNEGOTIATE) 00150 auth_list.push_back("negotiate"); 00151 00152 if(auth_type & CURLAUTH_NTLM) 00153 auth_list.push_back("ntlm"); 00154 00155 if(auth_type & CURLAUTH_DIGEST) 00156 auth_list.push_back("digest"); 00157 00158 if(auth_type & CURLAUTH_BASIC) 00159 auth_list.push_back("basic"); 00160 00161 return str::join(auth_list, ","); 00162 } 00163 00164 00165 std::ostream & operator << (std::ostream & str, const AuthData & auth_data) 00166 { 00167 auth_data.dumpOn(str); 00168 return str; 00169 } 00170 00171 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data) 00172 { 00173 auth_data.dumpOn(str); 00174 return str; 00175 } 00176 00177 00178 } // namespace media 00179 } // namespace zypp