summaryrefslogtreecommitdiff
path: root/wocky/wocky-auth-registry.h
blob: f5e3f4513efbfe18ea42dd36ebd9cd916a0f75dc (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#if !defined (WOCKY_H_INSIDE) && !defined (WOCKY_COMPILATION)
# error "Only <wocky/wocky.h> can be included directly."
#endif

/* wocky-auth-registry.h */
#ifndef _WOCKY_AUTH_REGISTRY_H
#define _WOCKY_AUTH_REGISTRY_H

#include <glib-object.h>
#include <gio/gio.h>
#include "wocky-auth-handler.h"
#include "wocky-enumtypes.h"

G_BEGIN_DECLS

GQuark wocky_auth_error_quark (void);
#define WOCKY_AUTH_ERROR \
  wocky_auth_error_quark ()

/**
 * WockyAuthError:
 * @WOCKY_AUTH_ERROR_INIT_FAILED: Failed to initialize our auth
 *   support
 * @WOCKY_AUTH_ERROR_NOT_SUPPORTED: Server doesn't support this
 *   authentication method
 * @WOCKY_AUTH_ERROR_NO_SUPPORTED_MECHANISMS: Server doesn't support
 *   any mechanisms that we support
 * @WOCKY_AUTH_ERROR_NETWORK: Couldn't send our stanzas to the server
 * @WOCKY_AUTH_ERROR_INVALID_REPLY: Server sent an invalid reply
 * @WOCKY_AUTH_ERROR_NO_CREDENTIALS: Failure to provide user
 *   credentials
 * @WOCKY_AUTH_ERROR_FAILURE: Server sent a failure
 * @WOCKY_AUTH_ERROR_CONNRESET: disconnected
 * @WOCKY_AUTH_ERROR_STREAM: XMPP stream error while authing
 * @WOCKY_AUTH_ERROR_RESOURCE_CONFLICT: Resource conflict (relevant in
 *   in jabber auth)
 * @WOCKY_AUTH_ERROR_NOT_AUTHORIZED: Provided credentials are not
 *   valid
 *
 * #WockyAuthRegistry specific errors.
 */
typedef enum
{
  WOCKY_AUTH_ERROR_INIT_FAILED,
  WOCKY_AUTH_ERROR_NOT_SUPPORTED,
  WOCKY_AUTH_ERROR_NO_SUPPORTED_MECHANISMS,
  WOCKY_AUTH_ERROR_NETWORK,
  WOCKY_AUTH_ERROR_INVALID_REPLY,
  WOCKY_AUTH_ERROR_NO_CREDENTIALS,
  WOCKY_AUTH_ERROR_FAILURE,
  WOCKY_AUTH_ERROR_CONNRESET,
  WOCKY_AUTH_ERROR_STREAM,
  WOCKY_AUTH_ERROR_RESOURCE_CONFLICT,
  WOCKY_AUTH_ERROR_NOT_AUTHORIZED,
} WockyAuthError;

#define WOCKY_AUTH_MECH_JABBER_DIGEST "X-WOCKY-JABBER-DIGEST"
#define WOCKY_AUTH_MECH_JABBER_PASSWORD "X-WOCKY-JABBER-PASSWORD"
#define WOCKY_AUTH_MECH_SASL_DIGEST_MD5 "DIGEST-MD5"
#define WOCKY_AUTH_MECH_SASL_PLAIN "PLAIN"
#define WOCKY_AUTH_MECH_SASL_SCRAM_SHA_1 "SCRAM-SHA-1"

/**
 * WockyAuthRegistryStartData:
 * @mechanism: the name of the mechanism
 * @initial_response: the data in the response
 *
 * A structure to hold the mechanism and response data.
 */
typedef struct {
  gchar *mechanism;
  GString *initial_response;
} WockyAuthRegistryStartData;

#define WOCKY_TYPE_AUTH_REGISTRY wocky_auth_registry_get_type()

#define WOCKY_AUTH_REGISTRY(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
  WOCKY_TYPE_AUTH_REGISTRY, WockyAuthRegistry))

#define WOCKY_AUTH_REGISTRY_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), \
  WOCKY_TYPE_AUTH_REGISTRY, WockyAuthRegistryClass))

#define WOCKY_IS_AUTH_REGISTRY(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
  WOCKY_TYPE_AUTH_REGISTRY))

#define WOCKY_IS_AUTH_REGISTRY_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
  WOCKY_TYPE_AUTH_REGISTRY))

#define WOCKY_AUTH_REGISTRY_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
  WOCKY_TYPE_AUTH_REGISTRY, WockyAuthRegistryClass))

typedef struct _WockyAuthRegistry WockyAuthRegistry;

/**
 * WockyAuthRegistryClass:
 * @start_auth_async_func: a function to call to start an asynchronous
 *   start auth operation; see wocky_auth_registry_start_auth_async() for
 *   more details.
 * @start_auth_finish_func: a function to call to finish an
 *   asynchronous start auth operation; see
 *   wocky_auth_registry_start_auth_finish() for more details.
 * @challenge_async_func: a function to call to start an asynchronous
 *   challenge operation; see wocky_auth_registry_challenge_async() for
 *   more details.
 * @challenge_finish_func: a function to call to finish an asynchronous
 *   challenge operation; see wocky_auth_registry_challenge_finish() for
 *   more details.
 * @success_async_func: a function to call to start an asynchronous
 *   success operation; see wocky_auth_registry_success_async() for
 *   more details.
 * @success_finish_func: a function to call to finish an asynchronous
 *   success operation; see wocky_auth_registry_success_finish() for
 *   more details.
 * @failure_func: a function to call on failure; see
 *   wocky_auth_registry_failure() for more details.
 *
 * The class of a #WockyAuthRegistry.
 */
typedef struct _WockyAuthRegistryClass WockyAuthRegistryClass;
typedef struct _WockyAuthRegistryPrivate WockyAuthRegistryPrivate;

/**
 * WockyAuthRegistryStartAuthAsyncFunc:
 * @self: a #WockyAuthRegistry object
 * @mechanisms: a list of avahilable mechanisms
 * @allow_plain: %TRUE if PLAIN is allowed, otherwise %FALSE
 * @is_secure_channel: %TRUE if channel is secure, otherwise %FALSE
 * @username: the username
 * @password: the password
 * @server: the server
 * @session_id: the session ID
 * @callback: a callback to be called when finished
 * @user_data: data to pass to @callback
 *
 * Starts a async authentication: chooses mechanism and gets initial data.
 * The default function chooses a #WockyAuthHandler by which mechanism it
 * supports and gets the initial data from the chosen handler.
 */
typedef void (*WockyAuthRegistryStartAuthAsyncFunc) (WockyAuthRegistry *self,
    GSList *mechanisms,
    gboolean allow_plain,
    gboolean is_secure_channel,
    const gchar *username,
    const gchar *password,
    const gchar *server,
    const gchar *session_id,
    GAsyncReadyCallback callback,
    gpointer user_data);

/**
 * WockyAuthRegistryStartAuthFinishFunc:
 * @self: a #WockyAuthRegistry object
 * @result: a #GAsyncResult object
 * @start_data: a location to fill with a #WockyAuthRegistryStartData structure
 * @error: a location to fill with a #GError if an error is hit, or %NULL
 *
 * Called to finish the #GAsyncResult task for authentication
 * start. By default, it extracts a #WockyAuthRegistryStartData
 * pointer from a given #GSimpleAsyncResult and copies it to the out
 * param.
 *
 * Returns: %TRUE on success, otherwise %FALSE
 */
typedef gboolean (*WockyAuthRegistryStartAuthFinishFunc) (
    WockyAuthRegistry *self,
    GAsyncResult *result,
    WockyAuthRegistryStartData **start_data,
    GError **error);

/**
 * WockyAuthRegistryChallengeAsyncFunc:
 * @self: a #WockyAuthRegistry object
 * @challenge_data: the challenge data string
 * @callback: a callback to call when finished
 * @user_data: data to pass to @callback
 *
 * Recieves a challenge and asynchronously provides a reply. By
 * default the challenge is passed on to the chosen #WockyAuthHandler.
 */
typedef void (*WockyAuthRegistryChallengeAsyncFunc) (WockyAuthRegistry *self,
    const GString *challenge_data,
    GAsyncReadyCallback callback,
    gpointer user_data);

/**
 * WockyAuthRegistryChallengeFinishFunc:
 * @self: a #WockyAuthRegistry object
 * @result: a #GAsyncResult object
 * @response: a location to be filled with the response string
 * @error: a location to fill with a #GError if an error is hit, or %NULL
 *
 * Finishes a #GAsyncResult from
 * #WockyAuthRegistryChallengeAsyncFunc. By default it extracts a
 * #GString response from the given #GSimpleAsyncResult and copies it
 * to the out param.
 *
 * Returns: %TRUE on success, otherwise %FALSE
 */
typedef gboolean (*WockyAuthRegistryChallengeFinishFunc) (
    WockyAuthRegistry *self,
    GAsyncResult *result,
    GString **response,
    GError **error);

/**
 * WockyAuthRegistrySuccessAsyncFunc:
 * @self: a #WockyAuthRegistry object
 * @callback: a callback to be called when finished
 * @user_data: data to pass to @callback
 *
 * Notifies the registry of authentication success, and allows a last ditch
 * attempt at aborting the authentication at the client's discretion.
 */
typedef void (*WockyAuthRegistrySuccessAsyncFunc) (WockyAuthRegistry *self,
    GAsyncReadyCallback callback,
    gpointer user_data);

/**
 * WockyAuthRegistrySuccessFinishFunc:
 * @self: a #WockyAuthRegistry object
 * @result: a #GAsyncResult object
 * @error: a location to fill with a #GError if an error is hit, or %NULL
 *
 * Finishes a #GAsyncResult from
 * #WockyAuthRegistrySuccessAsyncFunc. It checks for any errors set on
 * the given #GSimpleAsyncResult, copies the #GError to an out param
 * and returns %FALSE if there was an error.
 *
 * Returns: %TRUE on success, otherwise %FALSE
 */
typedef gboolean (*WockyAuthRegistrySuccessFinishFunc) (
    WockyAuthRegistry *self,
    GAsyncResult *result,
    GError **error);

/**
 * WockyAuthRegistryFailureFunc:
 * @self: a #WockyAuthRegistry object
 * @error: a #GError describing the failure
 *
 * Notifies the client of a server-side error. By default this is not
 * implemented.
 *
 */
typedef void (*WockyAuthRegistryFailureFunc) (WockyAuthRegistry *self,
    GError *error);

struct _WockyAuthRegistry
{
  /*<private>*/
  GObject parent;

  WockyAuthRegistryPrivate *priv;
};

struct _WockyAuthRegistryClass
{
  /*<private>*/
  GObjectClass parent_class;

  /*<public>*/
  WockyAuthRegistryStartAuthAsyncFunc start_auth_async_func;
  WockyAuthRegistryStartAuthFinishFunc start_auth_finish_func;

  WockyAuthRegistryChallengeAsyncFunc challenge_async_func;
  WockyAuthRegistryChallengeFinishFunc challenge_finish_func;

  WockyAuthRegistrySuccessAsyncFunc success_async_func;
  WockyAuthRegistrySuccessFinishFunc success_finish_func;

  WockyAuthRegistryFailureFunc failure_func;
};

GType wocky_auth_registry_get_type (void) G_GNUC_CONST;

WockyAuthRegistry *wocky_auth_registry_new (void);

void wocky_auth_registry_start_auth_async (WockyAuthRegistry *self,
    GSList *mechanisms,
    gboolean allow_plain,
    gboolean is_secure_channel,
    const gchar *username,
    const gchar *password,
    const gchar *server,
    const gchar *session_id,
    GAsyncReadyCallback callback,
    gpointer user_data);

gboolean wocky_auth_registry_start_auth_finish (WockyAuthRegistry *self,
    GAsyncResult *result,
    WockyAuthRegistryStartData **start_data,
    GError **error);

void wocky_auth_registry_challenge_async (WockyAuthRegistry *self,
    const GString *challenge_data,
    GAsyncReadyCallback callback,
    gpointer user_data);

gboolean wocky_auth_registry_challenge_finish (WockyAuthRegistry *self,
    GAsyncResult *res,
    GString **response,
    GError **error);

void wocky_auth_registry_success_async (WockyAuthRegistry *self,
    GAsyncReadyCallback callback,
    gpointer user_data);

gboolean wocky_auth_registry_success_finish (WockyAuthRegistry *self,
    GAsyncResult *res,
    GError **error);

void wocky_auth_registry_add_handler (WockyAuthRegistry *self,
    WockyAuthHandler *handler);

void wocky_auth_registry_start_data_free (
    WockyAuthRegistryStartData *start_data);

WockyAuthRegistryStartData * wocky_auth_registry_start_data_new (
    const gchar *mechanism,
    const GString *initial_response);

WockyAuthRegistryStartData * wocky_auth_registry_start_data_dup (
    WockyAuthRegistryStartData *start_data);

void wocky_auth_registry_failure (WockyAuthRegistry *self,
    GError *error);

gboolean wocky_auth_registry_supports_one_of (WockyAuthRegistry *self,
    GSList *mechanisms,
    gboolean allow_plain);

G_END_DECLS

#endif /* _WOCKY_AUTH_REGISTRY_H */