MediaUserAuth.cc

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

doxygen