pull: Add support for basic auth
This has been requested a few times for people delivering OSTree content and wanting to do access control.
This commit is contained in:
1 parent
437d2183a4
commit
36039759e2
4 files changed
+83
-3
No files matched your search
@@ -261,9 +261,8 @@ destroy_and_unref_source (GSource *source)
|
||||
}
|
||||
|
||||
static char *
|
||||
request_get_uri (FetcherRequest *req, guint idx)
|
||||
request_get_uri (FetcherRequest *req, SoupURI *baseuri)
|
||||
{
|
||||
SoupURI *baseuri = req->mirrorlist->pdata[idx];
|
||||
if (!req->filename)
|
||||
return soup_uri_to_string (baseuri, FALSE);
|
||||
{ g_autofree char *uristr = soup_uri_to_string (baseuri, FALSE);
|
||||
@@ -715,7 +714,8 @@ initiate_next_curl_request (FetcherRequest *req,
|
||||
|
||||
g_assert_cmpint (req->idx, <, req->mirrorlist->len);
|
||||
|
||||
{ g_autofree char *uri = request_get_uri (req, req->idx);
|
||||
SoupURI *baseuri = req->mirrorlist->pdata[req->idx];
|
||||
{ g_autofree char *uri = request_get_uri (req, baseuri);
|
||||
curl_easy_setopt (req->easy, CURLOPT_URL, uri);
|
||||
}
|
||||
|
||||
@@ -771,6 +771,12 @@ initiate_next_curl_request (FetcherRequest *req,
|
||||
if ((self->config_flags & OSTREE_FETCHER_FLAGS_TRANSFER_GZIP) > 0)
|
||||
curl_easy_setopt (req->easy, CURLOPT_ACCEPT_ENCODING, "");
|
||||
|
||||
/* If we have e.g. basic auth in the URL string, let's honor that */
|
||||
const char *username = soup_uri_get_user (baseuri);
|
||||
curl_easy_setopt (req->easy, CURLOPT_USERNAME, username);
|
||||
const char *password = soup_uri_get_password (baseuri);
|
||||
curl_easy_setopt (req->easy, CURLOPT_PASSWORD, password);
|
||||
|
||||
/* We should only speak HTTP; TODO: only enable file if specified */
|
||||
curl_easy_setopt (req->easy, CURLOPT_PROTOCOLS, (long)(CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FILE));
|
||||
/* Picked the current version in F25 as of 20170127, since
|
||||
|
||||
@@ -50,6 +50,7 @@ static int opt_random_408s_max = 100;
|
||||
static gint opt_port = 0;
|
||||
static gchar **opt_expected_cookies;
|
||||
static gchar **opt_expected_headers;
|
||||
static gboolean opt_require_basic_auth;
|
||||
|
||||
static guint emitted_random_500s_count = 0;
|
||||
static guint emitted_random_408s_count = 0;
|
||||
@@ -71,6 +72,7 @@ static GOptionEntry options[] = {
|
||||
{ "port", 'P', 0, G_OPTION_ARG_INT, &opt_port, "Use the specified TCP port", "PORT" },
|
||||
{ "port-file", 'p', 0, G_OPTION_ARG_FILENAME, &opt_port_file, "Write port number to PATH (- for standard output)", "PATH" },
|
||||
{ "force-range-requests", 0, 0, G_OPTION_ARG_NONE, &opt_force_ranges, "Force range requests by only serving half of files", NULL },
|
||||
{ "require-basic-auth", 0, 0, G_OPTION_ARG_NONE, &opt_require_basic_auth, "Require username foouser, password barpw", NULL },
|
||||
{ "random-500s", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_percentage, "Generate random HTTP 500 errors approximately for PERCENTAGE requests", "PERCENTAGE" },
|
||||
{ "random-500s-max", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_max, "Limit HTTP 500 errors to MAX (default 100)", "MAX" },
|
||||
{ "random-408s", 0, 0, G_OPTION_ARG_INT, &opt_random_408s_percentage, "Generate random HTTP 408 errors approximately for PERCENTAGE requests", "PERCENTAGE" },
|
||||
@@ -474,6 +476,13 @@ httpd_callback (SoupServer *server, SoupMessage *msg,
|
||||
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
basic_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
|
||||
const char *username, const char *password, gpointer data)
|
||||
{
|
||||
return g_str_equal (username, "foouser") && g_str_equal (password, "barpw");
|
||||
}
|
||||
|
||||
static void
|
||||
on_dir_changed (GFileMonitor *mon,
|
||||
GFile *file,
|
||||
@@ -571,6 +580,15 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error)
|
||||
SOUP_SERVER_SERVER_HEADER, "ostree-httpd ",
|
||||
NULL);
|
||||
#endif
|
||||
if (opt_require_basic_auth)
|
||||
{
|
||||
glnx_unref_object SoupAuthDomain *auth_domain =
|
||||
soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM, "auth-test",
|
||||
SOUP_AUTH_DOMAIN_ADD_PATH, "/",
|
||||
SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback,
|
||||
NULL);
|
||||
soup_server_add_auth_domain (server, auth_domain);
|
||||
}
|
||||
|
||||
soup_server_add_handler (server, NULL, httpd_callback, app, NULL);
|
||||
if (opt_port_file)
|
||||
|
||||
Reference in new issue
Block a user