libzypp 9.41.1

MediaUserAuth.cc

Go to the documentation of this file.
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   if (_url.isValid())
00047     str << "[" << _url.asString( url::ViewOptions() - url::ViewOptions::WITH_USERNAME - url::ViewOptions::WITH_PASSWORD ) << "]" << endl;
00048   else
00049     str << "[<no-url>]" << endl;
00050   str << "username: '" << _username << "'" << std::endl
00051       << "password: " << (_password.empty() ? "<empty>" : "<non-empty>");
00052   return str;
00053 }
00054 
00055 std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
00056 {
00057   if (_url.isValid())
00058     str
00059       << "[" << _url.asString(
00060         url::ViewOptions()
00061         - url::ViewOptions::WITH_USERNAME
00062         - url::ViewOptions::WITH_PASSWORD)
00063       << "]" << endl;
00064 
00065   str
00066     << "username = " << _username << endl
00067     << "password = " << _password << endl;
00068 
00069   return str;
00070 }
00071 
00072 CurlAuthData::CurlAuthData()
00073   : AuthData()
00074   , _auth_type_str()
00075   , _auth_type(CURLAUTH_NONE)
00076 {}
00077 
00078 CurlAuthData::CurlAuthData(const AuthData & authData)
00079   : AuthData(authData)
00080   , _auth_type_str()
00081   , _auth_type(CURLAUTH_NONE)
00082 {}
00083 
00084 bool CurlAuthData::valid() const
00085 {
00086   return username().size() && password().size();
00087 }
00088 
00089 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
00090 {
00091   AuthData::dumpOn(str) << endl
00092   << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
00093   return str;
00094 }
00095 
00096 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
00097 {
00098   curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
00099 
00100   std::vector<std::string>                  list;
00101   std::vector<std::string>::const_iterator  it;
00102   long                                      auth_type = CURLAUTH_NONE;
00103 
00104   zypp::str::split(auth_type_str, std::back_inserter(list), ",");
00105 
00106   for(it = list.begin(); it != list.end(); ++it)
00107   {
00108     if(*it == "basic")
00109     {
00110       auth_type |= CURLAUTH_BASIC;
00111     }
00112     else
00113     if(*it == "digest")
00114     {
00115       auth_type |= CURLAUTH_DIGEST;
00116     }
00117     else
00118     if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
00119        (*it == "ntlm"))
00120     {
00121       auth_type |= CURLAUTH_NTLM;
00122     }
00123     else
00124     if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
00125        (*it == "spnego" || *it == "negotiate"))
00126     {
00127       // there is no separate spnego flag for this auth type
00128       auth_type |= CURLAUTH_GSSNEGOTIATE;
00129     }
00130     else
00131     if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
00132        (*it == "gssnego" || *it == "negotiate"))
00133     {
00134       auth_type |= CURLAUTH_GSSNEGOTIATE;
00135     }
00136     else
00137     {
00138       std::string msg = boost::str(
00139         boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
00140 
00141       ZYPP_THROW(MediaException(msg));
00142     }
00143   }
00144 
00145   return auth_type;
00146 }
00147 
00148 std::string CurlAuthData::auth_type_long2str(long auth_type)
00149 {
00150   std::list<std::string> auth_list;
00151 
00152   if(auth_type & CURLAUTH_GSSNEGOTIATE)
00153     auth_list.push_back("negotiate");
00154 
00155   if(auth_type & CURLAUTH_NTLM)
00156     auth_list.push_back("ntlm");
00157 
00158   if(auth_type & CURLAUTH_DIGEST)
00159     auth_list.push_back("digest");
00160 
00161   if(auth_type & CURLAUTH_BASIC)
00162     auth_list.push_back("basic");
00163 
00164   return str::join(auth_list, ",");
00165 }
00166 
00167 
00168 std::ostream & operator << (std::ostream & str, const AuthData & auth_data)
00169 {
00170   auth_data.dumpOn(str);
00171   return str;
00172 }
00173 
00174 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
00175 {
00176   auth_data.dumpOn(str);
00177   return str;
00178 }
00179 
00180 
00181   } // namespace media
00182 } // namespace zypp