1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-auth-basic.c: HTTP Basic Authentication
5 * Copyright (C) 2001-2003, Ximian, Inc.
14 #include "soup-auth-basic.h"
15 #include "soup-headers.h"
16 #include "soup-message.h"
17 #include "soup-misc.h"
20 static gboolean update (SoupAuth *auth, SoupMessage *msg, GHashTable *auth_params);
21 static GSList *get_protection_space (SoupAuth *auth, SoupURI *source_uri);
22 static void authenticate (SoupAuth *auth, const char *username, const char *password);
23 static gboolean is_authenticated (SoupAuth *auth);
24 static char *get_authorization (SoupAuth *auth, SoupMessage *msg);
28 } SoupAuthBasicPrivate;
29 #define SOUP_AUTH_BASIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_AUTH_BASIC, SoupAuthBasicPrivate))
31 G_DEFINE_TYPE (SoupAuthBasic, soup_auth_basic, SOUP_TYPE_AUTH)
34 soup_auth_basic_init (SoupAuthBasic *basic)
39 finalize (GObject *object)
41 SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (object);
45 G_OBJECT_CLASS (soup_auth_basic_parent_class)->finalize (object);
49 soup_auth_basic_class_init (SoupAuthBasicClass *auth_basic_class)
51 SoupAuthClass *auth_class = SOUP_AUTH_CLASS (auth_basic_class);
52 GObjectClass *object_class = G_OBJECT_CLASS (auth_basic_class);
54 g_type_class_add_private (auth_basic_class, sizeof (SoupAuthBasicPrivate));
56 auth_class->scheme_name = "Basic";
57 auth_class->strength = 1;
59 auth_class->update = update;
60 auth_class->get_protection_space = get_protection_space;
61 auth_class->authenticate = authenticate;
62 auth_class->is_authenticated = is_authenticated;
63 auth_class->get_authorization = get_authorization;
65 object_class->finalize = finalize;
70 update (SoupAuth *auth, SoupMessage *msg, GHashTable *auth_params)
72 SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
74 /* If we're updating a pre-existing auth, the
75 * username/password must be bad now, so forget it.
76 * Other than that, there's nothing to do here.
79 memset (priv->token, 0, strlen (priv->token));
88 get_protection_space (SoupAuth *auth, SoupURI *source_uri)
92 space = g_strdup (source_uri->path);
94 /* Strip query and filename component */
95 p = strrchr (space, '/');
96 if (p && p != space && p[1])
99 return g_slist_prepend (NULL, space);
103 authenticate (SoupAuth *auth, const char *username, const char *password)
105 SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
109 user_pass = g_strdup_printf ("%s:%s", username, password);
110 len = strlen (user_pass);
113 memset (priv->token, 0, strlen (priv->token));
114 g_free (priv->token);
116 priv->token = g_base64_encode ((guchar *)user_pass, len);
118 memset (user_pass, 0, len);
123 is_authenticated (SoupAuth *auth)
125 return SOUP_AUTH_BASIC_GET_PRIVATE (auth)->token != NULL;
129 get_authorization (SoupAuth *auth, SoupMessage *msg)
131 SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
133 return g_strdup_printf ("Basic %s", priv->token);