Git init
[profile/ivi/libsoup2.4.git] / libsoup / soup-auth-basic.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-auth-basic.c: HTTP Basic Authentication
4  *
5  * Copyright (C) 2001-2003, Ximian, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <string.h>
13
14 #include "soup-auth-basic.h"
15 #include "soup-headers.h"
16 #include "soup-message.h"
17 #include "soup-misc.h"
18 #include "soup-uri.h"
19
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);
25
26 typedef struct {
27         char *token;
28 } SoupAuthBasicPrivate;
29 #define SOUP_AUTH_BASIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_AUTH_BASIC, SoupAuthBasicPrivate))
30
31 G_DEFINE_TYPE (SoupAuthBasic, soup_auth_basic, SOUP_TYPE_AUTH)
32
33 static void
34 soup_auth_basic_init (SoupAuthBasic *basic)
35 {
36 }
37
38 static void
39 finalize (GObject *object)
40 {
41         SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (object);
42
43         g_free (priv->token);
44
45         G_OBJECT_CLASS (soup_auth_basic_parent_class)->finalize (object);
46 }
47
48 static void
49 soup_auth_basic_class_init (SoupAuthBasicClass *auth_basic_class)
50 {
51         SoupAuthClass *auth_class = SOUP_AUTH_CLASS (auth_basic_class);
52         GObjectClass *object_class = G_OBJECT_CLASS (auth_basic_class);
53
54         g_type_class_add_private (auth_basic_class, sizeof (SoupAuthBasicPrivate));
55
56         auth_class->scheme_name = "Basic";
57         auth_class->strength = 1;
58
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;
64
65         object_class->finalize = finalize;
66 }
67
68
69 static gboolean
70 update (SoupAuth *auth, SoupMessage *msg, GHashTable *auth_params)
71 {
72         SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
73
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.
77          */
78         if (priv->token) {
79                 memset (priv->token, 0, strlen (priv->token));
80                 g_free (priv->token);
81                 priv->token = NULL;
82         }
83
84         return TRUE;
85 }
86
87 static GSList *
88 get_protection_space (SoupAuth *auth, SoupURI *source_uri)
89 {
90         char *space, *p;
91
92         space = g_strdup (source_uri->path);
93
94         /* Strip query and filename component */
95         p = strrchr (space, '/');
96         if (p && p != space && p[1])
97                 *p = '\0';
98
99         return g_slist_prepend (NULL, space);
100 }
101
102 static void
103 authenticate (SoupAuth *auth, const char *username, const char *password)
104 {
105         SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
106         char *user_pass;
107         int len;
108
109         user_pass = g_strdup_printf ("%s:%s", username, password);
110         len = strlen (user_pass);
111
112         if (priv->token) {
113                 memset (priv->token, 0, strlen (priv->token));
114                 g_free (priv->token);
115         }
116         priv->token = g_base64_encode ((guchar *)user_pass, len);
117
118         memset (user_pass, 0, len);
119         g_free (user_pass);
120 }
121
122 static gboolean
123 is_authenticated (SoupAuth *auth)
124 {
125         return SOUP_AUTH_BASIC_GET_PRIVATE (auth)->token != NULL;
126 }
127
128 static char *
129 get_authorization (SoupAuth *auth, SoupMessage *msg)
130 {
131         SoupAuthBasicPrivate *priv = SOUP_AUTH_BASIC_GET_PRIVATE (auth);
132
133         return g_strdup_printf ("Basic %s", priv->token);
134 }