summaryrefslogtreecommitdiff
path: root/rest/rest-proxy-auth.c
blob: 3d808d4afa6c8ceae9b4805c3af003b7575f2d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * librest - RESTful web services access
 * Copyright (c) 2012, Red Hat, Inc.
 *
 * Authors: Christophe Fergeau <cfergeau@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#include <config.h>

#include <rest/rest-proxy-auth.h>
#include <rest/rest-proxy-auth-private.h>

G_DEFINE_TYPE (RestProxyAuth, rest_proxy_auth, G_TYPE_OBJECT)

#define REST_PROXY_AUTH_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), REST_TYPE_PROXY_AUTH, RestProxyAuthPrivate))

struct _RestProxyAuthPrivate {
  /* used to hold state during async authentication */
  RestProxy *proxy;
  SoupSession *session;
  SoupMessage *message;
  SoupAuth *auth;
  gboolean paused;
};

static void
rest_proxy_auth_dispose (GObject *object)
{
  RestProxyAuthPrivate *priv = ((RestProxyAuth*)object)->priv;

  g_clear_object (&priv->proxy);
  g_clear_object (&priv->session);
  g_clear_object (&priv->message);
  g_clear_object (&priv->auth);

  G_OBJECT_CLASS (rest_proxy_auth_parent_class)->dispose (object);
}

static void
rest_proxy_auth_class_init (RestProxyAuthClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (RestProxyAuthPrivate));

  object_class->dispose = rest_proxy_auth_dispose;
}

static void
rest_proxy_auth_init (RestProxyAuth *proxy)
{
  proxy->priv = REST_PROXY_AUTH_GET_PRIVATE (proxy);
}

G_GNUC_INTERNAL RestProxyAuth*
rest_proxy_auth_new (RestProxy *proxy,
                     SoupSession *session,
                     SoupMessage *message,
                     SoupAuth *soup_auth)
{
  RestProxyAuth *rest_auth;

  g_return_val_if_fail (REST_IS_PROXY (proxy), NULL);
  g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
  g_return_val_if_fail (SOUP_IS_MESSAGE (message), NULL);
  g_return_val_if_fail (SOUP_IS_AUTH (soup_auth), NULL);

  rest_auth = REST_PROXY_AUTH (g_object_new (REST_TYPE_PROXY_AUTH, NULL));
  rest_auth->priv->proxy = g_object_ref(proxy);
  rest_auth->priv->session = g_object_ref(session);
  rest_auth->priv->message = g_object_ref(message);
  rest_auth->priv->auth = g_object_ref(soup_auth);

  return rest_auth;
}

void
rest_proxy_auth_pause (RestProxyAuth *auth)
{
  g_return_if_fail (REST_IS_PROXY_AUTH (auth));

  if (auth->priv->paused)
      return;

  auth->priv->paused = TRUE;
  soup_session_pause_message (auth->priv->session, auth->priv->message);
}

void
rest_proxy_auth_unpause (RestProxyAuth *auth)
{
  RestProxy *proxy;
  gchar *username;
  gchar *password;

  g_return_if_fail (REST_IS_PROXY_AUTH (auth));
  g_return_if_fail (auth->priv->paused);

  proxy = REST_PROXY (auth->priv->proxy);
  g_object_get (G_OBJECT (proxy), "username", &username, "password", &password, NULL);
  soup_auth_authenticate (auth->priv->auth, username, password);
  g_free (username);
  g_free (password);
  soup_session_unpause_message (auth->priv->session, auth->priv->message);
  auth->priv->paused = FALSE;
}

G_GNUC_INTERNAL gboolean rest_proxy_auth_is_paused (RestProxyAuth *auth)
{
  g_return_val_if_fail (REST_IS_PROXY_AUTH (auth), FALSE);

  return auth->priv->paused;
}