- support for HTTP authentication prompt added (#190609)
[platform/upstream/libzypp.git] / zypp / media / MediaUserAuth.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaUserAuth.cc
10  *
11  */
12
13 #include <list>
14 #include <boost/format.hpp>
15
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/String.h"
18
19 #include "zypp/media/MediaException.h"
20 #include "zypp/media/MediaUserAuth.h"
21
22
23 namespace zypp {
24   namespace media {
25
26
27 bool AuthData::valid() const
28 {
29   return username().size() && password().size();
30 }
31
32 std::ostream & AuthData::dumpOn( std::ostream & str ) const
33 {
34   str << "username: '" << _username << "'" << std::endl
35       << "password: " << (_password.empty() ? "<empty>" : "<non-empty>")
36       << std::endl;
37   return str;
38 }
39
40
41 bool CurlAuthData::valid() const
42 {
43   return username().size() && password().size() && _auth_type != CURLAUTH_NONE;
44 }
45
46
47 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
48 {
49   AuthData::dumpOn(str) << " auth_type: " << _auth_type_str
50     << " (" << _auth_type << ")" << std::endl;
51   return str;
52 }
53
54 long CurlAuthData::auth_type_str2long(std::string & auth_type_str)
55 {
56   curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
57
58   std::vector<std::string>                  list;
59   std::vector<std::string>::const_iterator  it;
60   long                                      auth_type = CURLAUTH_NONE;
61
62   zypp::str::split(auth_type_str, std::back_inserter(list), ",");
63
64   for(it = list.begin(); it != list.end(); ++it)
65   {
66     if(*it == "basic")
67     {
68       auth_type |= CURLAUTH_BASIC;
69     }
70     else
71     if(*it == "digest")
72     {
73       auth_type |= CURLAUTH_DIGEST;
74     }
75     else
76     if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
77        (*it == "ntlm"))
78     {
79       auth_type |= CURLAUTH_NTLM;
80     }
81     else
82     if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
83        (*it == "spnego" || *it == "negotiate"))
84     {
85       // there is no separate spnego flag for this auth type
86       auth_type |= CURLAUTH_GSSNEGOTIATE;
87     }
88     else
89     if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
90        (*it == "gssnego" || *it == "negotiate"))
91     {
92       auth_type |= CURLAUTH_GSSNEGOTIATE;
93     }
94     else
95     {
96       std::string msg = boost::str(
97         boost::format (_("Unsupported HTTP authentication method '%s'")) % *it);
98
99       ZYPP_THROW(MediaException(msg));
100     }
101   }
102
103   return auth_type;
104 }
105
106 std::string CurlAuthData::auth_type_long2str(long auth_type)
107 {
108   std::list<std::string> auth_list;
109
110   if(auth_type & CURLAUTH_GSSNEGOTIATE)
111     auth_list.push_back("negotiate");
112
113   if(auth_type & CURLAUTH_NTLM)
114     auth_list.push_back("ntlm");
115
116   if(auth_type & CURLAUTH_DIGEST)
117     auth_list.push_back("digest");
118
119   if(auth_type & CURLAUTH_BASIC)
120     auth_list.push_back("basic");
121
122   return str::join(auth_list, ",");
123 }
124
125
126 std::ostream & operator << (std::ostream & str, AuthData & auth_data)
127 {
128   auth_data.dumpOn(str);
129   return str;
130 }
131
132 std::ostream & operator << (std::ostream & str, CurlAuthData & auth_data)
133 {
134   auth_data.dumpOn(str);
135   return str;
136 }
137
138
139   } // namespace media
140 } // namespace zypp