1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * soup-auth.c: HTTP Authentication framework |
---|
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.h" |
---|
15 | #include "soup-auth-basic.h" |
---|
16 | #include "soup-auth-digest.h" |
---|
17 | |
---|
18 | #define PARENT_TYPE G_TYPE_OBJECT |
---|
19 | static GObjectClass *parent_class; |
---|
20 | |
---|
21 | static void |
---|
22 | class_init (GObjectClass *object_class) |
---|
23 | { |
---|
24 | parent_class = g_type_class_ref (PARENT_TYPE); |
---|
25 | } |
---|
26 | |
---|
27 | SOUP_MAKE_TYPE (soup_auth, SoupAuth, class_init, NULL, PARENT_TYPE) |
---|
28 | |
---|
29 | |
---|
30 | typedef struct { |
---|
31 | const char *scheme; |
---|
32 | GType (*type_func) (void); |
---|
33 | int strength; |
---|
34 | } AuthScheme; |
---|
35 | |
---|
36 | static AuthScheme known_auth_schemes [] = { |
---|
37 | { "Basic", soup_auth_basic_get_type, 0 }, |
---|
38 | { "Digest", soup_auth_digest_get_type, 3 }, |
---|
39 | { NULL } |
---|
40 | }; |
---|
41 | |
---|
42 | /* FIXME: it should be possible to register new auth schemes! */ |
---|
43 | |
---|
44 | /** |
---|
45 | * soup_auth_new_from_header_list: |
---|
46 | * @vals: a list of WWW-Authenticate headers from a server response |
---|
47 | * |
---|
48 | * Creates a #SoupAuth value based on the strongest available |
---|
49 | * supported auth type in @vals. |
---|
50 | * |
---|
51 | * Return value: the new #SoupAuth, or %NULL if none could be created. |
---|
52 | **/ |
---|
53 | SoupAuth * |
---|
54 | soup_auth_new_from_header_list (const GSList *vals) |
---|
55 | { |
---|
56 | char *header = NULL; |
---|
57 | AuthScheme *scheme = NULL, *iter; |
---|
58 | SoupAuth *auth = NULL; |
---|
59 | |
---|
60 | g_return_val_if_fail (vals != NULL, NULL); |
---|
61 | |
---|
62 | while (vals) { |
---|
63 | char *tryheader = vals->data; |
---|
64 | |
---|
65 | for (iter = known_auth_schemes; iter->scheme; iter++) { |
---|
66 | if (!g_strncasecmp (tryheader, iter->scheme, |
---|
67 | strlen (iter->scheme))) { |
---|
68 | if (!scheme || |
---|
69 | scheme->strength < iter->strength) { |
---|
70 | header = tryheader; |
---|
71 | scheme = iter; |
---|
72 | } |
---|
73 | |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | vals = vals->next; |
---|
79 | } |
---|
80 | |
---|
81 | if (!scheme) |
---|
82 | return NULL; |
---|
83 | |
---|
84 | auth = g_object_new (scheme->type_func (), NULL); |
---|
85 | if (!auth) |
---|
86 | return NULL; |
---|
87 | |
---|
88 | SOUP_AUTH_GET_CLASS (auth)->construct (auth, header); |
---|
89 | return auth; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * soup_auth_authenticate: |
---|
94 | * @auth: a #SoupAuth |
---|
95 | * @username: the username provided by the user or client |
---|
96 | * @password: the password provided by the user or client |
---|
97 | * |
---|
98 | * This is called by the session after requesting a username and |
---|
99 | * password from the application. @auth should take the information |
---|
100 | * and do whatever scheme-specific processing is needed. |
---|
101 | **/ |
---|
102 | void |
---|
103 | soup_auth_authenticate (SoupAuth *auth, const char *username, const char *password) |
---|
104 | { |
---|
105 | g_return_if_fail (SOUP_IS_AUTH (auth)); |
---|
106 | g_return_if_fail (username != NULL); |
---|
107 | g_return_if_fail (password != NULL); |
---|
108 | |
---|
109 | SOUP_AUTH_GET_CLASS (auth)->authenticate (auth, username, password); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * soup_auth_get_scheme_name: |
---|
114 | * @auth: a #SoupAuth |
---|
115 | * |
---|
116 | * Returns @auth's scheme name. (Eg, "Basic") |
---|
117 | * |
---|
118 | * Return value: the scheme name |
---|
119 | **/ |
---|
120 | const char * |
---|
121 | soup_auth_get_scheme_name (SoupAuth *auth) |
---|
122 | { |
---|
123 | g_return_val_if_fail (SOUP_IS_AUTH (auth), NULL); |
---|
124 | |
---|
125 | return SOUP_AUTH_GET_CLASS (auth)->scheme_name; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * soup_auth_get_realm: |
---|
130 | * @auth: a #SoupAuth |
---|
131 | * |
---|
132 | * Returns @auth's realm, if any. |
---|
133 | * |
---|
134 | * Return value: the realm name |
---|
135 | **/ |
---|
136 | const char * |
---|
137 | soup_auth_get_realm (SoupAuth *auth) |
---|
138 | { |
---|
139 | g_return_val_if_fail (SOUP_IS_AUTH (auth), NULL); |
---|
140 | |
---|
141 | return SOUP_AUTH_GET_CLASS (auth)->get_realm (auth); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * soup_auth_is_authenticated: |
---|
146 | * @auth: a #SoupAuth |
---|
147 | * |
---|
148 | * Tests if @auth has been given a username and password |
---|
149 | * |
---|
150 | * Return value: %TRUE if @auth has been given a username and password |
---|
151 | **/ |
---|
152 | gboolean |
---|
153 | soup_auth_is_authenticated (SoupAuth *auth) |
---|
154 | { |
---|
155 | g_return_val_if_fail (SOUP_IS_AUTH (auth), TRUE); |
---|
156 | |
---|
157 | return SOUP_AUTH_GET_CLASS (auth)->is_authenticated (auth); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * soup_auth_get_authorization: |
---|
162 | * @auth: a #SoupAuth |
---|
163 | * @msg: the #SoupMessage to be authorized |
---|
164 | * |
---|
165 | * Generates an appropriate "Authorization" header for @msg. (This |
---|
166 | * will only be called if soup_auth_is_authenticated() returns %TRUE.) |
---|
167 | * |
---|
168 | * Return value: the "Authorization" header, which must be freed. |
---|
169 | **/ |
---|
170 | char * |
---|
171 | soup_auth_get_authorization (SoupAuth *auth, SoupMessage *msg) |
---|
172 | { |
---|
173 | g_return_val_if_fail (SOUP_IS_AUTH (auth), NULL); |
---|
174 | g_return_val_if_fail (msg != NULL, NULL); |
---|
175 | |
---|
176 | return SOUP_AUTH_GET_CLASS (auth)->get_authorization (auth, msg); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * soup_auth_get_protection_space: |
---|
181 | * @auth: a #SoupAuth |
---|
182 | * @source_uri: the URI of the request that @auth was generated in |
---|
183 | * response to. |
---|
184 | * |
---|
185 | * Returns a list of paths on the server which @auth extends over. |
---|
186 | * (All subdirectories of these paths are also assumed to be part |
---|
187 | * of @auth's protection space, unless otherwise discovered not to |
---|
188 | * be.) |
---|
189 | * |
---|
190 | * Return value: the list of paths, which must be freed with |
---|
191 | * soup_auth_free_protection_space(). |
---|
192 | **/ |
---|
193 | GSList * |
---|
194 | soup_auth_get_protection_space (SoupAuth *auth, const SoupUri *source_uri) |
---|
195 | { |
---|
196 | g_return_val_if_fail (SOUP_IS_AUTH (auth), NULL); |
---|
197 | g_return_val_if_fail (source_uri != NULL, NULL); |
---|
198 | |
---|
199 | return SOUP_AUTH_GET_CLASS (auth)->get_protection_space (auth, source_uri); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * soup_auth_free_protection_space: |
---|
204 | * @auth: a #SoupAuth |
---|
205 | * @space: the return value from soup_auth_get_protection_space() |
---|
206 | * |
---|
207 | * Frees @space. |
---|
208 | **/ |
---|
209 | void |
---|
210 | soup_auth_free_protection_space (SoupAuth *auth, GSList *space) |
---|
211 | { |
---|
212 | GSList *s; |
---|
213 | |
---|
214 | for (s = space; s; s = s->next) |
---|
215 | g_free (s->data); |
---|
216 | g_slist_free (space); |
---|
217 | } |
---|