1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-auth-domain-basic.c: HTTP Basic Authentication (server-side)
5 * Copyright (C) 2007 Novell, Inc.
14 #include "soup-auth-domain-basic.h"
18 * SECTION:soup-auth-domain-basic
19 * @short_description: Server-side "Basic" authentication
21 * #SoupAuthDomainBasic handles the server side of HTTP "Basic" (ie,
22 * cleartext password) authentication.
35 SoupAuthDomainBasicAuthCallback auth_callback;
37 GDestroyNotify auth_dnotify;
38 } SoupAuthDomainBasicPrivate;
40 #define SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_AUTH_DOMAIN_BASIC, SoupAuthDomainBasicPrivate))
42 G_DEFINE_TYPE (SoupAuthDomainBasic, soup_auth_domain_basic, SOUP_TYPE_AUTH_DOMAIN)
45 soup_auth_domain_basic_init (SoupAuthDomainBasic *basic)
50 soup_auth_domain_basic_finalize (GObject *object)
52 SoupAuthDomainBasicPrivate *priv =
53 SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE (object);
55 if (priv->auth_dnotify)
56 priv->auth_dnotify (priv->auth_data);
58 G_OBJECT_CLASS (soup_auth_domain_basic_parent_class)->finalize (object);
62 soup_auth_domain_basic_set_property (GObject *object, guint prop_id,
63 const GValue *value, GParamSpec *pspec)
65 SoupAuthDomainBasicPrivate *priv =
66 SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE (object);
69 case PROP_AUTH_CALLBACK:
70 priv->auth_callback = g_value_get_pointer (value);
73 if (priv->auth_dnotify) {
74 priv->auth_dnotify (priv->auth_data);
75 priv->auth_dnotify = NULL;
77 priv->auth_data = g_value_get_pointer (value);
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 soup_auth_domain_basic_get_property (GObject *object, guint prop_id,
87 GValue *value, GParamSpec *pspec)
89 SoupAuthDomainBasicPrivate *priv =
90 SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE (object);
93 case PROP_AUTH_CALLBACK:
94 g_value_set_pointer (value, priv->auth_callback);
97 g_value_set_pointer (value, priv->auth_data);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 * soup_auth_domain_basic_new:
107 * @optname1: name of first option, or %NULL
108 * @...: option name/value pairs
110 * Creates a #SoupAuthDomainBasic. You must set the
111 * %SOUP_AUTH_DOMAIN_REALM parameter, to indicate the realm name to be
112 * returned with the authentication challenge to the client. Other
113 * parameters are optional.
115 * Return value: the new #SoupAuthDomain
118 soup_auth_domain_basic_new (const char *optname1, ...)
120 SoupAuthDomain *domain;
123 va_start (ap, optname1);
124 domain = (SoupAuthDomain *)g_object_new_valist (SOUP_TYPE_AUTH_DOMAIN_BASIC,
128 g_return_val_if_fail (soup_auth_domain_get_realm (domain) != NULL, NULL);
134 * SoupAuthDomainBasicAuthCallback:
135 * @domain: the domain
136 * @msg: the message being authenticated
137 * @username: the username provided by the client
138 * @password: the password provided by the client
139 * @user_data: the data passed to soup_auth_domain_basic_set_auth_callback()
141 * Callback used by #SoupAuthDomainBasic for authentication purposes.
142 * The application should verify that @username and @password and valid
143 * and return %TRUE or %FALSE.
145 * If you are maintaining your own password database (rather than
146 * using the password to authenticate against some other system like
147 * PAM or a remote server), you should make sure you know what you are
148 * doing. In particular, don't store cleartext passwords, or
149 * easily-computed hashes of cleartext passwords, even if you don't
150 * care that much about the security of your server, because users
151 * will frequently use the same password for multiple sites, and so
152 * compromising any site with a cleartext (or easily-cracked) password
153 * database may give attackers access to other more-interesting sites
156 * Return value: %TRUE if @username and @password are valid
160 * soup_auth_domain_basic_set_auth_callback:
161 * @domain: the domain
162 * @callback: the callback
163 * @user_data: data to pass to @auth_callback
164 * @dnotify: destroy notifier to free @user_data when @domain
167 * Sets the callback that @domain will use to authenticate incoming
168 * requests. For each request containing authorization, @domain will
169 * invoke the callback, and then either accept or reject the request
170 * based on @callback's return value.
172 * You can also set the auth callback by setting the
173 * %SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK and
174 * %SOUP_AUTH_DOMAIN_BASIC_AUTH_DATA properties, which can also be
175 * used to set the callback at construct time.
178 soup_auth_domain_basic_set_auth_callback (SoupAuthDomain *domain,
179 SoupAuthDomainBasicAuthCallback callback,
181 GDestroyNotify dnotify)
183 SoupAuthDomainBasicPrivate *priv =
184 SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE (domain);
186 if (priv->auth_dnotify)
187 priv->auth_dnotify (priv->auth_data);
189 priv->auth_callback = callback;
190 priv->auth_data = user_data;
191 priv->auth_dnotify = dnotify;
193 g_object_notify (G_OBJECT (domain), SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK);
194 g_object_notify (G_OBJECT (domain), SOUP_AUTH_DOMAIN_BASIC_AUTH_DATA);
200 memset (pw, 0, strlen (pw));
205 parse_basic (SoupMessage *msg, const char *header,
206 char **username, char **password)
208 char *decoded, *colon;
211 if (!header || (strncmp (header, "Basic ", 6) != 0))
214 decoded = (char *)g_base64_decode (header + 6, &len);
218 colon = memchr (decoded, ':', len);
224 plen = len - (colon - decoded) - 1;
226 *password = g_strndup (colon + 1, plen);
227 memset (colon + 1, 0, plen);
233 soup_auth_domain_basic_accepts (SoupAuthDomain *domain, SoupMessage *msg,
236 SoupAuthDomainBasicPrivate *priv =
237 SOUP_AUTH_DOMAIN_BASIC_GET_PRIVATE (domain);
238 char *username, *password;
241 if (!parse_basic (msg, header, &username, &password))
244 if (priv->auth_callback) {
245 ok = priv->auth_callback (domain, msg, username, password,
248 ok = soup_auth_domain_try_generic_auth_callback (
249 domain, msg, username);
263 soup_auth_domain_basic_challenge (SoupAuthDomain *domain, SoupMessage *msg)
267 challenge = g_string_new ("Basic ");
268 soup_header_g_string_append_param (challenge, "realm", soup_auth_domain_get_realm (domain));
269 return g_string_free (challenge, FALSE);
273 soup_auth_domain_basic_check_password (SoupAuthDomain *domain,
275 const char *username,
276 const char *password)
279 char *msg_username, *msg_password;
282 header = soup_message_headers_get_one (msg->request_headers,
284 if (!parse_basic (msg, header, &msg_username, &msg_password))
287 ok = (!strcmp (username, msg_username) &&
288 !strcmp (password, msg_password));
289 g_free (msg_username);
290 pw_free (msg_password);
296 soup_auth_domain_basic_class_init (SoupAuthDomainBasicClass *basic_class)
298 SoupAuthDomainClass *auth_domain_class =
299 SOUP_AUTH_DOMAIN_CLASS (basic_class);
300 GObjectClass *object_class = G_OBJECT_CLASS (basic_class);
302 g_type_class_add_private (basic_class, sizeof (SoupAuthDomainBasicPrivate));
304 auth_domain_class->accepts = soup_auth_domain_basic_accepts;
305 auth_domain_class->challenge = soup_auth_domain_basic_challenge;
306 auth_domain_class->check_password = soup_auth_domain_basic_check_password;
308 object_class->finalize = soup_auth_domain_basic_finalize;
309 object_class->set_property = soup_auth_domain_basic_set_property;
310 object_class->get_property = soup_auth_domain_basic_get_property;
313 * SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK:
315 * Alias for the #SoupAuthDomainBasic:auth-callback property.
316 * (The #SoupAuthDomainBasicAuthCallback.)
318 g_object_class_install_property (
319 object_class, PROP_AUTH_CALLBACK,
320 g_param_spec_pointer (SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK,
321 "Authentication callback",
322 "Password-checking callback",
325 * SOUP_AUTH_DOMAIN_BASIC_AUTH_DATA:
327 * Alias for the #SoupAuthDomainBasic:auth-data property.
328 * (The data to pass to the #SoupAuthDomainBasicAuthCallback.)
330 g_object_class_install_property (
331 object_class, PROP_AUTH_DATA,
332 g_param_spec_pointer (SOUP_AUTH_DOMAIN_BASIC_AUTH_DATA,
333 "Authentication callback data",
334 "Data to pass to authentication callback",