22 #include <curl/curl.h>
26 #include <boost/program_options.hpp>
28 #include <drizzled/item.h>
30 #include <drizzled/identifier.h>
31 #include <drizzled/plugin/authentication.h>
32 #include <drizzled/gettext.h>
33 namespace po= boost::program_options;
34 using namespace drizzled;
40 static size_t curl_cb_read(
void *ptr,
size_t size,
size_t nmemb,
void *stream)
44 return (size * nmemb);
52 std::string sysvar_auth_url;
54 Auth_http(std::string name_arg,
const std::string &url_arg) :
56 sysvar_auth_url(url_arg)
61 curl_handle= curl_easy_init();
64 rv= curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
65 rv= curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
66 rv= curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
69 rv= curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
72 rv= curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, curl_cb_read);
75 std::string& getAuthURL()
77 return sysvar_auth_url;
80 bool setAuthURL(std::string& new_auth_url)
82 if (new_auth_url.empty())
84 errmsg_printf(error::ERROR, _(
"auth url cannot be an empty string"));
87 sysvar_auth_url= new_auth_url;
93 curl_easy_cleanup(curl_handle);
94 curl_global_cleanup();
97 virtual bool authenticate(
const identifier::User &sctx,
const string &password)
99 long http_response_code;
101 assert(sctx.username().c_str());
104 rv= curl_easy_setopt(curl_handle, CURLOPT_URL, sysvar_auth_url.c_str());
105 #if defined(HAVE_CURLOPT_USERNAME)
107 rv= curl_easy_setopt(curl_handle, CURLOPT_USERNAME,
108 sctx.username().c_str());
109 rv= curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
113 string userpwd(sctx.username());
115 userpwd.append(password);
116 rv= curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd.c_str());
121 rv= curl_easy_perform(curl_handle);
124 rv= curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_response_code);
132 if (http_response_code == 401)
149 std::string new_auth_url(var->value->
str_value.data());
150 if (auth->setAuthURL(new_auth_url))
155 errmsg_printf(error::ERROR, _(
"auth_http url cannot be NULL"));
169 if (curl_global_init(CURL_GLOBAL_NOTHING) != 0)
172 const string auth_url(vm[
"url"].as<string>());
173 if (auth_url.size() == 0)
175 errmsg_printf(error::ERROR,
176 _(
"auth_http plugin loaded but required option url not "
177 "specified. Against which URL are you intending on "
178 "authenticating?\n"));
182 auth=
new Auth_http(
"auth_http", auth_url);
184 context.registerVariable(
new sys_var_std_string(
"url", auth->getAuthURL(), NULL, &updateAuthURL));
191 context(
"url", po::value<string>()->default_value(
""),
192 N_(
"URL for HTTP Auth check"));
197 DRIZZLE_DECLARE_PLUGIN
203 N_(
"Authenication against a web server using HTTP"),
205 auth_http::initialize,
207 auth_http::init_options
209 DRIZZLE_DECLARE_PLUGIN_END;