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 namespace zypp {
00024   namespace media {
00025 
00026 
00027 bool AuthData::valid() const
00028 {
00029   return username().size() && password().size();
00030 }
00031 
00032 std::ostream & AuthData::dumpOn( std::ostream & str ) const
00033 {
00034   str << "username: '" << _username << "'" << std::endl
00035       << "password: " << (_password.empty() ? "<empty>" : "<non-empty>")
00036       << std::endl;
00037   return str;
00038 }
00039 
00040 
00041 bool CurlAuthData::valid() const
00042 {
00043   return username().size() && password().size() && _auth_type != CURLAUTH_NONE;
00044 }
00045 
00046 
00047 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
00048 {
00049   AuthData::dumpOn(str) << " auth_type: " << _auth_type_str
00050     << " (" << _auth_type << ")" << std::endl;
00051   return str;
00052 }
00053 
00054 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
00055 {
00056   curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
00057 
00058   std::vector<std::string>                  list;
00059   std::vector<std::string>::const_iterator  it;
00060   long                                      auth_type = CURLAUTH_NONE;
00061 
00062   zypp::str::split(auth_type_str, std::back_inserter(list), ",");
00063 
00064   for(it = list.begin(); it != list.end(); ++it)
00065   {
00066     if(*it == "basic")
00067     {
00068       auth_type |= CURLAUTH_BASIC;
00069     }
00070     else
00071     if(*it == "digest")
00072     {
00073       auth_type |= CURLAUTH_DIGEST;
00074     }
00075     else
00076     if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
00077        (*it == "ntlm"))
00078     {
00079       auth_type |= CURLAUTH_NTLM;
00080     }
00081     else
00082     if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
00083        (*it == "spnego" || *it == "negotiate"))
00084     {
00085       // there is no separate spnego flag for this auth type
00086       auth_type |= CURLAUTH_GSSNEGOTIATE;
00087     }
00088     else
00089     if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
00090        (*it == "gssnego" || *it == "negotiate"))
00091     {
00092       auth_type |= CURLAUTH_GSSNEGOTIATE;
00093     }
00094     else
00095     {
00096       std::string msg = boost::str(
00097         boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
00098 
00099       ZYPP_THROW(MediaException(msg));
00100     }
00101   }
00102 
00103   return auth_type;
00104 }
00105 
00106 std::string CurlAuthData::auth_type_long2str(long auth_type)
00107 {
00108   std::list<std::string> auth_list;
00109 
00110   if(auth_type & CURLAUTH_GSSNEGOTIATE)
00111     auth_list.push_back("negotiate");
00112 
00113   if(auth_type & CURLAUTH_NTLM)
00114     auth_list.push_back("ntlm");
00115 
00116   if(auth_type & CURLAUTH_DIGEST)
00117     auth_list.push_back("digest");
00118 
00119   if(auth_type & CURLAUTH_BASIC)
00120     auth_list.push_back("basic");
00121 
00122   return str::join(auth_list, ",");
00123 }
00124 
00125 
00126 std::ostream & operator << (std::ostream & str, AuthData & auth_data)
00127 {
00128   auth_data.dumpOn(str);
00129   return str;
00130 }
00131 
00132 std::ostream & operator << (std::ostream & str, CurlAuthData & auth_data)
00133 {
00134   auth_data.dumpOn(str);
00135   return str;
00136 }
00137 
00138 
00139   } // namespace media
00140 } // namespace zypp

Generated on Tue Sep 25 19:23:02 2007 for libzypp by  doxygen 1.5.3