1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-auth-manager.c: SoupAuth manager for SoupSession
5 * Copyright (C) 2007 Red Hat, Inc.
14 #include "soup-auth-manager.h"
15 #include "soup-address.h"
16 #include "soup-headers.h"
17 #include "soup-marshal.h"
18 #include "soup-message-private.h"
19 #include "soup-message-queue.h"
20 #include "soup-path-map.h"
21 #include "soup-session.h"
22 #include "soup-session-feature.h"
23 #include "soup-session-private.h"
26 static void soup_auth_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
27 static SoupSessionFeatureInterface *soup_session_feature_default_interface;
29 static void attach (SoupSessionFeature *feature, SoupSession *session);
30 static void request_queued (SoupSessionFeature *feature, SoupSession *session,
32 static void request_started (SoupSessionFeature *feature, SoupSession *session,
33 SoupMessage *msg, SoupSocket *socket);
34 static void request_unqueued (SoupSessionFeature *feature,
35 SoupSession *session, SoupMessage *msg);
36 static gboolean add_feature (SoupSessionFeature *feature, GType type);
37 static gboolean remove_feature (SoupSessionFeature *feature, GType type);
38 static gboolean has_feature (SoupSessionFeature *feature, GType type);
45 static guint signals[LAST_SIGNAL] = { 0 };
47 G_DEFINE_TYPE_WITH_CODE (SoupAuthManager, soup_auth_manager, G_TYPE_OBJECT,
48 G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
49 soup_auth_manager_session_feature_init))
53 GPtrArray *auth_types;
56 GHashTable *auth_hosts;
57 } SoupAuthManagerPrivate;
58 #define SOUP_AUTH_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_AUTH_MANAGER, SoupAuthManagerPrivate))
62 SoupPathMap *auth_realms; /* path -> scheme:realm */
63 GHashTable *auths; /* scheme:realm -> SoupAuth */
66 static void soup_auth_host_free (SoupAuthHost *host);
69 soup_auth_manager_init (SoupAuthManager *manager)
71 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
73 priv->auth_types = g_ptr_array_new_with_free_func ((GDestroyNotify)g_type_class_unref);
74 priv->auth_hosts = g_hash_table_new_full (soup_uri_host_hash,
77 (GDestroyNotify)soup_auth_host_free);
81 finalize (GObject *object)
83 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (object);
85 g_ptr_array_free (priv->auth_types, TRUE);
87 g_hash_table_destroy (priv->auth_hosts);
89 g_clear_object (&priv->proxy_auth);
91 G_OBJECT_CLASS (soup_auth_manager_parent_class)->finalize (object);
95 soup_auth_manager_class_init (SoupAuthManagerClass *auth_manager_class)
97 GObjectClass *object_class = G_OBJECT_CLASS (auth_manager_class);
99 g_type_class_add_private (auth_manager_class, sizeof (SoupAuthManagerPrivate));
101 object_class->finalize = finalize;
103 signals[AUTHENTICATE] =
104 g_signal_new ("authenticate",
105 G_OBJECT_CLASS_TYPE (object_class),
107 G_STRUCT_OFFSET (SoupAuthManagerClass, authenticate),
109 _soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
118 soup_auth_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface,
119 gpointer interface_data)
121 soup_session_feature_default_interface =
122 g_type_default_interface_peek (SOUP_TYPE_SESSION_FEATURE);
124 feature_interface->attach = attach;
125 feature_interface->request_queued = request_queued;
126 feature_interface->request_started = request_started;
127 feature_interface->request_unqueued = request_unqueued;
128 feature_interface->add_feature = add_feature;
129 feature_interface->remove_feature = remove_feature;
130 feature_interface->has_feature = has_feature;
134 auth_type_compare_func (gconstpointer a, gconstpointer b)
136 SoupAuthClass **auth1 = (SoupAuthClass **)a;
137 SoupAuthClass **auth2 = (SoupAuthClass **)b;
139 return (*auth1)->strength - (*auth2)->strength;
143 add_feature (SoupSessionFeature *feature, GType type)
145 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (feature);
146 SoupAuthClass *auth_class;
148 if (!g_type_is_a (type, SOUP_TYPE_AUTH))
151 auth_class = g_type_class_ref (type);
152 g_ptr_array_add (priv->auth_types, auth_class);
153 g_ptr_array_sort (priv->auth_types, auth_type_compare_func);
158 remove_feature (SoupSessionFeature *feature, GType type)
160 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (feature);
161 SoupAuthClass *auth_class;
164 if (!g_type_is_a (type, SOUP_TYPE_AUTH))
167 auth_class = g_type_class_peek (type);
168 for (i = 0; i < priv->auth_types->len; i++) {
169 if (priv->auth_types->pdata[i] == (gpointer)auth_class) {
170 g_ptr_array_remove_index (priv->auth_types, i);
179 has_feature (SoupSessionFeature *feature, GType type)
181 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (feature);
182 SoupAuthClass *auth_class;
185 if (!g_type_is_a (type, SOUP_TYPE_AUTH))
188 auth_class = g_type_class_peek (type);
189 for (i = 0; i < priv->auth_types->len; i++) {
190 if (priv->auth_types->pdata[i] == (gpointer)auth_class)
197 soup_auth_manager_emit_authenticate (SoupAuthManager *manager, SoupMessage *msg,
198 SoupAuth *auth, gboolean retrying)
200 g_signal_emit (manager, signals[AUTHENTICATE], 0, msg, auth, retrying);
204 attach (SoupSessionFeature *manager, SoupSession *session)
206 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
208 /* FIXME: should support multiple sessions */
209 priv->session = session;
211 soup_session_feature_default_interface->attach (manager, session);
214 static inline const char *
215 auth_header_for_message (SoupMessage *msg)
217 if (msg->status_code == SOUP_STATUS_PROXY_UNAUTHORIZED) {
218 return soup_message_headers_get_list (msg->response_headers,
219 "Proxy-Authenticate");
221 return soup_message_headers_get_list (msg->response_headers,
227 next_challenge_start (GSList *items)
229 /* The relevant grammar (from httpbis):
231 * WWW-Authenticate = 1#challenge
232 * Proxy-Authenticate = 1#challenge
233 * challenge = auth-scheme [ 1*SP ( b64token / #auth-param ) ]
234 * auth-scheme = token
235 * auth-param = token BWS "=" BWS ( token / quoted-string )
236 * b64token = 1*( ALPHA / DIGIT /
237 * "-" / "." / "_" / "~" / "+" / "/" ) *"="
239 * The fact that quoted-strings can contain commas, equals
240 * signs, and auth scheme names makes it tricky to "cheat" on
241 * the parsing. So soup_auth_manager_extract_challenge() will
242 * have used soup_header_parse_list() to split the header into
243 * items. Given the grammar above, the possible items are:
246 * auth-scheme 1*SP b64token
247 * auth-scheme 1*SP auth-param
250 * where the first three represent the start of a new challenge and
251 * the last one does not.
254 for (; items; items = items->next) {
255 const char *item = items->data;
256 const char *sp = strpbrk (item, "\t\r\n ");
257 const char *eq = strchr (item, '=');
260 /* No "=", so it can't be an auth-param */
263 if (!sp || sp > eq) {
264 /* No space, or first space appears after the "=",
265 * so it must be an auth-param.
269 while (g_ascii_isspace (*++sp))
272 /* First "=" appears immediately after the first
273 * space, so this must be an auth-param with
274 * space around the "=".
279 /* "auth-scheme auth-param" or "auth-scheme b64token" */
287 soup_auth_manager_extract_challenge (const char *challenges, const char *scheme)
289 GSList *items, *i, *next;
290 int schemelen = strlen (scheme);
294 items = soup_header_parse_list (challenges);
296 /* First item will start with the scheme name, followed by
297 * either nothing, or else a space and then the first
300 for (i = items; i; i = next_challenge_start (i->next)) {
302 if (!g_ascii_strncasecmp (item, scheme, schemelen) &&
303 (!item[schemelen] || g_ascii_isspace (item[schemelen])))
307 soup_header_free_list (items);
311 next = next_challenge_start (i->next);
312 challenge = g_string_new (item);
313 for (i = i->next; i != next; i = i->next) {
315 g_string_append (challenge, ", ");
316 g_string_append (challenge, item);
319 soup_header_free_list (items);
320 return g_string_free (challenge, FALSE);
324 create_auth (SoupAuthManagerPrivate *priv, SoupMessage *msg)
327 SoupAuthClass *auth_class;
328 char *challenge = NULL;
332 header = auth_header_for_message (msg);
336 for (i = priv->auth_types->len - 1; i >= 0; i--) {
337 auth_class = priv->auth_types->pdata[i];
338 challenge = soup_auth_manager_extract_challenge (header, auth_class->scheme_name);
345 auth = soup_auth_new (G_TYPE_FROM_CLASS (auth_class), msg, challenge);
351 check_auth (SoupMessage *msg, SoupAuth *auth)
357 header = auth_header_for_message (msg);
361 challenge = soup_auth_manager_extract_challenge (header, soup_auth_get_scheme_name (auth));
365 ok = soup_auth_update (auth, msg, challenge);
370 static SoupAuthHost *
371 get_auth_host_for_message (SoupAuthManagerPrivate *priv, SoupMessage *msg)
374 SoupURI *uri = soup_message_get_uri (msg);
376 host = g_hash_table_lookup (priv->auth_hosts, uri);
380 host = g_slice_new0 (SoupAuthHost);
381 host->uri = soup_uri_copy_host (uri);
382 g_hash_table_insert (priv->auth_hosts, host->uri, host);
388 soup_auth_host_free (SoupAuthHost *host)
390 g_clear_pointer (&host->auth_realms, soup_path_map_free);
391 g_clear_pointer (&host->auths, g_hash_table_destroy);
393 soup_uri_free (host->uri);
394 g_slice_free (SoupAuthHost, host);
398 lookup_auth (SoupAuthManagerPrivate *priv, SoupMessage *msg)
401 const char *path, *realm;
403 host = get_auth_host_for_message (priv, msg);
404 if (!host->auth_realms)
407 path = soup_message_get_uri (msg)->path;
410 realm = soup_path_map_lookup (host->auth_realms, path);
412 return g_hash_table_lookup (host->auths, realm);
418 authenticate_auth (SoupAuthManager *manager, SoupAuth *auth,
419 SoupMessage *msg, gboolean prior_auth_failed,
420 gboolean proxy, gboolean can_interact)
422 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
426 SoupMessageQueue *queue;
427 SoupMessageQueueItem *item;
429 queue = soup_session_get_queue (priv->session);
430 item = soup_message_queue_lookup (queue, msg);
432 uri = soup_connection_get_proxy_uri (item->conn);
433 soup_message_queue_item_unref (item);
440 uri = soup_message_get_uri (msg);
442 /* If a password is specified explicitly in the URI, use it
443 * even if the auth had previously already been authenticated.
446 if (!prior_auth_failed)
447 soup_auth_authenticate (auth, uri->user, uri->password);
448 } else if (!soup_auth_is_authenticated (auth) && can_interact) {
449 soup_auth_manager_emit_authenticate (manager, msg, auth,
453 return soup_auth_is_authenticated (auth);
457 update_auth (SoupMessage *msg, gpointer manager)
459 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
461 SoupAuth *auth, *prior_auth, *old_auth;
463 char *auth_info, *old_auth_info;
465 gboolean prior_auth_failed = FALSE;
467 host = get_auth_host_for_message (priv, msg);
469 /* See if we used auth last time */
470 prior_auth = soup_message_get_auth (msg);
471 if (prior_auth && check_auth (msg, prior_auth)) {
473 if (!soup_auth_is_authenticated (auth))
474 prior_auth_failed = TRUE;
476 auth = create_auth (priv, msg);
480 auth_info = soup_auth_get_info (auth);
482 if (!host->auth_realms) {
483 host->auth_realms = soup_path_map_new (g_free);
484 host->auths = g_hash_table_new_full (g_str_hash, g_str_equal,
485 g_free, g_object_unref);
488 /* Record where this auth realm is used. */
489 pspace = soup_auth_get_protection_space (auth, soup_message_get_uri (msg));
490 for (p = pspace; p; p = p->next) {
492 old_auth_info = soup_path_map_lookup (host->auth_realms, path);
494 if (!strcmp (old_auth_info, auth_info))
496 soup_path_map_remove (host->auth_realms, path);
499 soup_path_map_add (host->auth_realms, path,
500 g_strdup (auth_info));
502 soup_auth_free_protection_space (auth, pspace);
504 /* Now, make sure the auth is recorded. (If there's a
505 * pre-existing auth, we keep that rather than the new one,
506 * since the old one might already be authenticated.)
508 old_auth = g_hash_table_lookup (host->auths, auth_info);
511 if (auth != old_auth && auth != prior_auth) {
512 g_object_unref (auth);
516 g_hash_table_insert (host->auths, auth_info, auth);
519 /* If we need to authenticate, try to do it. */
520 authenticate_auth (manager, auth, msg,
521 prior_auth_failed, FALSE, TRUE);
525 requeue_if_authenticated (SoupMessage *msg, gpointer manager)
527 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
528 SoupAuth *auth = lookup_auth (priv, msg);
530 if (auth && soup_auth_is_authenticated (auth))
531 soup_session_requeue_message (priv->session, msg);
535 update_proxy_auth (SoupMessage *msg, gpointer manager)
537 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
538 SoupAuth *prior_auth;
539 gboolean prior_auth_failed = FALSE;
541 /* See if we used auth last time */
542 prior_auth = soup_message_get_proxy_auth (msg);
543 if (prior_auth && check_auth (msg, prior_auth)) {
544 if (!soup_auth_is_authenticated (prior_auth))
545 prior_auth_failed = TRUE;
548 if (!priv->proxy_auth) {
549 priv->proxy_auth = create_auth (priv, msg);
550 if (!priv->proxy_auth)
554 /* If we need to authenticate, try to do it. */
555 authenticate_auth (manager, priv->proxy_auth, msg,
556 prior_auth_failed, TRUE, TRUE);
560 requeue_if_proxy_authenticated (SoupMessage *msg, gpointer manager)
562 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
563 SoupAuth *auth = priv->proxy_auth;
565 if (auth && soup_auth_is_authenticated (auth))
566 soup_session_requeue_message (priv->session, msg);
570 request_queued (SoupSessionFeature *manager, SoupSession *session,
573 soup_message_add_status_code_handler (
574 msg, "got_headers", SOUP_STATUS_UNAUTHORIZED,
575 G_CALLBACK (update_auth), manager);
576 soup_message_add_status_code_handler (
577 msg, "got_body", SOUP_STATUS_UNAUTHORIZED,
578 G_CALLBACK (requeue_if_authenticated), manager);
580 soup_message_add_status_code_handler (
581 msg, "got_headers", SOUP_STATUS_PROXY_UNAUTHORIZED,
582 G_CALLBACK (update_proxy_auth), manager);
583 soup_message_add_status_code_handler (
584 msg, "got_body", SOUP_STATUS_PROXY_UNAUTHORIZED,
585 G_CALLBACK (requeue_if_proxy_authenticated), manager);
589 request_started (SoupSessionFeature *feature, SoupSession *session,
590 SoupMessage *msg, SoupSocket *socket)
592 SoupAuthManager *manager = SOUP_AUTH_MANAGER (feature);
593 SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
596 auth = lookup_auth (priv, msg);
597 if (!auth || !authenticate_auth (manager, auth, msg, FALSE, FALSE, FALSE))
599 soup_message_set_auth (msg, auth);
601 auth = priv->proxy_auth;
602 if (!auth || !authenticate_auth (manager, auth, msg, FALSE, TRUE, FALSE))
604 soup_message_set_proxy_auth (msg, auth);
608 request_unqueued (SoupSessionFeature *manager, SoupSession *session,
611 g_signal_handlers_disconnect_matched (msg, G_SIGNAL_MATCH_DATA,
612 0, 0, NULL, NULL, manager);