summaryrefslogtreecommitdiff
path: root/rest/rest-proxy-auth.c
blob: 59bfb29040832e0565fe88d610680ac3bff5413c (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * 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>
#include "rest-private.h"

#define GET_PRIVATE(o) rest_proxy_auth_get_instance_private(REST_PROXY_AUTH(o))

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

G_DEFINE_TYPE_WITH_PRIVATE (RestProxyAuth, rest_proxy_auth, G_TYPE_OBJECT)

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

  g_clear_object (&priv->proxy);
#ifdef WITH_SOUP_2
  g_clear_object (&priv->session);
#endif
  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);

  object_class->dispose = rest_proxy_auth_dispose;
}

static void
rest_proxy_auth_init (RestProxyAuth *proxy)
{
  proxy->priv = 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);
#ifdef WITH_SOUP_2
  g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
#endif
  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);
#ifdef WITH_SOUP_2
  rest_auth->priv->session = g_object_ref(session);
#endif
  rest_auth->priv->message = g_object_ref(message);
  rest_auth->priv->auth = g_object_ref(soup_auth);

  return rest_auth;
}

/**
 * rest_proxy_auth_pause:
 * @auth: a #RestProxyAuth
 *
 * Pauses @auth.
 *
 * If @auth is already paused, this function does not
 * do anything.
 *
 * Deprecated: 0.9: This object get removed from [class@Rest.Proxy]
 */
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;
#ifdef WITH_SOUP_2
  soup_session_pause_message (auth->priv->session, auth->priv->message);
#endif
}

/**
 * rest_proxy_auth_unpause:
 * @auth: a paused #RestProxyAuth
 *
 * Unpauses a paused #RestProxyAuth instance.
 *
 * Deprecated: 0.9: This object get removed from [class@Rest.Proxy]
 */
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);
#ifdef WITH_SOUP_2
  soup_session_unpause_message (auth->priv->session, auth->priv->message);
#endif
  auth->priv->paused = FALSE;
}

/**
 * rest_proxy_auth_cancel:
 * @auth: a #RestProxyAuth
 *
 * Cancel the authentication process
 * by cancelling the associated #SoupMessage.
 * It results in returning #GError REST_PROXY_ERROR_CANCELLED
 * to the function that requested the authentication.
 *
 * Deprecated: 0.9: This object get removed from [class@Rest.Proxy]
 */
void
rest_proxy_auth_cancel (RestProxyAuth *auth)
{
  g_return_if_fail (REST_IS_PROXY_AUTH (auth));

#ifdef WITH_SOUP_2
  soup_session_cancel_message (auth->priv->session, auth->priv->message, SOUP_STATUS_CANCELLED);
#else
  soup_auth_cancel (auth->priv->auth);
#endif
}

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;
}