libzypp: zypp-curl/auth/curlauthdata.cc Source File

libzypp 17.38.15

curlauthdata.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "curlauthdata.h"
14
15#include <zypp-core/base/Gettext.h>
16#include <zypp-core/base/String.h>
17#include <zypp-media/MediaException>
18
19#include <curl/curl.h>
20
21
22using std::endl;
23
24namespace zypp::media {
25
26 CurlAuthData::CurlAuthData()
27 : AuthData()
28 , _auth_type_str()
29 , _auth_type(CURLAUTH_NONE)
30 {}
31
32 CurlAuthData::CurlAuthData(const AuthData & authData)
33 : AuthData(authData)
34 , _auth_type_str()
35 , _auth_type(CURLAUTH_NONE)
36 {}
37
38 bool CurlAuthData::valid() const
39 {
40 return username().size() && password().size();
41 }
42
43 std::ostream & CurlAuthData::dumpOn( std::ostream & str ) const
44 {
45 AuthData::dumpOn(str) << endl
46 << " auth_type: " << _auth_type_str << " (" << _auth_type << ")";
47 return str;
48 }
49
50 long CurlAuthData::auth_type_str2long( std::string & auth_type_str )
51 {
52 return auth_type_str2long( const_cast< const std::string &>(auth_type_str) );
53 }
54
55 long CurlAuthData::auth_type_str2long( const std::string & auth_type_str )
56 {
57 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
58
59 std::vector<std::string> list;
60 std::vector<std::string>::const_iterator it;
61 long auth_type = CURLAUTH_NONE;
62
63 zypp::str::split(auth_type_str, std::back_inserter(list), ",");
64
65 for(it = list.begin(); it != list.end(); ++it)
66 {
67 if(*it == "basic")
68 {
69 auth_type |= CURLAUTH_BASIC;
70 }
71 else
72 if(*it == "digest")
73 {
74 auth_type |= CURLAUTH_DIGEST;
75 }
76 else
77 if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
78 (*it == "ntlm"))
79 {
80 auth_type |= CURLAUTH_NTLM;
81 }
82 else
83 if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
84 (*it == "spnego" || *it == "negotiate"))
85 {
86 // there is no separate spnego flag for this auth type
87 auth_type |= CURLAUTH_GSSNEGOTIATE;
88 }
89 else
90 if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
91 (*it == "gssnego" || *it == "negotiate"))
92 {
93 auth_type |= CURLAUTH_GSSNEGOTIATE;
94 }
95 else
96 {
97 ZYPP_THROW(MediaException(str::Format(_("Unsupported HTTP authentication method '%s'")) % *it));
98 }
99 }
100
101 return auth_type;
102 }
103
104 std::string CurlAuthData::auth_type_long2str(long auth_type)
105 {
106 std::list<std::string> auth_list;
107
108 if(auth_type & CURLAUTH_GSSNEGOTIATE)
109 auth_list.push_back("negotiate");
110
111 if(auth_type & CURLAUTH_NTLM)
112 auth_list.push_back("ntlm");
113
114 if(auth_type & CURLAUTH_DIGEST)
115 auth_list.push_back("digest");
116
117 if(auth_type & CURLAUTH_BASIC)
118 auth_list.push_back("basic");
119
120 return str::join(auth_list, ",");
121 }
122
123 std::ostream & operator << (std::ostream & str, const CurlAuthData & auth_data)
124 {
125 auth_data.dumpOn(str);
126 return str;
127 }
128
129}
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
String.h
zypp::media::AuthData::password
std::string password() const
Definition authdata.h:57
zypp::media::AuthData::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Definition authdata.cc:55
zypp::media::AuthData::AuthData
AuthData()
Definition authdata.h:33
zypp::media::AuthData::username
std::string username() const
Definition authdata.h:56
zypp::media::CurlAuthData
Curl HTTP authentication data.
Definition curlauthdata.h:22
zypp::media::CurlAuthData::dumpOn
std::ostream & dumpOn(std::ostream &str) const override
Definition curlauthdata.cc:43
zypp::media::CurlAuthData::auth_type_long2str
static std::string auth_type_long2str(long auth_type)
Converts a long of ORed CURLAUTH_* identifiers into a string of comma separated list of authenticatio...
Definition curlauthdata.cc:104
zypp::media::CurlAuthData::_auth_type_str
std::string _auth_type_str
Definition curlauthdata.h:98
zypp::media::CurlAuthData::auth_type_str2long
static long auth_type_str2long(std::string &auth_type_str)
Converts a string of comma separated list of authetication type names into a long of ORed CURLAUTH_* ...
Definition curlauthdata.cc:50
zypp::media::CurlAuthData::_auth_type
long _auth_type
Definition curlauthdata.h:99
zypp::media::CurlAuthData::CurlAuthData
CurlAuthData()
Default constructor.
Definition curlauthdata.cc:26
zypp::media::CurlAuthData::valid
bool valid() const override
Checks validity of authentication data.
Definition curlauthdata.cc:38
zypp::media::MediaException::MediaException
MediaException()
Ctor taking message.
Definition mediaexception.h:42
curlauthdata.h
str
String related utilities and Regular expression matching.
zypp::media
Definition MediaNetworkRequestExecutor.h:22
zypp::media::operator<<
std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
Definition MediaHandler.cc:1181
zypp::str::join
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition String.h:846
zypp::str::split
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition String.h:602
zypp::str::Format
Convenient building of std::string with boost::format.
Definition String.h:254