Git init
[profile/ivi/libsoup2.4.git] / libsoup / soup-auth-ntlm.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-auth-ntlm.c: HTTP NTLM Authentication helper
4  *
5  * Copyright (C) 2007 Red Hat, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <string.h>
13
14 #include "soup-auth-ntlm.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 *username, *password;
28 } SoupAuthNTLMPrivate;
29 #define SOUP_AUTH_NTLM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_AUTH_NTLM, SoupAuthNTLMPrivate))
30
31 G_DEFINE_TYPE (SoupAuthNTLM, soup_auth_ntlm, SOUP_TYPE_AUTH)
32
33 static void
34 soup_auth_ntlm_init (SoupAuthNTLM *ntlm)
35 {
36 }
37
38 static void
39 finalize (GObject *object)
40 {
41         SoupAuthNTLMPrivate *priv = SOUP_AUTH_NTLM_GET_PRIVATE (object);
42
43         g_free (priv->username);
44         if (priv->password) {
45                 memset (priv->password, 0, strlen (priv->password));
46                 g_free (priv->password);
47         }
48
49         G_OBJECT_CLASS (soup_auth_ntlm_parent_class)->finalize (object);
50 }
51
52 static void
53 soup_auth_ntlm_class_init (SoupAuthNTLMClass *auth_ntlm_class)
54 {
55         SoupAuthClass *auth_class = SOUP_AUTH_CLASS (auth_ntlm_class);
56         GObjectClass *object_class = G_OBJECT_CLASS (auth_ntlm_class);
57
58         g_type_class_add_private (auth_ntlm_class, sizeof (SoupAuthNTLMPrivate));
59
60         auth_class->scheme_name = "NTLM";
61         auth_class->strength = 3;
62
63         auth_class->update = update;
64         auth_class->get_protection_space = get_protection_space;
65         auth_class->authenticate = authenticate;
66         auth_class->is_authenticated = is_authenticated;
67         auth_class->get_authorization = get_authorization;
68
69         object_class->finalize = finalize;
70 }
71
72 SoupAuth *
73 soup_auth_ntlm_new (const char *realm, const char *host)
74 {
75         SoupAuth *auth;
76
77         auth = g_object_new (SOUP_TYPE_AUTH_NTLM,
78                              SOUP_AUTH_REALM, realm,
79                              SOUP_AUTH_HOST, host,
80                              NULL);
81         return auth;
82 }
83
84 static gboolean
85 update (SoupAuth *auth, SoupMessage *msg, GHashTable *auth_params)
86 {
87         g_return_val_if_reached (FALSE);
88 }
89
90 static GSList *
91 get_protection_space (SoupAuth *auth, SoupURI *source_uri)
92 {
93         g_return_val_if_reached (NULL);
94 }
95
96 static void
97 authenticate (SoupAuth *auth, const char *username, const char *password)
98 {
99         SoupAuthNTLMPrivate *priv = SOUP_AUTH_NTLM_GET_PRIVATE (auth);
100
101         g_return_if_fail (username != NULL);
102         g_return_if_fail (password != NULL);
103
104         priv->username = g_strdup (username);
105         priv->password = g_strdup (password);
106 }
107
108 static gboolean
109 is_authenticated (SoupAuth *auth)
110 {
111         return SOUP_AUTH_NTLM_GET_PRIVATE (auth)->password != NULL;
112 }
113
114 static char *
115 get_authorization (SoupAuth *auth, SoupMessage *msg)
116 {
117         g_return_val_if_reached (NULL);
118 }
119
120 const char *
121 soup_auth_ntlm_get_username (SoupAuth *auth)
122 {
123         SoupAuthNTLMPrivate *priv = SOUP_AUTH_NTLM_GET_PRIVATE (auth);
124
125         return priv->username;
126 }
127
128 const char *
129 soup_auth_ntlm_get_password (SoupAuth *auth)
130 {
131         SoupAuthNTLMPrivate *priv = SOUP_AUTH_NTLM_GET_PRIVATE (auth);
132
133         return priv->password;
134 }